WebM and ffmpeg: concatenate videos and add bumpers

So I have this MP4 video. I want to:

The original video is named girltalk.mp4.

Transcode to WebM with ffmpeg

Easy:

$ ffmpeg -i girltalk.mp4 \
  -f webm -vcodec libvpx \
  -acodec libvorbis \
  -aq 90 -ac 2 \
  girltalk.webm

(You need a version of ffmpeg >= 0.6)

Interesting extra options:

If you want no audio:
-an : Disable audio

If you want just a subsection of the video
-ss  : Set start time offset in seconds or hh:mm:ss[.xxx] format.
-t  : Set recording time in seconds or hh:mm:ss[.xxx] format.

if you want to resize the video
-s  : Set frame size ( WidthxHeight)

if you want to crop the video
-cropbottom  : Set bottom crop band size ( in pixels ).
-cropleft  : Set left crop band size ( in pixels ).
-cropright  : Set right crop band size ( in pixels ).
-croptop  : Set top crop band size ( in pixels ).

Create the bumper

Create one image

First, you need the dimension of the video:
$ ffmpeg -i girltalk.webm 2>&1 | grep "Stream \#0.0"


  Stream #0.0: Video: libvpx, yuv420p, 320x240,\
    PAR 1:1 DAR 4:3, 12 fps, \
    12 tbr, 1k tbn, 12 tbc
Got it: 320x240.

Create the bumper with your prefered image editor (Gimp!). So far, it's just an image of the same size as the video.

Here's mine:

Create a video from the image

You need the framerate of your main video. From the previous command, we know it's 12 fps (the quality of this video is really poor, usually, it's 25). Also, you'll need to know the audio sampling frequency.

$ ffmpeg -i girltalk.webm 2>&1 | grep "Stream \#0.1"



  Stream #0.0: Video: libvpx, yuv420p, 320x240,\
    PAR 1:1 DAR 4:3, 12 fps, \
    12 tbr, 1k tbn, 12 tbc

22050 Hz here. Usually it's 44100 Hz.

You also need to know how you long you want your bumper to last. Let's say 5 seconds:

ffmpeg \
  -loop_input -f image2 -i bumper.jpg\
  -acodec pcm_s16le -f s16le -i /dev/zero \
  -r 12 -t 5 \
  -map 0:0 -map 1:0 \
  -f webm -vcodec libvpx \
  -ar 22050 -acodec libvorbis -aq 90 -ac 2 \
  bumper.webm
So here, I create a video which is just this bumper image repeated 12 times per second during 5 seconds, with a silence track (important). Result:

Concatenate WebM videos

Ok, so now, we have 2 videos. And we want to concatenate them.

WebM is an interesting format. You can just put the 2 videos inside the same file, no need to reencode. The only condition is to have the same bitrate and the same number of tracks. That's why we create a "silence" track and we respected the main video bitrate for the bumper video.

So to "repackage" the video with two videos inside (hmmm), you'll need mkvmerge, from mkvtoolnix:

$ mkvmerge -o girltalk.final.webm bumper.webm +girltalk.webm

If you have any problems with the last command, just check the 2 streams from the 2 videos (ffmpeg -i video.webm), they have to have the exact same properties.

Enjoy the result:

Note: sounds like Chrome WebM decoder doesn't take into account the chained tracks in WebM. Works well in Firefox and Opera.

25 Jan 2011 / tweet this / @paulrouget