5

I have an array of audio and video clips, where each audio clip has a 1:1 correlation with it's video clip. The encoding of each video and each audio clip are the same. How can I concat all of the audio clips, and all the video clips, then merge them together to output a video. As of now I only figured out how to merge 1 audio clip with 1 video clip:

$ ffmpeg -i video_1.webm -i audio_1.wav -acodec copy -vcodec copy output.mkv

Update I just came across mkvmerge would this possibly be a better option?

Dan Ramos
  • 1,092
  • 2
  • 19
  • 35

2 Answers2

9

If all the files are encoded with the same codecs then it's easy to do. First merge the audio and video files as you have already done so each pair of files is contained in one mkv. Then you can concatenate them with the concat demuxer like this:

ffmpeg -f concat -i <(printf "file '%s'\n" ./file1.mkv ./file2.mkv ./file3.mkv) -c copy merged.mkv

or:

ffmpeg -f concat -i <(printf "file '%s'\n" ./*.mkv) -c copy merged.mkv

You could also list one file per line in a text file called mergelist.txt (or whatever you want to call it), i.e.:

file './file1.mkv'
file './file2.mkv'
file './file3.mkv'

Then use that as the input, a la:

ffmpeg -f concat -i mergelist.txt -c copy merged.mkv

This is by far the easiest and fastest way to do what you want since it won't re-encode the files, just line them up one after another.

Justin Buser
  • 2,813
  • 25
  • 32
4

You can find your answer here in this old question:

Concatenate two mp4 files using ffmpeg

This answer is not restricted to MP4. But it will depend on the file format you wanna concatenate!

Once you have your new VIDEO file and AUDIO file, to merge them together:

ffmpeg -i AUDIO -i VIDEO -acodec copy -vcodec copy OUTPUT

Community
  • 1
  • 1
Wagner Patriota
  • 5,494
  • 26
  • 49
  • Ill give that a go to concat the video files, but then how do i go about taking the concatenated video files, and merge/add them to the concatenated audio files? – Dan Ramos Sep 10 '13 at 05:52
  • Excellent, works great! Last question, are the audio and video files being compressed twice, once during concat, once during merging? Or, does ffmpeg simply combine the files w/ no compression? – Dan Ramos Sep 10 '13 at 06:01
  • depending on the way the you concatenate them, them WILL HAVE to be reencoded... the last step I gave to you will not need to reencode. The parameters "-acodec copy" and "-vcodec copy" guarantee just a transmux, without loosing any quality. – Wagner Patriota Sep 10 '13 at 06:05
  • I'm sorry, but how is repeating the command line from the question an answer to the question? He wants to concatenate multiple files, which means one after the other, not next to each other. This will certainly combine audio A and video B into MKV C, but won't help him concatenate MKV A MKV B and MKV C. – Justin Buser Feb 20 '14 at 02:38
  • read it over. initialy he HAS audio and video separated. I explained that the command he already has is just to join audio+video. How to concatenate videos and how to concatenate audios is his actual question. I gave him a link for this... once concatenated, he can use the command HE ALREADY KNOWS to join audio+video. – Wagner Patriota Feb 20 '14 at 14:31