My ffmpeg Cookbook

I have been doing more screencasting lately, so I thought I'd share some recipes here, for my own future use and in case anyone else wants to use them. I capture my videos using Kazam on Ubuntu, usually by resizing my second monitor to 800x600 and then capturing that. Kinda eye-bleeding to record but looks good in playback and also works well either in tiny web view or on a big screen. I also screencapture my android device and for that I use Screen Recorder.

All the commands here use ffmpeg, a commandline video manipulation tool. Please note that this is not the ffmpeg command that ships with ubuntu, but the Real Thing (TM) compiled from source. I used instructions at https://trac.ffmpeg.org/wiki/UbuntuCompilationGuide to get it and all its dependencies set up.

Extract A Video Clip

I don't use graphical editing software - I don't use a pointing device and all those tools are really difficult to use keyboard-only! However for just extracting a clip from a video, try this:

ffmpeg -i sample2.mp4 -vf trim=2:67 -an sample2-trimmed.mp4
[pyg]

This takes the <code>sample2.mp4</code> file, and outputs <code>sample-trimmed.mp4</code> which is just the video between 2 seconds and 67 seconds of the original.  Mostly I use this to chop off the visible starting and stopping of recordings, especially on the mobile device.

<h3>Add padding to make a video a certain size</h3>

When screencasting from the phone, I don't end up with 800x600 video which I need to be able to glue them all together into one screen cast.  In fact it's 272x480, so ffmpeg can help me to put black around it to make it up to the right size by using the <code>pad</code> filter.  The arguments are (in order):
<ol><li>width: 800</li>
<li>height: 600</li>
<li>offset X: (800 - 272) / 2 = 264</li>
<li>offset YU: (600 - 480) / 2 = 60</li></ol>

(I did draw a picture to get this sorted out!)  The resulting command:

[pyg language="text"]
ffmpeg -i charles-mb6b.mp4 -vf pad=800:600:160:60:black -an charles-mb6c.mp4

It should also be possible to scale it up, but I didn't figure that out yet, pointers welcome.

Combining Clips

I tend to storyboard what I want to do, then record lots of snippets, creating a new video when I change application so I can get everything cleanly placed and resized, then glue them together later. The glue code goes something like this:

ffmpeg -i mobile1a.mp4 -i mobile2a.mp4 -i mobile3a.mp4 -y -filter_complex '[0:0] setsar=1/1[sarfix];[sarfix] [1:0] concat=n=2:v=1:a=0 [v]' -map '[v]' -an output.mp4

The video inputs go in each with a -i and you will also need to set the n value of the concat filter to say how many videos you are inputting.

Your Turn

Do you have any tips to share or any evolutions on the ones listed here? Leave a comment!

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>