Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions ffmpeg/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
73 changes: 73 additions & 0 deletions ffmpeg/tests/test_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down