How to join video files from terminal?

I have tried avidemux2_cli, mencoder, ffmpeg, cat.. But this doesn't always work (With the most of the times the error is that the audio codec is not the same) Maybe i put wrong options in the commands.

So the commands:

cat Sample.avi rrr.avi > complete.avi
ffmpeg -i Sample.avi -i output.avi -vcodec copy -acodec copy complete.avi
mencoder -ovc lavc -oac copy Sample.avi rrr.avi -o complete.avi
avidemux2_cli --audio-codec copy --video-codec copy --output-format avi --load Sample.avi -append output.avi --save video.avi

The cat problem is that it doesn't show error but it doesn't work always..Like the complete.avi will be exactly the same with Sample.avi

Fmmpeg does nothing. The complete.avi is always the same with Sample.avi

Mencoder error: All files must have identical audio codec and format for -oac copy. So the complete.avi is the same with Sample.avi

avidemux2_cli there is no error but the complete.avi is again the same with Sample.avi..

So to sum up, all complete.avi are the same with Sample.avi.. And the problem is that they don't have the same audio codec ( i quess ).. Any ideas?


If you want to combine two videos safely, you need to change the video codec and audio codec of input files into the same codecs.

For example, using xvid (video codec) and mp3lame (audio codec):

sudo apt-get install mencoder libxvidcore4 libmp3lame0

you can encode the input videos by the below commands:

mencoder -ovc xvid -oac mp3lame -xvidencopts bitrate=1000 first.avi -o video1.avi
mencoder -ovc xvid -oac mp3lame -xvidencopts bitrate=1000 second.avi -o video2.avi

To be matched your videos, you can change the options "bitrate" etc. (and also the codecs).

If the encoded video names are "video1.avi" and "video2.avi", and the output video name is "joined-video.avi",

mencoder -ovc copy -oac mp3lame video1.avi video2.avi -o joined-video.avi 

Using this command, you can make the joined video file.


I had to join three pieces of video with identical codecs. Reading the previous answers I managed to do this:

mencoder -ovc copy -oac faac 01.mp4 02.mp4 03.mp4 -o output.mp4

Much faster and simpler than Openshot!

First I tried the '-oac copy' option, but it seems that the audio codec in my videos doesn't allow it. I got an error message recommending '-oac pcm'. The pcm codec is not a good option though: the resulting file is very big and 2/3 of it is uncompressed audio.

Hope this helps


A solution that I found here helped me to join without loss two mp4 files:

sudo apt-get install gpac
MP4Box -cat part1.mp4 -cat part2.mp4 -new joinedfile.mp4

I tried using Avidemux, but had problems with sound in the merged video.


Your files won't join properly because something doesn't match up.

Like many others I had problems with sound syncing up properly in merged videos. and while MP4Box works fine if the clips have identical specifications, in some cases I would get whacked by the “No suitable destination track found - creating new one" in which case Totem (a.k.a Videos) Wouldn't play that segment in the joined file. After some trial and error and further research I've developed the following method for dealing with joining videos which thus far has worked every time with acceptable (to me) quality.

If frame sizes don't match (joining errors pertaining to video tracks):

Normalize video without disrupting audio (-c:a copy) by setting constraints (divisible by 8 for maximum codec compatibility) for unmatched segments

Example commands

width=856

height=480

avconv -i input.mp4 -filter:v "scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2" -c:a copy output.mp4

If Audio Doesn't match (joining errors pertaining to audio tracks):

Normalize Audio

Process audio without disrupting video (c:v copy) so that all clips match the desired codec, frequency and bitrate with c:a codec -ar frequency like -ar 48000 and and bit-rate with b:a 128k for stereo (64K per channel seems a good target) or whatever matches.

Example: avconv -i input.mp4 -c:v copy -c:a libmp3lame -ar 48000 b:a 128k output.mp4 (note that libmp3lame is a codec for mp3 audio) You can get a list of avconv supported encoders by issuing the command avconv -codecs or to narrow things down to a specific coded avconv -codecs|grep *codecname* for example avconv -codecs|grep mp3

Now that we've normalized the files, we can join them. In this example I'll join the files with MP4Box. If you don't have it you can get it by issuing the command sudo apt-get install gpac

Join the files with the command MP4Box file1.mp4 -cat file2.mp4 -cat file3.mp4 -cat file4.mp4 -out joinedfile.mp4 (note that there is no cat before the first file and each additional file is preceded by -cat and the target file is preceded by -out)

If using MP4Box (recommended for MP4) for joining you may find that you have to process all clips with avconv whether specs appear to match or not to avoid the error “No suitable destination track found - creating new one (type vide) ”

Note: I've found Avidemux to have problems with videos encoded using H.264/AVC (per totem) files due to the B-frames as a reference. This seems to result in audio out of sync when joining even when choosing to copy both audio and video without remux. Crashes when not choosing safe mode and out of sync when using safe mode

Sources:

https://superuser.com/questions/547296/resizing-videos-with-ffmpeg-avconv-to-fit-into-static-sized-player

https://stackoverflow.com/questions/20703160/problems-with-setting-constant-bitrate-by-using-avconv

https://trac.ffmpeg.org/wiki/Encode/AAC#fdk_cbr

Note: As far as I know other formats (containers) have similar constraints for matching frame size and audio rates, so this may work for avi as well if you attempt the joining via cat or mencoder after normalizing video and audio.

Optional: Use Handbrake to re-encode the joined file for smaller size if desired with a variable framerate and a constant quality setting of 25 (suggested settings – adjust as needed for your material and quality expectations)


You should type like this:

avidemux2_cli --audio-codec copy --video-codec copy --output-format avi --load Sample.avi --append output.avi --save video.avi

If you want to append more files:

avidemux2_cli --audio-codec copy --video-codec copy --output-format avi --load Sample.avi --append output1.avi --append output2.avi --append output3.avi --save video.avi