Merge video files
Quick notes about how to merge (as in append) multiple video files together with mkvmerge.
Video files can be complex. For example, they might or might not have an associated audio track. Other videos can have more than one.
Just like audio tracks, they can have zero, one, or more subtitles. And there are forced subtitles too.
Like audio tracks, there can be subtitles in different languages, but also multiple for the same language, and they might not be labeled at all.
Some videos even have more than one video track!
And there are of course different formats (for audio, video, and subtitles), video dimensions, video qualities, video framerates, audio sample rates, …
Long story short, merging video files is generally messy, and mkvmerge
can probably handle most if not all scenarios.
For the most simple setup; multiple video files, each with one video and one audio track (no subtitles), the command for appending one track after the other is relatively simple
mkvmerge --output <output> '[' <input1> <input2> ... <input_n> ']'
In case of errors, I would recommend using the --verbose
flag, as it might show useful information for resolving the issue,
The most two common error cases I’ve encountered are different formats and different sample rates.
Error; The formats do not match
If the input files do have different formats, when using --verbose
, the output might look similar to
'a.webm' track 0: Using the output module for the format 'VP8/VP9'.
'a.webm' track 1: Using the output module for the format 'Opus'.
'b.mkv' track 0: Using the output module for the format 'VP8/VP9'.
'b.mkv' track 1: Using the output module for the format 'AAC'.
'c.mkv' track 0: Using the output module for the format 'VP8/VP9'.
'c.mkv' track 1: Using the output module for the format 'AAC'.
'd.mkv' track 0: Using the output module for the format 'VP8/VP9'.
'd.mkv' track 1: Using the output module for the format 'AAC'.
[...]
Error: The track number 1 from the file 'a.mkv' cannot be appended to the track number 1 from the file 'b.webm'. The formats do not match.
In this case, it is necessary to convert all video files to a common format before it is possible to append them together with mkvmerge
.
Fortunately, a .webm
file can be converted to a .mkv
file without re-encoding. This means that it is possible to convert one format to another without altering the video and audio quality.
The conversion can be done with FFmpeg:
ffmpeg -i "input.webm" -c:v copy -c:a aac "output.mkv"
Error; The sample rate of the two audio tracks is different
Even if the format is the same, it might not be enough to append one video file to another:
Error: The track number 1 from the file 'a.mkv' cannot be appended to the track number 1 from the file 'b.mkv'. The sample rate of the two audio tracks is different: 44100 and 48000
The operation cannot be "forced", if it were possible, then the playback speed would be wrong for the second part of the video.
Fortunately, this issue can be fixed directly with FFmpeg. It supports the parameter -ar
for setting the audio sampling rate in Hz.
ffmpeg -i "input" -c:v copy -c:a aac -ar 44100 "output.mkv"
Do you want to share your opinion? Or is there an error, some parts that are not clear enough?
You can contact me anytime.