From 93321d64925b7a631f94c5c0d8282a243383a5b6 Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:20:54 +0000 Subject: [PATCH] Avoid double-escaping positional filter arguments Remove the duplicate escape_chars pass on positional filter arguments in FilterNode._get_filter so positional and keyword options share single level-1 escaping before filtergraph escaping. Fixes #745 --- ffmpeg/nodes.py | 3 +- ffmpeg/tests/test_ffmpeg.py | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/ffmpeg/nodes.py b/ffmpeg/nodes.py index e8b28385..3a59eb4a 100644 --- a/ffmpeg/nodes.py +++ b/ffmpeg/nodes.py @@ -285,14 +285,13 @@ def _get_filter(self, outgoing_edges): if self.name in ('split', 'asplit'): args = [len(outgoing_edges)] - out_args = [escape_chars(x, '\\\'=:') for x in args] out_kwargs = {} for k, v in list(kwargs.items()): k = escape_chars(k, '\\\'=:') v = escape_chars(v, '\\\'=:') out_kwargs[k] = v - arg_params = [escape_chars(v, '\\\'=:') for v in out_args] + arg_params = [escape_chars(x, '\\\'=:') for x in args] kwarg_params = ['{}={}'.format(k, out_kwargs[k]) for k in sorted(out_kwargs)] params = arg_params + kwarg_params diff --git a/ffmpeg/tests/test_ffmpeg.py b/ffmpeg/tests/test_ffmpeg.py index 8dbc271a..f8532860 100644 --- a/ffmpeg/tests/test_ffmpeg.py +++ b/ffmpeg/tests/test_ffmpeg.py @@ -415,6 +415,79 @@ def _get_drawtext_font_repr(font): assert expected == actual +def test_filter_positional_arg_escape(): + """Test string escaping of positional filter args (e.g. ``subtitles`` path).""" + + def _get_subtitles_arg_repr(value): + args = ( + ffmpeg.input('in') + .filter('subtitles', 'a{}b'.format(value)) + .output('out') + .get_args() + ) + assert args[:3] == ['-i', 'in', '-filter_complex'] + assert args[4:] == ['-map', '[s0]', 'out'] + match = re.match( + r'\[0\]subtitles=a((.|\n)*)b\[s0\]', + args[3], + re.MULTILINE, + ) + assert match is not None, 'Invalid -filter_complex arg: {!r}'.format(args[3]) + return match.group(1) + + expected_backslash_counts = { + 'x': 0, + '\'': 3, + '\\': 3, + '%': 0, + ':': 2, + ',': 1, + '[': 1, + ']': 1, + ';': 1, + '=': 2, + '\n': 0, + } + for ch, expected_backslash_count in list(expected_backslash_counts.items()): + expected = '{}{}'.format('\\' * expected_backslash_count, ch) + actual = _get_subtitles_arg_repr(ch) + assert expected == actual + + +def test_filter_subtitles_drive_path_escape(): + positional_args = ( + ffmpeg.input('C:/Users/test/1_1_1.mp4') + .filter('subtitles', 'C:/Users/test/1_1_1.srt') + .output('C:/Users/test/output.mp4') + .get_args() + ) + assert positional_args == [ + '-i', + 'C:/Users/test/1_1_1.mp4', + '-filter_complex', + '[0]subtitles=C\\\\:/Users/test/1_1_1.srt[s0]', + '-map', + '[s0]', + 'C:/Users/test/output.mp4', + ] + + kwarg_args = ( + ffmpeg.input('C:/Users/test/1_1_1.mp4') + .filter('subtitles', filename='C:/Users/test/1_1_1.srt') + .output('C:/Users/test/output.mp4') + .get_args() + ) + assert kwarg_args == [ + '-i', + 'C:/Users/test/1_1_1.mp4', + '-filter_complex', + '[0]subtitles=filename=C\\\\:/Users/test/1_1_1.srt[s0]', + '-map', + '[s0]', + 'C:/Users/test/output.mp4', + ] + + def test_filter_text_arg_str_escape(): """Test string escaping of normal filter args (e.g. ``text`` param of ``drawtext`` filter).