From 16784d055ca5cb403c3bf5db640f2fce472dca9b Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:38:33 -0600 Subject: [PATCH] normalize whitespace in custom export ffmpeg args The custom export validator tokenized args on any whitespace, but the export command was built by splitting on single spaces, so repeated spaces, tabs, or newlines produced a different argv than the one that was validated. The exporter now rejoins custom args with single spaces so ffmpeg receives exactly the tokens the validator checked. --- frigate/record/export.py | 16 ++++++++++++++-- frigate/test/test_export.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/frigate/record/export.py b/frigate/record/export.py index 9bfe64e4ac..1f1cdea64a 100644 --- a/frigate/record/export.py +++ b/frigate/record/export.py @@ -261,6 +261,18 @@ def validate_ffmpeg_args(args: str) -> tuple[bool, str]: return True, "" +def normalize_ffmpeg_args(args: str | None) -> str | None: + """Rejoin args with single spaces. + + validate_ffmpeg_args tokenizes on any whitespace, while export commands are + built with split(" "). Normalizing keeps the two token lists identical. + """ + if args is None: + return None + + return " ".join(args.split()) + + class PlaybackSourceEnum(str, Enum): recordings = "recordings" preview = "preview" @@ -327,8 +339,8 @@ class RecordingExporter(threading.Thread): self.end_time = end_time self.playback_source = playback_source self.export_case_id = export_case_id - self.ffmpeg_input_args = ffmpeg_input_args - self.ffmpeg_output_args = ffmpeg_output_args + self.ffmpeg_input_args = normalize_ffmpeg_args(ffmpeg_input_args) + self.ffmpeg_output_args = normalize_ffmpeg_args(ffmpeg_output_args) self.cpu_fallback = cpu_fallback self.chapters = chapters self.stream = stream diff --git a/frigate/test/test_export.py b/frigate/test/test_export.py index 83ddbccee2..399438f925 100644 --- a/frigate/test/test_export.py +++ b/frigate/test/test_export.py @@ -143,6 +143,37 @@ class TestValidateFfmpegArgs(unittest.TestCase): self.assertRejected("-metadata comment=x") +class TestCustomArgsTokenization(unittest.TestCase): + """The built command must contain exactly the tokens the validator checked.""" + + def test_irregular_whitespace_does_not_shift_tokens(self) -> None: + output_args = "-c:v libx264\t-crf 23\n -r 25 " + self.assertTrue(validate_ffmpeg_args(output_args)[0]) + + with patch("frigate.record.export.Path.mkdir"): + exporter = RecordingExporter( + MagicMock(), + "front_abc123", + "front", + None, + None, + 1_000, + 2_000, + PlaybackSourceEnum.recordings, + ffmpeg_input_args=" -an ", + ffmpeg_output_args=output_args, + ) + exporter.config.ffmpeg.ffmpeg_path = "ffmpeg" + exporter.staged_runs = ["/cache/stage_0.mp4"] + + cmd, _ = exporter.get_record_export_command("/exports/out.mp4") + + for token in cmd[: cmd.index("-metadata")]: + self.assertNotEqual(token, "") + self.assertEqual(token.split(), [token]) + self.assertIn("-c:v libx264 -crf 23 -r 25", " ".join(cmd)) + + class TestExportVideoPath(unittest.TestCase): """Tests for the file path an export takes once the user names it."""