Open-source X Spaces transcription.

Star

46 lines · 1.3 KB

python
1from __future__ import annotations
2
3import subprocess
4import sys
5import tempfile
6from pathlib import Path
7from unittest import mock
8
9sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
10
11from space_tape import download as dl
12
13
14def _captured_cmd() -> list[str]:
15 seen: list[list[str]] = []
16
17 def fake_run(cmd, **kwargs):
18 seen.append(cmd)
19 out = Path(cmd[cmd.index("-o") + 1].replace("%(ext)s", "m4a"))
20 out.write_bytes(b"ID3")
21 return subprocess.CompletedProcess(cmd, 0, "", "")
22
23 with tempfile.TemporaryDirectory() as tmp, \
24 mock.patch.object(dl.shutil, "which", return_value="/usr/bin/yt-dlp"), \
25 mock.patch.object(dl.subprocess, "run", side_effect=fake_run):
26 dl.download("https://x.com/i/spaces/1pKdRDlVbRrJW", tmp)
27 return seen[0]
28
29
30def test_download_keeps_hydra_id3() -> None:
31 # ffmpeg's HLS demuxer and yt-dlp's FixupM3u8 both strip the ID3 frames.
32 cmd = _captured_cmd()
33 assert cmd[cmd.index("--downloader") + 1] == "m3u8:native"
34 assert cmd[cmd.index("--fixup") + 1] == "never"
35
36
37def test_download_rerun_overwrites() -> None:
38 # --no-part + native HLS otherwise appends a second copy on a re-run.
39 assert "--force-overwrites" in _captured_cmd()
40
41
42if __name__ == "__main__":
43 test_download_keeps_hydra_id3()
44 test_download_rerun_overwrites()
45 print("ok")