Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: use config proxy with ffprobe (#1821)

NTFSvolume98edf8b042dd
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 24a500319..9db180a33 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -53,4 +53,4 @@ jobs:
- name: Run pytest
timeout-minutes: 10
run: |
- uv run pytest -v --cov
+ uv run pytest --ignore=tests/test_ffmpeg.py -v --cov
diff --git a/.github/workflows/ffmpeg.yaml b/.github/workflows/ffmpeg.yaml
new file mode 100644
index 000000000..f22de66b4
--- /dev/null
+++ b/.github/workflows/ffmpeg.yaml
@@ -0,0 +1,43 @@
+name: FFmpeg
+
+on:
+ pull_request:
+ branches:
+ - main
+ paths:
+ - "cyberdrop_dl/ffmpeg.py"
+ - "cyberdrop_dl/pyproject.toml"
+ - "tests/test_ffmpeg.py"
+ workflow_dispatch:
+
+permissions: {}
+
+jobs:
+ test_ffmpeg:
+ name: FFmpeg tests
+ permissions:
+ contents: read
+
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Checkout code
+ uses: actions/[email protected]
+ with:
+ persist-credentials: false
+
+ - name: Install uv
+ uses: astral-sh/[email protected]
+ with:
+ python-version: ${{ matrix.python-version }}
+ enable-cache: false
+
+ - name: Install dependencies
+ run: |
+ sudo apt install ffmpeg -y
+ uv sync --all-extras
+
+ - name: Run pytest
+ timeout-minutes: 10
+ run: |
+ uv run pytest tests/test_ffmpeg.py -v --cov
diff --git a/cyberdrop_dl/clients/downloads.py b/cyberdrop_dl/clients/downloads.py
index b1bebad5d..4a33fdb6a 100644
--- a/cyberdrop_dl/clients/downloads.py
+++ b/cyberdrop_dl/clients/downloads.py
@@ -407,23 +407,27 @@ async def filter_by_duration(media_item: MediaItem, config: Config) -> bool:
if limits is None:
return False
- media_item.duration = await _probe_duration(media_item)
+ if media_item.duration is None:
+ media_item.duration = _get_duration(await _probe_item(media_item, config))
if media_item.duration is None:
return False
return media_item.duration not in limits
-async def _probe_duration(media_item: MediaItem) -> float | None:
- if media_item.duration:
- return media_item.duration
-
+async def _probe_item(media_item: MediaItem, config: Config) -> ffmpeg.FFprobeResult:
if media_item.downloaded:
- properties = await ffmpeg.probe(media_item.path)
+ return await ffmpeg.probe(media_item.path)
+
+ return await ffmpeg.probe_url(
+ media_item.url,
+ headers=media_item.headers,
+ proxy=config.global_settings.general.proxy,
+ verify=bool(config.global_settings.general.ssl_context),
+ )
- else:
- properties = await ffmpeg.probe(media_item.url, headers=media_item.headers)
+def _get_duration(properties: ffmpeg.FFprobeResult) -> float | None:
if properties.format.duration:
return properties.format.duration
if properties.video:
diff --git a/cyberdrop_dl/ffmpeg.py b/cyberdrop_dl/ffmpeg.py
index 66d4f85ef..3bfea3c02 100644
--- a/cyberdrop_dl/ffmpeg.py
+++ b/cyberdrop_dl/ffmpeg.py
@@ -10,8 +10,7 @@ import shutil
import subprocess
import uuid
from fractions import Fraction
-from pathlib import Path
-from typing import TYPE_CHECKING, Any, Literal, Self, TypeAlias, TypedDict, overload
+from typing import TYPE_CHECKING, Any, Literal, Self, TypeAlias, TypedDict
from multidict import CIMultiDict, CIMultiDictProxy
@@ -19,6 +18,7 @@ from cyberdrop_dl.utils import DictDataclass
if TYPE_CHECKING:
from collections.abc import Generator, Iterable, Iterator, Mapping, Sequence
+ from pathlib import Path
from cyberdrop_dl.url_objects import AbsoluteHttpURL
@@ -177,27 +177,37 @@ def _raw_concat(files: Iterable[Path], output: Path) -> None:
file.unlink()
-@overload
-async def probe(input: Path, /) -> FFprobeResult: ...
+async def probe(input: Path, /) -> FFprobeResult:
+ assert input.is_absolute()
+ command = *_FFPROBE_CALL_PREFIX, str(input)
+ return await _probe(command)
-@overload
-async def probe(input: AbsoluteHttpURL, /, *, headers: Mapping[str, str] | None = None) -> FFprobeResult: ...
+async def probe_url(
+ input: AbsoluteHttpURL,
+ /,
+ *,
+ headers: Mapping[str, str] | None = None,
+ proxy: AbsoluteHttpURL | None = None,
+ verify: bool = True,
+) -> FFprobeResult:
+ def extra_params() -> Generator[str]:
+ if headers:
+ for name, value in headers.items():
+ yield "-headers"
+ yield f"{name}: {value}"
-async def probe(input: Path | AbsoluteHttpURL, /, *, headers: Mapping[str, str] | None = None) -> FFprobeResult:
- _check_ffprobe()
+ if proxy:
+ yield "-http_proxy"
+ yield str(proxy)
- if isinstance(input, Path):
- assert input.is_absolute()
- assert not headers
+ command = *_FFPROBE_CALL_PREFIX, "-tls_verify", str(int(verify)), str(input), *extra_params()
+ return await _probe(command)
- command = *_FFPROBE_CALL_PREFIX, str(input)
- if headers:
- command = (
- *command,
- *itertools.chain.from_iterable(("-headers", f"{name}: {value}") for name, value in headers.items()),
- )
+
+async def _probe(command: Sequence[str | Path]) -> FFprobeResult:
+ _check_ffprobe()
result = await _run_command(command)
if not result.success:
return _EMPTY_FFPROBE_RESULT
diff --git a/tests/test_ffmpeg.py b/tests/test_ffmpeg.py
index bae85c7f8..05cc115ed 100644
--- a/tests/test_ffmpeg.py
+++ b/tests/test_ffmpeg.py
@@ -1,18 +1,118 @@
+import contextlib
import datetime
+from collections.abc import Generator
from pathlib import Path
+from typing import Any
+from unittest.mock import AsyncMock, patch
import pytest
from cyberdrop_dl import ffmpeg
from cyberdrop_dl.url_objects import AbsoluteHttpURL
-FFPROBE_IS_INSTALLED = bool(ffmpeg.ffprobe_version())
-pytestmark = pytest.mark.skipif(not FFPROBE_IS_INSTALLED, reason="ffprobe is not installed")
+def mock_call() -> Generator[AsyncMock]:
+ with patch("cyberdrop_dl.ffmpeg._run_command", new_callable=AsyncMock) as m:
+ m.return_value = ffmpeg.SubProcessResult(
+ stdout="{}",
+ stderr="",
+ return_code=0,
+ )
+ yield m
+
+
+def last_call_args(mock: AsyncMock) -> list[Any]:
+ return list(mock.call_args.args[0])
+
+
+def get_probe_args(mock: AsyncMock) -> list[Any]:
+ return last_call_args(mock)[len(ffmpeg._FFPROBE_CALL_PREFIX) :]
+
+
+async def test_probe_url_command() -> None:
+ url = AbsoluteHttpURL("https://example.com/stream.m3u8")
+ with mock_call() as m:
+ await ffmpeg.probe_url(url)
+ assert last_call_args(m) == [
+ "ffprobe",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ "-show_streams",
+ "-show_format",
+ "-print_format",
+ "json",
+ "-tls_verify",
+ "1",
+ "https://example.com/stream.m3u8",
+ ]
+
+
+async def test_probe_url_w_headers() -> None:
+ url = AbsoluteHttpURL("https://example.com/stream.m3u8")
+
+ with mock_call() as m:
+ await ffmpeg.probe_url(
+ url,
+ headers={
+ "Authorization": "Bearer token",
+ "User-Agent": "test",
+ },
+ )
+
+ args = get_probe_args(m)
+ assert args == [
+ "-tls_verify",
+ "1",
+ "https://example.com/stream.m3u8",
+ "-headers",
+ "Authorization: Bearer token",
+ "-headers",
+ "User-Agent: test",
+ ]
+
+
+async def test_probe_url_w_proxy() -> None:
+ url = AbsoluteHttpURL("https://example.com/stream.m3u8")
+ proxy = AbsoluteHttpURL("http://proxy.internal:8080")
+ with mock_call() as m:
+ await ffmpeg.probe_url(url, proxy=proxy)
+
+ args = last_call_args(m)
+ idx = args.index("-http_proxy")
+ assert args[idx + 1] == "http://proxy.internal:8080"
+
+
+async def test_probe_url_w_verify_disabled() -> None:
+ url = AbsoluteHttpURL("https://example.com/stream.m3u8")
+ with mock_call() as m:
+ await ffmpeg.probe_url(url, verify=False)
+
+ args = get_probe_args(m)
+ assert args == ["-tls_verify", "0", "https://example.com/stream.m3u8"]
+
+
+async def test_probe_url_proxy_headers_no_verify() -> None:
+ url = AbsoluteHttpURL("https://example.com/stream.m3u8")
+ proxy = AbsoluteHttpURL("http://proxy:8080")
+ with mock_call() as m:
+ await ffmpeg.probe_url(url, headers={"X-Custom": "value"}, proxy=proxy, verify=False)
+
+ args = get_probe_args(m)
+ assert args == [
+ "-tls_verify",
+ "0",
+ "https://example.com/stream.m3u8",
+ "-headers",
+ "X-Custom: value",
+ "-http_proxy",
+ "http://proxy:8080",
+ ]
async def test_ffprobe_video_url() -> None:
- output = await ffmpeg.probe(
+ output = await ffmpeg.probe_url(
AbsoluteHttpURL("https://videos.pexels.com/video-files/29691053/12769314_360_640_60fps.mp4")
)