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."""