fix(DASH/HLS): Don't merge folders, skip final merge if only 1 segment

This commit is contained in:
rlaphoenix 2024-03-09 01:37:55 +00:00
parent 77e663ebee
commit 4d6c72ba30
2 changed files with 45 additions and 34 deletions

View File

@ -486,28 +486,33 @@ class DASH:
for control_file in save_dir.glob("*.aria2__temp"): for control_file in save_dir.glob("*.aria2__temp"):
control_file.unlink() control_file.unlink()
segments_to_merge = sorted(save_dir.iterdir()) segments_to_merge = [
progress(downloaded="Merging", completed=0, total=len(segments_to_merge)) x
for x in sorted(save_dir.iterdir())
if x.is_file()
]
with open(save_path, "wb") as f: with open(save_path, "wb") as f:
if init_data: if init_data:
f.write(init_data) f.write(init_data)
for segment_file in segments_to_merge: if len(segments_to_merge) > 1:
segment_data = segment_file.read_bytes() progress(downloaded="Merging", completed=0, total=len(segments_to_merge))
# TODO: fix encoding after decryption? else:
if ( for segment_file in segments_to_merge:
not drm and isinstance(track, Subtitle) and segment_data = segment_file.read_bytes()
track.codec not in (Subtitle.Codec.fVTT, Subtitle.Codec.fTTML) # TODO: fix encoding after decryption?
): if (
segment_data = try_ensure_utf8(segment_data) not drm and isinstance(track, Subtitle) and
segment_data = segment_data.decode("utf8"). \ track.codec not in (Subtitle.Codec.fVTT, Subtitle.Codec.fTTML)
replace("‎", html.unescape("‎")). \ ):
replace("‏", html.unescape("‏")). \ segment_data = try_ensure_utf8(segment_data)
encode("utf8") segment_data = segment_data.decode("utf8"). \
f.write(segment_data) replace("‎", html.unescape("‎")). \
f.flush() replace("‏", html.unescape("‏")). \
segment_file.unlink() encode("utf8")
progress(advance=1) f.write(segment_data)
f.flush()
segment_file.unlink()
progress(advance=1)
track.path = save_path track.path = save_path
if callable(track.OnDownloaded): if callable(track.OnDownloaded):

View File

@ -508,22 +508,28 @@ class HLS:
return return
# finally merge all the discontinuity save files together to the final path # finally merge all the discontinuity save files together to the final path
progress(downloaded="Merging") segments_to_merge = [
if isinstance(track, (Video, Audio)): x
HLS.merge_segments( for x in sorted(save_dir.iterdir())
segments=sorted(list(save_dir.iterdir())), if x.is_file()
save_path=save_path ]
) if len(segments_to_merge) == 1:
shutil.rmtree(save_dir) shutil.move(segments_to_merge[0], save_path)
else: else:
with open(save_path, "wb") as f: progress(downloaded="Merging")
for discontinuity_file in sorted(save_dir.iterdir()): if isinstance(track, (Video, Audio)):
if discontinuity_file.is_dir(): HLS.merge_segments(
continue segments=segments_to_merge,
discontinuity_data = discontinuity_file.read_bytes() save_path=save_path
f.write(discontinuity_data) )
f.flush() shutil.rmtree(save_dir)
shutil.rmtree(save_dir) 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)
f.flush()
shutil.rmtree(save_dir)
progress(downloaded="Downloaded") progress(downloaded="Downloaded")