Concatenating mp4 videos (with ffmpeg)

mp4 cannot be concatenated directly. But fmpeg (and so avconv I guess) can by the way of intermediary mpeg transport streams.

Here’s a dirty bash script that does that assuming you want to concat files named like MYFILE-1.mp4 MYFILE-2.mp4. Then you would just execute the script with MYFILE- as argument.

#!/bin/bash
echo $1
[ ! -e "$1"1.*4 ] && echo "nothing for $1""1.mp4" && exit

list=""
for i in `seq 1 20`; do
[ ! -e "$1"$i.*4 ] && continue
echo "$1$i.mp4 exists, creating $1$i.ts"
ffmpeg -i "$1"$i.*4 -c copy -bsf:v h264_mp4toannexb -f mpegts "$1"$i.ts
list="$list|$1"$i".ts"
done

echo "##########################################"
list=`echo "$list" | cut -c 2-`
echo "concat of $list"
ffmpeg -i "concat:$list" -c copy -bsf:a aac_adtstoasc "$1""F.mp4"
rm -f "$1"*.ts

# let user manually remove source files after checks
# EOF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s