December 2011 1 post

Trimming/clipping, rotating, and extracting videos with FFmpeg

Sunday, December 25, 2011

FFmpeg is an amazing tool, but I find myself searching Google and StackOverflow to do what seem like extremely basic things...

Extract a video clip of length x starting at y seconds

For example, to produce a 5-second video from the 10s mark to the 15s mark of the input video:

$ ffmpeg -i input.avi -ss 10 -t 5 output.avi

Often you can do the above operation (and some others) without re-encoding. This results in output of the highest quality and also is very fast, but doesn't always work:

$ ffmpeg -i input.avi -ss 10 -t 5 -acodec copy -vcodec copy output.avi

Rotate video 90 degrees

There are some pages out there that say to use -vf rotate=90 or -vfilter rotate=90, but that just gives me an error. Here's how:

$ ffmpeg -i input.avi -vf transpose=1 output.avi

The options are:

  • 0: rotate 90 degrees counterclockwise + vertical flip
  • 1: rotate 90 degrees clockwise
  • 2: rotate 90 degrees counterclockwise
  • 3: rotate 90 degrees clockwise + vertical flip

Rotate video 180 degrees

It doesn't look like transpose has an option to rotate 180 degrees, so chain together vflip and hflip:

$ ffmpeg -i input.avi -vf 'vflip [a]; [a] hflip' output.avi

Extract video frames

This creates files named output1.png, output2.png, ...

$ ffmpeg -i input.avi -f image2 -an output%d.png

Converting video to animated GIF

I used this for an iPhone screencast; tweaking some of the options may be beneficial for other types of videos.

$ ffmpeg -i -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif

Tags: ffmpeg, video | Posted at 17:36 | Comments (2)