def cut_and_stitch(input_video, time_list, output_video):
"""
剪切掉所有time_list里的片段,将其余片段拼接起来
"""
all_cut = []
filter_cmd = []
for i, (start, end) in enumerate(time_list):
filter_cmd.append(
'[0:v] trim=start={}:end={},setpts=PTS-STARTPTS,format=yuv420p [{}v];'.format(start, end, i))
filter_cmd.append('[0:a] atrim=start={}:end={},asetpts=PTS-STARTPTS [{}a];'.format(start, end, i))
all_cut.append('[{}v][{}a]'.format(i, i))
cmd = "ffmpeg -i {} -filter_complex '{} concat=n={}:v=1:a=1[outv][outa]' -map [outv] -map [outa] -q:v 5 {}".format(
input_video, ' '.join(filter_cmd) + ''.join(all_cut), len(all_cut), output_video)
print('cmd:', cmd)
os.system(cmd)
网友评论