Skip to content

Instantly share code, notes, and snippets.

@abeluck
Created September 20, 2012 17:50
Show Gist options
  • Save abeluck/3757344 to your computer and use it in GitHub Desktop.
Save abeluck/3757344 to your computer and use it in GitHub Desktop.
Concat MP4 files using AnnexB bitstream format

Concat MP4 files without transcoding to mpeg

  1. Use the h264_mp4toannexb bitstream filter to convert the mp4s (video steam only) into the AnnexB bitstream format

    ffmpeg -i input1.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part1.ts
    # repeat for up to inputN
    
  2. Concat the files using cat

    cat part1.ts part2.ts > parts.ts
    
  3. Remux the files into a complete mp4

    ffmpeg -y -i parts.ts -acodec copy -absf aac_adtstoasc parts.mp4
    
#!/bin/sh
FFMPEG=../ffmpeg-all
$FFMPEG -y -i input1.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part1.ts
$FFMPEG -y -i input2.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part2.ts
cat part1.ts part2.ts > parts.ts
$FFMPEG -y -i parts.ts -acodec copy -absf aac_adtstoasc parts.mp4
#!/bin/sh
FFMPEG=../ffmpeg-all
$FFMPEG -y -i input1.mp4 -f mpeg input1.mpg
$FFMPEG -y -i input2.mp4 -f mpeg input2.mpg
cat input1.mpg input2.mpg > full.mpg
$FFMPEG -y -i full.mpg -b:v 1500k -s 720x480 -vcodec libx264 -acodec aac -ac 2 -b:a 96k -f mp4 -strict experimental output.mp4
@n8fr8
Copy link

n8fr8 commented Sep 20, 2012

with android I had to add a -vcodec copy flag to the final command so it would not try to convert codec "h264" to "libx264"

$FFMPEG -y -i parts.ts -vcodec copy -acodec copy -absf aac_adtstoasc parts.mp4

it works and its very fast!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment