I have 15 video files which I want to merge with ffmpeg. I want to save time by merge all of the video files in a single command line, not cut it one by one and merge it. Thanks.
2 Answers
Use the concat demuxer.
First, create a text file with the filenames.
file '1.mp4'
file '2.mp4'
file '3.mp4'
...
file '13.mp4'
file '14.mp4'
file '15.mp4'
Then, run the concat command.
ffmpeg -f concat -i textfile -c copy -fflags +genpts merged.mp4
For this to work, all videos should have same properties such as codec, resolution, framerate, sample rate, etc.
If they are not, you can encode the concat.
ffmpeg -f concat -i textfile -fflags +genpts merged.mp4
Also see FFmpeg Wiki: Concatenate.
-
Please explain how to "encode the concat". The command line you have provided for this does not work for me. Could you perhaps provide an example?– AreteMar 18, 2017 at 14:43
-
1@Arete Post a new Q with log of your ffmpeg command execution as well as properties of the input files + the list as well.– GyanMar 18, 2017 at 15:33
-
Gyan's suggestion did not work for me. The output video from the suggested commands had the same duration as the first input, and so video 2 and 3 content was missing, and my media player crashed trying to play the output.
I did however find my answer in the reference he provided: http://trac.ffmpeg.org/wiki/Concatenate#filter
ffmpeg -i v1.mp4 -i v2.mp4 -i v3.mp4 -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" v_out.mp4
This command worked for videos all having both video and audio streams. For one video not having audio, I modify the command slightly like so:
ffmpeg -i v1_NoAudio.mp4 -i v2.mp4 -i v3.mp4 -filter_complex "[0:v:0][1:v:0][2:v:0]concat=n=3:v=1[outv]" -map "[outv]" v_out_NoAudio.mp4
Effectively, I just removed any portions of the command about audio. This discards the audio from the second and third videos, as a rule of using this filter is that the channels remain the same in all videos.
-
This worked for me, because input files differed in metadata (although I could not tell what was wrong, since they had apparently the same resolution, codec, etc...).– FonzTechAug 28, 2019 at 21:18