Compare commits

..
2 Commits
Author SHA1 Message Date
imSp4rky 03bcfc0c45 perf(hls): rename single decrypted range instead of re-copying
Short-circuit merge() to os.replace() when joining one already-merged file with no init prefix; saves a full track-size copy, falls back to streamed copy across devices.
2026-06-29 09:55:08 -06:00
imSp4rky efb560f887 fix(hls): stream segment merges to avoid OOM on large tracks
Replace whole-file read_bytes() in merge()/discontinuity merge with shutil.copyfileobj (1MB buffer); fixes MemoryError on multi-GB tracks.
2026-06-29 09:31:38 -06:00
+12 -4
View File
@@ -740,11 +740,19 @@ class HLS:
delete: Delete the file once it's been merged.
include_map_data: Whether to include the init map data.
"""
prepend = include_map_data and map_data and map_data[1]
if len(via) == 1 and not prepend and delete:
try:
os.replace(via[0], to)
return
except OSError:
pass
with open(to, "wb") as x:
if include_map_data and map_data and map_data[1]:
if prepend:
x.write(map_data[1])
for file in via:
x.write(file.read_bytes())
with open(file, "rb") as segment:
shutil.copyfileobj(segment, x, 1024 * 1024)
x.flush()
if delete:
file.unlink()
@@ -1005,8 +1013,8 @@ class HLS:
else:
with open(save_path, "wb") as f:
for discontinuity_file in segments_to_merge:
discontinuity_data = discontinuity_file.read_bytes()
f.write(discontinuity_data)
with open(discontinuity_file, "rb") as src:
shutil.copyfileobj(src, f, 1024 * 1024)
f.flush()
os.fsync(f.fileno())
discontinuity_file.unlink()