How to merge videos by avconv?

avconv -i concat:file1.mp4\|file2.mp4 -c copy output.mp4

I don't know if works with any container's type ( worked for me with AVI ).


For mp4 the only working solution I found was with MP4Box from gpac package

#!/bin/bash
filesList=""
for file in $(ls *.mp4|sort -n);do
    filesList="$filesList -cat $file"
done
MP4Box $filesList -new merged_files_$(date +%Y%m%d_%H%M%S).mp4

or command is

MP4Box -cat file1.mp4 -cat file2.mp4 -new mergedFile.mp4

with mencoder and avconv I could'nt make it work :-(


mp4 files cannot be simply concatenated, as the "accepted" answer suggests.

If you run that, and that alone, you'll end up with output.mp4 having only the contents of file1.mp4.

That said, what you're looking to do in the original question can in fact be done, as long as you split original file into mpeg streams correctly.

The following commands will split input.mp4 into 3x 60 second segments, in file[1-3].ts:

avconv -ss 0 -i input.mp4 -t 60 -vcodec libx264 -acodec aac \
    -bsf:v h264_mp4toannexb -f mpegts -strict experimental -y file1.ts
avconv -ss 0 -i input.mp4 -t 60 -vcodec libx264 -acodec aac \
    -bsf:v h264_mp4toannexb -f mpegts -strict experimental -y file2.ts
avconv -ss 0 -i input.mp4 -t 60 -vcodec libx264 -acodec aac \
    -bsf:v h264_mp4toannexb -f mpegts -strict experimental -y file3.ts

You can then put them back together much as the other answer suggests:

avconv -i concat:"file1.ts|file2.ts|file3.ts" -c copy \
   -bsf:a aac_adtstoasc -y full.mp4

I used this process to create a scalable, parallel transcoder as described at:

  • http://blog.dustinkirkland.com/2014/07/scalable-parallel-video-transcoding-on.html

Tags:

Video

Avconv