Joining multiple video tracks super fast and easy with ffmpeg
Today I wanted to merge two video tracks together for my Tomb Raider 1 Remastered gameplays. I had these two separate videos because I recorded two parts for the same level, since I didn't have the opportunity to complete it in one single session.
I have come across this need several times, but I have never looked into it more deeply before.
After a quick search, I discovered the almighty solution: this operation can be done insanely fast and super easily with ffmpeg concatenate.
Important: this method allows to merge one video after the other, without editing it. If you need to add transitions or do other stuff, that requires video editing softwares like Openshot, Premiere, Vegas, or similar.
Table of contents
Merging videos with exactly same codecs
In most cases you will have videos recorded the same way and just need to have one track showing after the other.
For this, you can follow this simple instructions:
- Create a folder in your PC with all the the tracks you want to merge
- Rename the tracks to something easier, like "part1.mp4", "part2.mp4" and so on
- Create a text file inside that folder and name it "list.txt" or something similar
- Inside the text file, write the path to each file on separate lines like the example code below
- Run the ffmpeg command in your terminal to merge the videos together, see the code snippet below
The text file with the list can be similar to the following:
# list.txt
# See: https://trac.ffmpeg.org/wiki/Concatenate#samecodec
file 'part1.mkv'
file 'part2.mkv'
The first two lines are "comments" and will not be used by ffmpeg, you can remove them if you want. Of course in your case you might have different file types like mp4, wav, mov, or others. This is just an example.
The ffmpeg command to run in your terminal can be adapted to your needs, here is an example:
ffmpeg -f concat -safe 0 -i list.txt -c copy merged_video.mkv
This will create a "merged_video.mkv" file inside your folder. You can use other file types like mp4, mov, wav, and more. Moreover, you could use absolute paths both in the list.txt file and in the ffmpeg command. The "-safe 0" attribute can be omitted if you use relative paths.
If you wish, you can save the above ffmpeg command into a run.bat file and execute it like that, instead of having to copy the command every time in the terminal.
Merging videos with different codecs
This operation is surely more complex and more likely to go wrong, so I invite you to also read the ffmpeg documentation on this topic.
In short, we need to run a command similar to the following one, depending on our needs:
ffmpeg -i input1.mp4 -i input2.webm -i input3.mov \
-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]" output.mkv
In this case we assume to have three different file types as input: mp4, webm and mov. WebM is usually the compressed version of a video file, while the MOV usually comes from Apple devices.
The "filter_complex" parameter tells ffmpeg which streams to take from the input files and send as input to the concat filter.
Finally, the "map" parameters tell ffmpeg to use the results of the concat filter rather than the streams directly from the input.
Conclusions
In my case, I merged a 1-hour long track with another 23-minutes long track. The weight of both files was 4.77GB. The resulting output file had the same length and weight and it took only two minutes to complete the operation.
It was surprisingly easy and fast and it will surely help me a lot in the future. If this was useful to you too, please follow me on Facebook and Twitch, and subscribe to my Youtube channel. Also share a comment below!