How to concatenate two MP4 files using FFmpeg?

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

    For .mp4 files, I found it works better and faster to use the opensource command line tool: mp4box. Then You can use it this way:

    mp4box.exe -add video1.mp4 -cat video2.mp4 destvideo.mp4
    

    Download it here for most platforms: https://gpac.wp.imt.fr/mp4box/

    0 讨论(0)
  • 2020-11-22 03:05

    I ended up using mpg as the intermediate format and it worked (NOTE this is a dangerous example, -qscale 0 will re-encode the video...)

    ffmpeg -i 1.mp4 -qscale 0 1.mpg
    ffmpeg -i 2.mp4 -qscale 0 2.mpg
    cat 1.mpg 2.mpg | ffmpeg -f mpeg -i - -qscale 0 -vcodec mpeg4 output.mp4
    
    0 讨论(0)
  • 2020-11-22 03:07

    I was trying to concatenate three .mp3 audio files into one .m4a file and this ffmpeg command works.

    Input command:

    ffmpeg -i input1.mp3 -i input2.mp3 -i input3.mp3 \
           -filter_complex "concat=n=3:v=0:a=1" -f MOV -vn -y input.m4a
    

    Meanings of : -filter_complex "concat=n=3:v=0:a=1" :

    concat means use the media concatenate (joining) function.
    n means confirm total count of input files.
    v means has video? use 0 = no video, 1 = contains video.
    a means has audio? use 0 = no audio, 1 = contain audio.
    -f means force set file format (to see all supported formats, use ffmpeg -formats)
    -vn means disable video (and also -an would disable audio if not wanted)
    -y means overwrite output files (if the output file already exists).

    For more info: use ffmpeg -h full print all options (including all format and codec specific options, very long)

    0 讨论(0)
  • 2020-11-22 03:07

    Here is a script I made to concatenate several GoPro mp4's into a 720p mp4. Hope it's of help.

    #!/bin/sh
    cmd="( "
    for i; do
        cmd="${cmd}ffmpeg -i $i -ab 256000 -vb 10000000 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 100 -f mpeg -; "
    done
    cmd="${cmd} ) | ffmpeg -i - -vb 10000000 -ab 256000 -s 1280x720 -y out-`date +%F-%H%M.%S`.mp4"
    echo "${cmd}"
    eval ${cmd}
    
    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-11-22 03:10

    FOR MP4 FILES

    For .mp4 files (which I obtained from DailyMotion.com: a 50 minute tv episode, downloadable only in three parts, as three .mp4 video files) the following was an effective solution for Windows 7, and does NOT involve re-encoding the files.

    I renamed the files (as file1.mp4, file2.mp4, file3.mp4) such that the parts were in the correct order for viewing the complete tv episode.

    Then I created a simple batch file (concat.bat), with the following contents:

    :: Create File List
    echo file file1.mp4 >  mylist.txt 
    echo file file2.mp4 >> mylist.txt
    echo file file3.mp4 >> mylist.txt
    
    :: Concatenate Files
    ffmpeg -f concat -i mylist.txt -c copy output.mp4
    

    The batch file, and ffmpeg.exe, must both be put in the same folder as the .mp4 files to be joined. Then run the batch file. It will typically take less than ten seconds to run.
    .

    Addendum (2018/10/21) -

    If what you were looking for is a method for specifying all the mp4 files in the current folder without a lot of retyping, try this in your Windows batch file instead (MUST include the option -safe 0):

    :: Create File List
    for %%i in (*.mp4) do echo file '%%i'>> mylist.txt
    
    :: Concatenate Files
    ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
    

    This works on Windows 7, in a batch file. Don't try using it on the command line, because it only works in a batch file!

    0 讨论(0)
提交回复
热议问题