Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
refactor: rework ffmpeg module (#2118)
NTFSvolumefc6a425a8308
diff --git a/cyberdrop_dl/ffmpeg.py b/cyberdrop_dl/ffmpeg.pyindex d69dbb0e4..e9dd7129d 100644--- a/cyberdrop_dl/ffmpeg.py+++ b/cyberdrop_dl/ffmpeg.py@@ -2,19 +2,16 @@ from __future__ import annotationsimport asyncioimport dataclasses-import functools-import itertoolsimport jsonimport loggingimport shutil-import subprocessimport uuid-from fractions import Fractionfrom typing import TYPE_CHECKING, Any, Literal, Self, TypedDictfrom multidict import CIMultiDict, CIMultiDictProxyfrom cyberdrop_dl import aio+from cyberdrop_dl.utils import fast_cachefrom cyberdrop_dl.utils.dataclass import DictDataclassif TYPE_CHECKING:@@ -26,130 +23,152 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)+type CMD = Sequence[str | Path]-class Args:- CODEC_COPY = "-c", "copy"- MAP_ALL_STREAMS = "-map", "0"- CONCAT = "-f", "concat", "-safe", "0", "-i"- FIXUP_MP4 = *MAP_ALL_STREAMS, "-ignore_unknown", *CODEC_COPY, "-f", "mp4", "-movflags", "+faststart"- FIXUP_AUDIO_DTS_FILTER = "-bsf:a", "aac_adtstoasc"+@fast_cache+def _which(program: str) -> str | None:+ return shutil.which(program)-_FFMPEG_CALL_PREFIX = "ffmpeg", "-y", "-loglevel", "error"-_FFPROBE_CALL_PREFIX = (- "ffprobe",- "-hide_banner",- "-loglevel",- "error",- "-show_streams",- "-show_format",- "-print_format",- "json",-)+@fast_cache+def _get_bin_version(bin_path: str) -> str | None:+ import subprocess-def is_installed() -> bool:try:- _check()- except RuntimeError:- return False- else:- return True---def _check() -> None:- if not version():- raise RuntimeError("ffmpeg is not installed")- _check_ffprobe()-+ stdout = subprocess.run(+ (bin_path, "-version"),+ timeout=5,+ check=True,+ stdin=subprocess.PIPE,+ stdout=subprocess.PIPE,+ stderr=subprocess.DEVNULL,+ ).stdout.decode("utf-8", errors="ignore")-def _check_ffprobe() -> None:- if not ffprobe_version():- raise RuntimeError("ffprobe is not installed")+ except Exception: # noqa: BLE001+ return None+ else:+ return stdout.partition("version")[-1].partition("Copyright")[0].strip()def which_ffmpeg() -> str | None:- return shutil.which("ffmpeg")+ return _which("ffmpeg")def which_ffprobe() -> str | None:- return shutil.which("ffprobe")+ return _which("ffprobe")def version() -> str | None:if bin_path := which_ffmpeg():return _get_bin_version(bin_path)def ffprobe_version() -> str | None:if bin_path := which_ffprobe():return _get_bin_version(bin_path)-async def merge(input_files: Iterable[Path], output_file: Path) -> SubProcessResult:- result = await _merge(input_files, output_file)+def is_installed() -> bool:+ return bool(version() and ffprobe_version())+++async def run(args: CMD) -> SubProcessResult:+ assert args, "Supply at least 1 argument"+ if not version():+ raise RuntimeError("ffmpeg is not installed")+ cmd = "ffmpeg", "-y", "-nostats", "-loglevel", "warning", "-hide_banner", *args+ return await _run_cmd(cmd)+++async def ffprobe_run(args: CMD) -> SubProcessResult:+ assert args, "Supply at least 1 argument"+ if not ffprobe_version():+ raise RuntimeError("ffprobe is not installed")+ cmd = "ffprobe", "-hide_banner", "-loglevel", "warning", "-show_streams", "-show_format", "-print_format", "json"+ return await _run_cmd((*cmd, *args))+++async def _aac_dts_fix_args(audio_stream: Path) -> tuple[str, ...]:+ # Old issue of ffmpeg, fixed in v5.0+ # https://trac.ffmpeg.org/ticket/9433+ result = await probe(audio_stream)+ if result and (audio := result.audio) and audio.codec == "aac":+ return "-bsf:a", "aac_adtstoasc"+ return ()+++async def merge(video: Path, audio: Path, output: Path) -> SubProcessResult:+ inputs = "-i", video, "-i", audio, "-map", "0:v:0", "-map", "1:a:0", "-c", "copy", *(await _aac_dts_fix_args(audio))+ args = *inputs, "-movflags", "+faststart", output+ result = await run(args)if result.success:- await aio.gather(*map(_try_delete, input_files))+ await aio.gather(_try_delete(video), _try_delete(audio))return result-async def _merge(input_files: Iterable[Path], output_file: Path) -> SubProcessResult:- inputs = itertools.chain.from_iterable(("-i", path) for path in input_files)- command = *_FFMPEG_CALL_PREFIX, *inputs, *Args.MAP_ALL_STREAMS, *Args.CODEC_COPY, output_file- return await _run_command(command)+def quote_concat_arg(arg: str) -> str:+ arg = arg.replace("'", r"'\''").replace("'''", "'")+ arg = arg[1:] if arg[0] == "'" else "'" + arg+ return arg[:-1] if arg[-1] == "'" else arg + "'"+++def create_concat_doc(files: Iterable[Path]) -> str:+ "Input paths MUST be absolute!!."++ def lines():+ yield "ffconcat version 1.0"+ for file in files:+ if not file.is_absolute():+ raise ValueError("file is not an absolute path", file)+ yield f"file {quote_concat_arg(str(file))}"++ return "\n".join(lines())+++async def concat(files: Sequence[Path], output: Path) -> SubProcessResult:+ """Concatenate fragments of the same video.+ All files must have the same streams (same codecs, resolution, time base, etc..)"""+ # https://trac.ffmpeg.org/wiki/Concatenate#demuxer-async def concat(input_files: Iterable[Path], output_file: Path) -> SubProcessResult:- concat_file = output_file.with_suffix(output_file.suffix + ".ffmpeg_concat.txt")- await _create_concat_file(input_files, output_file=concat_file)+ concat_in = output.with_suffix(output.suffix + ".ffconcat.txt")+ concat_out = output.with_suffix(".concat" + output.suffix)+ logger.debug("Writing ffmpeg concat file to '%s'", concat_in)+ await aio.write_text(concat_in, create_concat_doc(files), encoding="utf8")++ args = "-f", "concat", "-safe", "0", "-i", concat_in, "-c", "copy", concat_outtry:- result = await _concat(concat_file, output_file)+ result = await run(args)if result.success:- await aio.gather(*map(_try_delete, input_files))+ await aio.move(concat_out, output)+ await aio.gather(*(_try_delete(f) for f in files))+ else:+ await _try_delete(concat_out)finally:- await _try_delete(concat_file)+ await _try_delete(concat_in)return result-async def _concat(input_file: Path, /, output: Path) -> SubProcessResult:- temp_file = output.with_suffix(".concat" + output.suffix)- command = *_FFMPEG_CALL_PREFIX, *Args.CONCAT, input_file, *Args.CODEC_COPY, temp_file- result = await _run_command(command)- if result.success:- await aio.move(temp_file, output)- else:- await _try_delete(temp_file)- return result---async def _create_concat_file(input_files: Iterable[Path], output_file: Path) -> None:- # Input paths MUST be absolute!!.+async def optimize(file: Path) -> SubProcessResult:+ """+ - Make MP4s actual MP4s instead of MPEG-TS+ - Fix broken timestamps+ - Optimize for streaming (faststart)- def write() -> None:- with output_file.open("w", encoding="utf8") as f:- f.writelines(f"file '{file}'\n" for file in input_files)+ Should only be used for HLS downloads"""- return await asyncio.to_thread(write)+ fixup_out = file.with_suffix(".fixup" + file.suffix)+ inputs = "-i", file, "-map", "0", "-ignore_unknown", "-c", "copy", "-f", "mp4", *(await _aac_dts_fix_args(file))+ args = *inputs, "-movflags", "+faststart", fixup_out+ result = await run(args)--async def fixup_video(file: Path) -> SubProcessResult:- temp_file = file.with_suffix(".fixup" + file.suffix)- command = *_FFMPEG_CALL_PREFIX, "-i", file, *Args.FIXUP_MP4- probe_result = await probe(file)- if probe_result and (audio := probe_result.audio) and audio.codec == "aac":- command = *command, *Args.FIXUP_AUDIO_DTS_FILTER- command = *command, temp_file- result = await _run_command(command)if result.success:await aio.unlink(file)- await aio.move(temp_file, file)+ await aio.move(fixup_out, file)else:- await _try_delete(temp_file)+ await _try_delete(fixup_out)return result@@ -158,16 +177,15 @@ async def _try_delete(file: Path) -> None:try:await aio.unlink(file, missing_ok=True)except OSError as e:- logger.warning(f"Unable to delete '{file}' {e}")+ logger.warning("Unable to delete '%s': %r", file, e)-async def raw_concat(files: Iterable[Path], output: Path) -> None:- logger.debug("Merging subs to '%s'", output)- await asyncio.to_thread(_raw_concat, files, output)- await aio.gather(*map(_try_delete, files))+async def raw_concat(files: Sequence[Path], output: Path) -> None:+ await asyncio.to_thread(_concat_bytes, files, output)+ await aio.gather(*(_try_delete(f) for f in files))-def _raw_concat(files: Iterable[Path], output: Path) -> None:+def _concat_bytes(files: Iterable[Path], output: Path) -> None:with output.open("wb") as out:for file in files:with file.open("rb") as fp_in:@@ -176,8 +194,7 @@ def _raw_concat(files: Iterable[Path], output: Path) -> None:async def probe(file: Path, /) -> FFprobeResult:assert file.is_absolute()- command = *_FFPROBE_CALL_PREFIX, str(file)- return await _probe(command)+ return await _probe([str(file)])async def probe_url(@@ -186,48 +203,35 @@ async def probe_url(*,headers: Mapping[str, str] | None = None,proxy: AbsoluteHttpURL | None = None,+ cookies: tuple[str, ...] = (),verify: bool = True,) -> FFprobeResult:def extra_params() -> Generator[str]:if headers:- for name, value in headers.items():- yield "-headers"- yield f"{name}: {value}"+ yield "-headers"+ yield "".join(f"{name}: {value}\r\n" for name, value in headers.items())if proxy:+ # Only available since v5.0yield "-http_proxy"yield str(proxy)- command = *_FFPROBE_CALL_PREFIX, "-tls_verify", str(int(verify)), str(url), *extra_params()- return await _probe(command)+ if cookies:+ # TODO: add cookies support+ pass++ args = "-tls_verify", str(int(verify)), *extra_params(), str(url)+ return await _probe(args)-async def _probe(command: Sequence[str | Path]) -> FFprobeResult:- _check_ffprobe()- result = await _run_command(command)+async def _probe(args: CMD) -> FFprobeResult:+ result = await ffprobe_run(args)if not result.success:return _EMPTY_FFPROBE_RESULTreturn FFprobeResult.from_output(json.loads(result.stdout))-def _get_bin_version(bin_path: str) -> str | None:- try:- stdout = subprocess.run(- (bin_path, "-version"),- timeout=5,- check=True,- stdin=subprocess.PIPE,- stdout=subprocess.PIPE,- stderr=subprocess.DEVNULL,- ).stdout.decode("utf-8", errors="ignore")-- except Exception: # noqa: BLE001- return None- else:- return stdout.partition("version")[-1].partition("Copyright")[0].strip()--# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ FFprobe ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@@ -319,6 +323,8 @@ class VideoStream(Stream):resolution: str | None = f"{width}x{height}"if (avg_fps := stream_info.get("avg_frame_rate")) and str(avg_fps) not in {"0/0", "0", "0.0"}:+ from fractions import Fraction+fps: TruncatedFloat | None = TruncatedFloat(Fraction(avg_fps))defaults = super(VideoStream, cls).validate(stream_info)@@ -425,21 +431,35 @@ class SubProcessResult:return str(self.__json__())-async def _run_command(command: Sequence[str | Path]) -> SubProcessResult:- assert not isinstance(command, str)- program, *cmd = command+def _split_cmd(command: CMD) -> tuple[str, str, list[str | Path]]:+ program, *args = command++ match program:+ case "ffmpeg":+ path = which_ffmpeg()+ case "ffprobe":+ path = which_ffprobe()+ case _:+ raise ValueError(f"Unexpected program in command {command}")++ assert path+ return program, path, args- if program == "ffmpeg":- bin_path = which_ffmpeg()- elif program == "ffprobe":- bin_path = which_ffprobe()- else:- raise ValueError(f"Unexpected program in command {command}")- assert bin_path+async def _run_cmd(command: CMD) -> SubProcessResult:+ assert not isinstance(command, str)+ program, path, args = _split_cmd(command)+ import asyncio.subprocess+process_id = str(uuid.uuid4())- logger.debug("Running %s subprocess [id=%s]:\n%s", program, process_id, {"command": [bin_path, *map(str, cmd)]})- process = await asyncio.create_subprocess_exec(bin_path, *cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)+ logger.debug("Running %s subprocess [id=%s]:\n%s", program, process_id, (path, *map(str, args)))++ process = await asyncio.subprocess.create_subprocess_exec(+ path,+ *args,+ stdout=asyncio.subprocess.PIPE,+ stderr=asyncio.subprocess.PIPE,+ )stdout, stderr = await process.communicate()result = SubProcessResult(stdout=stdout.decode("utf-8", errors="ignore"),diff --git a/tests/test_ffmpeg.py b/tests/test_ffmpeg.pyindex cd77017c9..5ca26f461 100644--- a/tests/test_ffmpeg.py+++ b/tests/test_ffmpeg.py@@ -13,7 +13,7 @@ from cyberdrop_dl.url_objects import AbsoluteHttpURL@contextlib.contextmanagerdef mock_call() -> Generator[AsyncMock]:- with patch("cyberdrop_dl.ffmpeg._run_command", new_callable=AsyncMock) as m:+ with patch("cyberdrop_dl.ffmpeg._run_cmd", new_callable=AsyncMock) as m:m.return_value = ffmpeg.SubProcessResult(stdout="{}",stderr="",@@ -27,7 +27,7 @@ def last_call_args(mock: AsyncMock) -> list[Any]:def get_probe_args(mock: AsyncMock) -> list[Any]:- return last_call_args(mock)[len(ffmpeg._FFPROBE_CALL_PREFIX) :]+ return last_call_args(mock)[8:]@pytest.mark.http@@ -183,18 +183,32 @@ class TestMergeSubs:out = tmp_path / "merged.srt"- ffmpeg._raw_concat(srcs, out)+ ffmpeg._concat_bytes(srcs, out)assert out.read_bytes() == b"AAA\nBBB\nCCC\n"def test_empty_input(self, tmp_path: Path) -> None:out = tmp_path / "empty.srt"- ffmpeg._raw_concat([], out)+ ffmpeg._concat_bytes([], out)assert out.read_bytes() == b""def test_single_file(self, tmp_path: Path) -> None:src = tmp_path / "subtitles.srt"src.write_bytes(b" test \n")out = tmp_path / "out.srt"- ffmpeg._raw_concat([src], out)+ ffmpeg._concat_bytes([src], out)assert out.read_bytes() == b" test \n"+++ ("arg", "expected"),+ [+ ("Crime dAmour", r"'Crime dAmour'"),+ ("'Crime dAmour'", r"\''Crime dAmour'\'"),+ ("Crime d'Amour", r"'Crime d'\''Amour'"),+ (" this string starts and ends with whitespaces ", "' this string starts and ends with whitespaces '"),+ (r"c:\foo", r"'c:\foo'"),+ ],+)+def test_quote(arg: str, expected: str) -> None:+ assert ffmpeg.quote_concat_arg(arg) == expected