How to concatenate two MP4 files using FFmpeg?

后端 未结 22 3080
遇见更好的自我
遇见更好的自我 2020-11-22 02:32

I\'m trying to concatenate two mp4 files using ffmpeg. I need this to be an automatic process hence why I chose ffmpeg. I\'m converting the two files into .ts files and th

22条回答
  •  心在旅途
    2020-11-22 03:17

    this worked for me (on windows)

    ffmpeg -i "concat:input1|input2" -codec copy output
    

    an example...

    ffmpeg -i "concat:01.mp4|02.mp4" -codec copy output.mp4
    

    Python

    Using some python code to do it with as many mp4 there are in a folder (install python from python.org, copy and paste and save this code into a file called mp4.py and run it from the cmd opened in the folder with python mp4.py and all the mp4 in the folder will be concatenated)

    import glob
    import os
    
    stringa = ""
    for f in glob.glob("*.mp4"):
        stringa += f + "|"
    os.system("ffmpeg -i \"concat:" + stringa + "\" -codec copy output.mp4")
    

    Version 2 with Python

    Taken from my post on my blog, this is how I do it in python:

    import os
    import glob
    
    def concatenate():
        stringa = "ffmpeg -i \"concat:"
        elenco_video = glob.glob("*.mp4")
        elenco_file_temp = []
        for f in elenco_video:
            file = "temp" + str(elenco_video.index(f) + 1) + ".ts"
            os.system("ffmpeg -i " + f + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + file)
            elenco_file_temp.append(file)
        print(elenco_file_temp)
        for f in elenco_file_temp:
            stringa += f
            if elenco_file_temp.index(f) != len(elenco_file_temp)-1:
                stringa += "|"
            else:
                stringa += "\" -c copy  -bsf:a aac_adtstoasc output.mp4"
        print(stringa)
        os.system(stringa)
    
    concatenate()
    

提交回复
热议问题