Skip to content

Escape caption subtitle paths for both ffmpeg parsers - #289

Open
codebanditssss wants to merge 1 commit into
AssemblyAI:mainfrom
codebanditssss:fix/caption-filter-paths
Open

codebanditssss wants to merge 1 commit into
AssemblyAI:mainfrom
codebanditssss:fix/caption-filter-paths

Conversation

@codebanditssss

Copy link
Copy Markdown

Fixes #287

Summary

assembly caption puts the temporary SRT path inside -vf subtitles=.... ffmpeg parses that value twice: first as a filtergraph, then as the subtitles filter's option string. The current code escapes only once, so a colon or apostrophe in the temp directory reaches the option parser unescaped, and ffmpeg fails after transcription and SRT export have already finished.

This change escapes the filename from the inside out:

  1. Option level: \, ' and :.
  2. Filtergraph level: \, ', ,, ;, [ and ].

The path is normalized with PurePath.as_posix(). That still turns Windows \ separators into /, but it no longer rewrites a literal backslash inside a POSIX directory name. Plain paths, --font-size (force_style) and the rest of the ffmpeg command are unchanged. -vf is still passed as a single argv item, so there is no shell-quoting layer.

Tests

  • New tests/test_caption_ffmpeg.py runs the real Typer caption command with real ffmpeg and a real generated video. Only client.transcribe and its SRT export are faked. For each temp directory (plain, o'brien, comma,dir, semi;dir, bracket[dir], space dir, and on POSIX also tmp:dir and back\slash), with and without --font-size 28, the test checks three things: the command exits 0, the boundary saw exactly one transcription and one SRT export, and the output's frame hashes differ from the uncaptioned input, which proves the subtitles were actually drawn.
  • test_windows_path_filter_contract feeds a PureWindowsPath (C:\captions\captions.srt) through subtitles_filter and checks that real ffmpeg parses the drive colon and renders the subtitles. On POSIX it uses a relative directory literally named C:. This covers only the parser contract. It is not a native Windows run.
  • tests/test_caption_exec.py: the exact-escape expectation now matches the two-level contract that the real-ffmpeg tests verify. The fake run_ffmpeg boundary undoes both levels to find the temp SRT.

Before the fix, 7 of the 17 new cases fail: colon, apostrophe and literal backslash, each with and without font size, plus the drive-letter contract. The colon cases fail with Invalid argument, the apostrophe cases with No such file or directory. Comma, semicolon, bracket, space and plain cases already passed and stay as controls. After the fix, all of them pass.

Validation

Linux (Fedora 44), Python 3.13.14, ffmpeg 8.1.2:

  • The gate ran sequentially with TERM=xterm-256color and PYTEST_ADDOPTS=--timeout=180.
  • ./scripts/check.sh printed All checks passed.: 3791 tests passed, 99.51% branch coverage, diff-cover 100%. The mutation gate generated 0 mutants because the changed lines contain only string constants.
  • Locally unavailable and self-skipped: shellcheck, actionlint, gitleaks, swiftlint/swift (macOS only) and brew. CI runs these.
  • Before/after repro from the issue: on this branch, temp directories plain, tmp:colon and tmp'quote all exit 0 and write the captioned video.

I have not tested this on native Windows. The Windows CI job will be the first native Windows run of the new real-ffmpeg tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Caption subtitle paths are escaped at the wrong ffmpeg parser level

1 participant