Merge multiple video with ffmpeg single command line in specific time without cut it one by one and merge it

6

5

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.

ridwan

Posted 2016-02-14T13:16:44.887

Reputation: 61

Answers

10

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.

Gyan

Posted 2016-02-14T13:16:44.887

Reputation: 21 016

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? – Arete – 2017-03-18T14:43:15.780

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. – Gyan – 2017-03-18T15:33:34.387

Second command is working well but audio is missing! – Jay Patel – 2018-03-20T07:19:11.223

1

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.

Benjamin Rudolph

Posted 2016-02-14T13:16:44.887

Reputation: 11

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...). – FonzTech – 2019-08-28T21:18:50.903