How to merge multiple (more than two) videos on Ubuntu?
Solution 1
This solved the matter: https://stackoverflow.com/a/22324018/5014767
melt
is a great command line utility for this.
Here is the page
Edit from comments: The command which solved my problem was this
melt {input-sequence} -consumer avformat:{output-name} acodec=libmp3lame vcodec=libx264
Solution 2
I am using mkvmerge
to join multiple MP4 files into single one:
mkvmerge -o outfile.mkv infile_01.mp4 \+ infile_02.mp4 \+ infile_03.mp4
Solution 3
You can do it using ffmpeg
:
ffmpeg -i concat:"input1.mp4|input2.mp4" output.mp4
Solution 4
Create a file files.txt with all the files you want to have concatenated in the following form (lines starting with a # are ignored):
# this is a comment
file 'file1.mp4'
file '/path/to/file2.mp4'
file 'file3.mp4'
Note that these can be either relative or absolute paths. Then you can stream copy or re-encode your files:
ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp4
Solution 5
I wrote a little shell script to concat MP4s without transcoding using ffmpeg.
for f in $(ls *.MP4); do
ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
done
CONCAT=$(echo $(ls *.ts) | sed -e "s/ /|/g")
ffmpeg -i "concat:$CONCAT" -c copy -bsf:a aac_adtstoasc output.mp4
rm *.ts
This creates intermediate files in an MPEG container and then concatenates them into an MP4.
Related videos on Youtube
Dhruv Singal
Updated on September 18, 2022Comments
-
Dhruv Singal over 1 year
I want to merge videos in batch size of twenty (20) each. I'm running a Linux machine. The videos are in mp4 format and moderate quality. Some even have the audio stream missing. So far I've tried
ffmpeg
,mencoder
,cvlc/vlc
andMP4Box
. I want to write a command line script to achieve this, since I'm doing batch processing.The main issue is that some of the solutions I tried work well for two videos, some work well for videos with audio stream and yet others work well for some other subset of my video set. However, I have not been able to find a comprehensive solution for this task.
-
Maythux almost 9 yearsPlease don't ask same question in multi SE sites: superuser.com/questions/928548/…
-
ohaal almost 9 yearsCould you post specifically what you've tried so far?
-
Dhruv Singal almost 9 years@ohaal I tried using various concatenation methods given in
ffmpeg
, including filters and file protocols. I also tried using theconcat
feature ofcvlc/vlc
. I also tried theconcat
feature ofmencoder
. All of these worked partially. This finally helped me solve the matter. -
ohaal almost 9 yearsYes, you already mentioned the tools, what I meant was the actual commands you ran. Also it would be great if you could include the command that ended up solving it for you (in your answer), for future visitors.
-
Ciro Santilli OurBigBook.com about 6 yearsrelated ffmpeg subset: stackoverflow.com/questions/7333232/…
-
-
Dhruv Singal almost 9 yearsI tried doing that. The issue with this is that it doesn't give a proper out. 1. The audio tracks are muddled. 2. There is some disturbance in the video track.
-
Dhruv Singal almost 9 yearsI tried this but it only returns the first video as the output. I read somewhere that it doesn't work, though I don't remember the rationale behind this.
-
kos almost 9 years@DhruvSingal Yes, I can confirm this, I've tried this on two MP4 videos with the exact same video / audio encoding and as you I could only browse through the first video. Which seems reasonable, however I'm pretty sure that I've done this before once with a couple of MP4 videos. Perhaps the memory isn't serving well here, or there are other requirements for this to work. I want to research on this since I'm interested also, if I find something I'll update the answer and drop you a comment
-
Maythux almost 9 yearsI used it for more than sample, the result is high quality, maybe your source video quality is the problem?
-
Dhruv Singal almost 9 yearsYou are probably right. This solved the issue: askubuntu.com/a/637179/420614
-
Ken Mollerup over 8 yearsWe somehow need to change the metadata at the start of the first video to encompass the entire length of the combined movie, and other stuff probably. Only a few mpeg4 files let me see the metadata..
-
haridsv over 6 yearsIt is better to use the extension
.mkv
for the output file, as it is a Matroska container irrespective of what extension you give. -
llogan over 6 yearsDon't use the concat protocol to concatenate MP4. It only works with certain formats that most general users don't encounter. Use concat demuxer or concat filter instead.
-
Morgoth over 6 yearsHere's how to install
mkvmerge
: tipsonubuntu.com -
Paddy Landau almost 6 yearsIt's important to note that this script will fail if any of the filenames contain spaces or, in some cases, special characters. For the loop, it's significantly better to use just
for f in *.MP4
without usingls
. (You should always avoid usingls
in scripts for a couple of important reasons; there are better options.) Also, use quotes for variables unless you are certain that there are no spaces or other special characters, so"${f}"
(or just"$f"
) instead of unquoted$f
. -
August almost 6 yearsI tried ffmpeg with 2 mp4 files of 30min each. I got an output file with length 7 hours.
-
tisaconundrum over 5 years
CONCAT=$(echo $(ls *.ts) | sed -e "s/ /|/g")
doesn't work well with file names that are 1, 2, 3... 10... 20. It ends up parsing them as 1, 10... 2, 20... etc. I suggestCONCAT=$(echo $(ls -v *.ts) | sed -e "s/ /|/g")
this will ensure order. -
SemperPeritus about 5 yearsUse with formats that support file level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4. Source: stackoverflow.com/questions/7333232/…
-
42n4 over 4 yearsOnly this command mkvmerge works. All ffmpeg ones fail with shifted audio at least.
-
vy32 about 4 yearsThis can't work.
-
bomben about 3 yearsTo install
mkvmerge
dosudo apt install mkvtoolnix
-
Cameron about 3 yearsWorks like a charm, built in Rust! I opted to just
cargo build --release
directly from the repo as I needed to use the-safe 0
option onffmpeg
-
Varun Chandak about 3 years
-safe 0
worked for me -
Sanket Patel about 3 yearsFor those having issues merging vids with different widths.. this solution works fine...
-
Antonello over 2 yearsIs it possible to specify in the same files multiple outputs, eg file 'fileA1. mp4' 'A.mp4', 'fileA2. mp4' 'A.mp4', file 'B1.mp4' 'B.mp4', etc ?
-
Chetan over 2 yearsLife saver alert!
-
tatsu about 2 yearsthis ended up working for me but I had to make both the inputs and the output .mkv for it to function. .mp4 would bork the audio.
-
EsmaeelE about 2 yearsThis is simple and fast way to merge two
.mkv
files.