fix: yt-dlp im temporären Verzeichnis ausführen (Streudatei „3“)

yt-dlp ruft zur Prüfung „ffmpeg -bsfs“ auf; das System-ffmpeg 6.1.1
(Ubuntu) legt dabei eine leere Datei „3“ im Arbeitsverzeichnis an.
yt-dlp läuft jetzt in einem temporären Verzeichnis mit absolutem
Zielpfad, die Datei verschwindet mit dem Verzeichnis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dieter Schlüter 2026-10-10 20:12:08 +02:00
commit 7e6ffa5899
2 changed files with 40 additions and 13 deletions

View file

@ -12,13 +12,15 @@ def fake_yt_dlp():
"""Factory for a subprocess.run stand-in.
It answers the info request with `info` and writes `vtt` where yt-dlp would
write the subtitle file. `run.calls` records every command.
write the subtitle file. `run.calls` records every command, `run.cwds` the
working directories.
"""
def make(info, vtt="WEBVTT\n", write=True, fail_download=False):
calls = []
calls, cwds = [], []
def run(command, capture_output, text):
def run(command, capture_output, text, cwd=None):
calls.append(command)
cwds.append(cwd)
if "--dump-single-json" in command:
return subprocess.CompletedProcess(command, 0, json.dumps(info), "")
if fail_download:
@ -32,6 +34,7 @@ def fake_yt_dlp():
return subprocess.CompletedProcess(command, 0, "", "")
run.calls = calls
run.cwds = cwds
return run
return make

View file

@ -1,6 +1,7 @@
"""Tests for yt_untertitel.py; yt-dlp is always replaced by fakes (no network)."""
import subprocess
import sys
from pathlib import Path
import pytest
@ -67,6 +68,19 @@ def test_main_downloads_auto_original(fake_yt_dlp, tmp_path, monkeypatch, capsys
assert "vtt2txt.py -i" in out
def test_yt_dlp_runs_in_temporary_directory(fake_yt_dlp, tmp_path, monkeypatch):
# ffmpeg -bsfs, called by yt-dlp, leaves a file "3" in its working directory
run = fake_yt_dlp(AUTO_ONLY)
monkeypatch.setattr(yt.subprocess, "run", run)
monkeypatch.chdir(tmp_path)
run_main(monkeypatch, "-u", URL, "-n", "Rede")
assert all(cwd is not None and Path(cwd) != tmp_path for cwd in run.cwds)
template = run.calls[1][run.calls[1].index("-o") + 1]
assert template == f"{tmp_path / 'Rede'}.%(ext)s"
assert (tmp_path / "Rede.de-orig.vtt").is_file()
def test_main_prefers_manual_and_defaults_to_video_id(fake_yt_dlp, tmp_path, monkeypatch):
info = dict(AUTO_ONLY, subtitles={"de": []})
run = fake_yt_dlp(info)
@ -84,7 +98,7 @@ def test_main_no_matching_track(fake_yt_dlp, tmp_path, monkeypatch):
def test_main_info_error(tmp_path, monkeypatch):
def run(command, capture_output, text):
def run(command, capture_output, text, cwd=None):
return subprocess.CompletedProcess(command, 1, "", "ERROR: Video unavailable")
monkeypatch.setattr(yt.subprocess, "run", run)