From f36d45fb5e0058783acfab612514a9f6891d398c Mon Sep 17 00:00:00 2001 From: Barbagus Date: Wed, 25 Jan 2023 08:53:25 +0100 Subject: [PATCH] Enable interrupt/resume of MP4 streams - skipping the processing of an existing target output file - skipping the download of an existing target stream file - resume the download of an existing target stream temporary file using a HTTP range request --- src/delarte/__init__.py | 5 +++++ src/delarte/download.py | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/delarte/__init__.py b/src/delarte/__init__.py index 75799e9..7adb7ee 100644 --- a/src/delarte/__init__.py +++ b/src/delarte/__init__.py @@ -132,6 +132,11 @@ def download_targets(targets, http_session, on_progress): from .muxing import mux_target for target in targets: + output_path = f"{target.output}.mkv" + + if os.path.isfile(output_path): + print(f"Skipping {output_path!r}") + continue video_path = target.output + ".video.mp4" audio_path = target.output + ".audio.mp4" diff --git a/src/delarte/download.py b/src/delarte/download.py index b765380..d89812f 100644 --- a/src/delarte/download.py +++ b/src/delarte/download.py @@ -19,10 +19,14 @@ def download_mp4_media(url, file_name, http_session, on_progress): temp_file = f"{file_name}.tmp" - with open(temp_file, "w+b") as f: - r = http_session.get(url, timeout=5, stream=True) + with open(temp_file, "ab") as f: + r = http_session.get( + url, timeout=5, stream=True, headers={"Range": f"bytes={f.tell()}-"} + ) r.raise_for_status() - total = int(r.headers["content-length"]) + + _, total = r.headers["content-range"].split("/") + total = int(total) for content in r.iter_content(_CHUNK): f.write(content)