Skip to content

Instantly share code, notes, and snippets.

Last active Dec 23, 2017
What would you like to do?
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
We will generate a line like the following (copied from and slightly edited):
ffmpeg -i input1.mp4 -i input2.webm -i \
-filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mkv
import sys
import shlex
def generate_inputs(input_files):
Generate list of inputs to ffmpeg, with the -i option before each file name.
inputs = []
for input_file in input_files:
inputs.extend(['-i', input_file])
return inputs
def generate_filter(num_files):
Generate list of mappings for the -filter_complex option of ffmpeg.
>>> generate_filter(3)
['-filter_complex', '[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]']
cmd_filter = ['-filter_complex']
filter_string_parts = [] # this will be joined to become a single string
for i in range(num_files):
filter_string_parts.append('[%d:v:0][%d:a:0]' % (i, i))
filter_string_parts.append('concat=n=%d:v=1:a=1[outv][outa]' % (num_files))
filter_string = ''.join(filter_string_parts)
return cmd_filter
def generate_cmdline(args):
>>> l = ['input1.mp4', 'input2.webm', '']
>>> generate_cmdline(l)
"ffmpeg -i input1.mp4 -i input2.webm -i -filter_complex '[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]' -map '[outv]' -map '[outa]' output.mkv"
cmd_part0 = ['ffmpeg']
cmd_inputs = generate_inputs(args)
cmd_filter = generate_filter(len(args))
cmd_trailing = ['-map', '[outv]', '-map', '[outa]', 'output.mkv']
cmd_final = [shlex.quote(s) for s in
cmd_part0 + cmd_inputs + cmd_filter + cmd_trailing]
return ' '.join(cmd_final)
if __name__ == '__main__':
cmd_line = generate_cmdline(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment