Skip to content

Instantly share code, notes, and snippets.

@NazmusShakib
Created June 12, 2019 18:55
Show Gist options
  • Save NazmusShakib/276642449d04f0da43ad419c7aa07c25 to your computer and use it in GitHub Desktop.
Save NazmusShakib/276642449d04f0da43ad419c7aa07c25 to your computer and use it in GitHub Desktop.
ffmpeg: merge multiple MP4 files, it's necessary to pass by .ts files
import os
import subprocess
import glob
def main():
tsFileList = []
# consider only .mp4 files
files = glob.glob('*.mp4')
for file in files:
# file transcode to MPEG-2 TS
subprocess.call(['ffmpeg', '-i', file, '-c', 'copy', '-bsf:v', 'h264_mp4toannexb', '-f', 'mpegts', file+'.ts'])
tsFileList.append(file+'.ts')
process(tsFileList)
def process(tsFileList):
input = ''
for eachfile in tsFileList:
input += eachfile + '|'
# concatenation
subprocess.call(['ffmpeg', '-i', 'concat:' + input, '-c', 'copy', '-bsf:a', 'aac_adtstoasc', 'output.mp4'])
# remove all .ts files
for eachfile in tsFileList:
os.remove(eachfile)
print("done!")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment