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 800×600 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 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

This takes the sample2.mp4 file, and outputs sample-trimmed.mp4 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.

Add padding to make a video a certain size

When screencasting from the phone, I don’t end up with 800×600 video which I need to be able to glue them all together into one screen cast. In fact it’s 272×480, so ffmpeg can help me to put black around it to make it up to the right size by using the pad filter. The arguments are (in order):

  1. width: 800
  2. height: 600
  3. offset X: (800 – 272) / 2 = 264
  4. offset YU: (600 – 480) / 2 = 60

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

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!

One thought on “My ffmpeg Cookbook

  1. How to crop a video that ended up slightly larger than planned:

    ffmpeg -i b3b-2.mp4 -vf crop=900:596:2:0 -an b3b-2-intermediate.mp4

    How to scale up a video to a specific constrainted size (in this case, height)

    ffmpeg -i c4d-1.mp4 -vf scale=h=’min(600\, ih*3/2):w=-1′ -an c4d-2.mp4

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.