How to concatenate two MP4 files using FFmpeg?

后端 未结 22 3406
遇见更好的自我
遇见更好的自我 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:10

    FFmpeg has three concatenation methods:

    1. concat video filter

    Use this method if your inputs do not have the same parameters (width, height, etc), or are not the same formats/codecs, or if you want to perform any filtering. (You could re-encode just the inputs that don't match so they share the same codec and other parameters, then use the concat demuxer to avoid re-encoding the other inputs).

    ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \
           -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] 
                            concat=n=3:v=1:a=1 [v] [a]" \
           -map "[v]" -map "[a]" output.mkv
    

    Note that this method performs a re-encode.

    2. concat demuxer

    Use this method when you want to avoid a re-encode and your format does not support file level concatenation (most files used by general users do not support file level concatenation).

    $ cat mylist.txt
    file '/path/to/file1'
    file '/path/to/file2'
    file '/path/to/file3'
    
    $ ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
    

    For Windows:

    (echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
    ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4
    

    3. concat protocol

    Use this method with formats that support file level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4.

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

    This method does not work for many formats, including MP4, due to the nature of these formats and the simplistic concatenation performed by this method.


    If in doubt about which method to use, try the concat demuxer.

    Also see

    • FFmpeg FAQ: How can I join video files?
    • FFmpeg Wiki: How to concatenate (join, merge) media files

提交回复
热议问题