Skip to content

Ffmpeg: concatenating preroll and postroll without reencoding

Mark Van den Borre edited this page Nov 26, 2016 · 4 revisions

While concatenating with (cpu intensive) reencoding is trivial, concatenating wihout reencoding is quite picky it seems.

Create preroll and postroll video

Let's create a static preroll and postroll video from an image to test. (Note that Niels made some nice ways to draw on this video programmatically if needed. See git.fosdem.org video repo 2015 artwork.)

ffmpeg -loop 1 -i preroll.jpg -framerate 25 -c:v libx264 -t 5 -pix_fmt yuv420p /tmp/preroll_noaudio.mp4
ffmpeg -loop 1 -i postroll.jpg -framerate 25 -c:v libx264 -t 5 -pix_fmt yuv420p /tmp/postroll_noaudio.mp4

Add an audio channel to the video only file

We also need a silent audio track to put under this so we can properly -c copy concatenate the files:

ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i preroll.mp4 -shortest -c:v copy -c:a aac preroll.mp4
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -i postroll.mp4 -shortest -c:v copy -c:a aac postroll.mp4

We now have a h264 video stream and a silent aac audio stream.

Concatenate the files, attempt #1

Let's create a job file that lists files to be concatenated:

ffconcat version 1.0
file 'prerollsilentaudio.mp4'
duration 5
file '720p.mp4'
file 'postrollsilentaudio.mp4'
duration 5

Now let's run the concatenation job:

ffmpeg -f concat -i files.txt -c copy output.mp4

Close, but no cigar. This produces a garbled file, if it doesn't barf out with complaints.

Attempt 2: go through mpeg ts format

Starting from the mp4 files:

ffmpeg -i video.mp4 -c copy video.ts
ffmpeg -i prerollsilentaudio.mp4 -c copy prerollsilentaudio.ts
ffmpeg -i postrollsilentaudio.mp4 -c copy postrollsilentaudio.ts
ffmpeg -f mpegts -i "concat:prerollsilentaudio.ts|video.ts|postrollsilentaudio.ts" -c copy -bsf:a aac_adtstoasc output.mp4

...or starting from the image file:

ffmpeg -loop 1 -i img.png -c:v libx264 -r 25 -frames:v 25 pre.ts
ffmpeg -i video.mp4 -bsf:v h264_mp4toannexb -c copy video.ts
for m in *.ts; do echo file "$m" >> concat.txt; done
ffmpeg -f concat -i concat.txt -c copy out.ts

On a somewhat recent ssd, a one hour presentation can be processed in under 15 seconds, with neglegible cpu time. It does not require setting up an empty audio stream for the image either. Only disadvantage is the need for temporary files, but as this is a short lived process, that should only be a minor problem.

Debugging and troubleshooting

You probably want to have a closer look at ffprobe and mediainfo, two interesting a/v media file diagnostic tools. The encoding profiles are not close enough together. A diff of the output of these, and a closer look at the specific codec settings used to encode them might help.

Great help from furq on irc.freenode.net#ffmpeg , as alway. This time, he suggested using ffprobe -show_streams <filename.mp4>.

Reading materials

https://trac.ffmpeg.org/wiki/Create%20a%20video%20slideshow%20from%20images#Usingasingleimageasaninput http://stackoverflow.com/questions/12368151/adding-silent-audio-to-mov-in-ffmpeg https://trac.ffmpeg.org/wiki/Concatenate

Clone this wiki locally