mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-23 22:07:07 +03:00
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.
This commit is contained in:
@@ -261,6 +261,18 @@ def validate_ffmpeg_args(args: str) -> tuple[bool, str]:
|
|||||||
return True, ""
|
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):
|
class PlaybackSourceEnum(str, Enum):
|
||||||
recordings = "recordings"
|
recordings = "recordings"
|
||||||
preview = "preview"
|
preview = "preview"
|
||||||
@@ -327,8 +339,8 @@ class RecordingExporter(threading.Thread):
|
|||||||
self.end_time = end_time
|
self.end_time = end_time
|
||||||
self.playback_source = playback_source
|
self.playback_source = playback_source
|
||||||
self.export_case_id = export_case_id
|
self.export_case_id = export_case_id
|
||||||
self.ffmpeg_input_args = ffmpeg_input_args
|
self.ffmpeg_input_args = normalize_ffmpeg_args(ffmpeg_input_args)
|
||||||
self.ffmpeg_output_args = ffmpeg_output_args
|
self.ffmpeg_output_args = normalize_ffmpeg_args(ffmpeg_output_args)
|
||||||
self.cpu_fallback = cpu_fallback
|
self.cpu_fallback = cpu_fallback
|
||||||
self.chapters = chapters
|
self.chapters = chapters
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
|
|||||||
@@ -143,6 +143,37 @@ class TestValidateFfmpegArgs(unittest.TestCase):
|
|||||||
self.assertRejected("-metadata comment=x")
|
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):
|
class TestExportVideoPath(unittest.TestCase):
|
||||||
"""Tests for the file path an export takes once the user names it."""
|
"""Tests for the file path an export takes once the user names it."""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user