08 December, 2013

Useful FFMpeg Filter

FFMpeg offers more than standard encoding/decoding.  It also allows for running a series of filters, both audio and video, that may prove useful in many a situation.

Let's start with an good quality input video;

$ ffmpeg -y -i video.avi -ss 10 -t 40 -qscale 0 -s 360x240 clip01.mpg

Ok, now that we have a video let's look at a few things we may choose to do with it using our newly discovered filters.

Adjusting Video Speed

Suppose you wish to speed the video up, perhaps to expedite review of a surveillance video or perhaps minimizing the exposure to the latest Sandra Bullock film.

We can double the playback speed by specifying a 0.5 playback coefficient;

$ ffmpeg -y -i clip01.mpg -filter:v "setpts=0.5*PTS" clip02.mpg


You'll notice however from the previous video that while the video element is playing at double speed, the audio hasn't changed.  We can speed up the audio similarly by adding an appropriate audio filter.

$ ffmpeg -y -i clip01.mpg -filter:v "setpts=0.5*PTS" -filter:a "atempo=2.0" clip03.mpg


Attempting to speed up faster than 2x will present a hurdle with speeding up the audio filter, which specifies a coefficient range of [0.5, 2.0].  You can get around that by chaining filters;

$ ffmpeg -i clip01.mpg -filter:v "setpts=0.25*PTS" -filter:a "atempo=2.0,atempo=2.0" clip04.mpg


Cropping Video

Suppose we wish to crop the inner middle of the video.  Given the video is 360x240, we need to grab 180x120 inset by 90,60.

$ ffmpeg -y -i clip01.mpg -filter:v "crop=180:120:90:60" clip05.mpg


Processing Edge Detection

Perhaps less useful, but certainly interesting, you can perform edge detection via a Canny Edge Detection algorithm;
$ ffmpeg -y -i clip01.mpg -filter:v "edgedetect=low=0.1:high=0.4" clip06.mpg


Flipping the Video

You can flip the video left-right or top-down;
$ ffmpeg -y -i clip01.mpg -filter:v "hflip" clip07.mpg

$ ffmpeg -y -i clip01.mpg -filter:v "vflip" clip08.mpg


Creating Video Using Source Filter

Occasionally, you want to generate a video using a test source; in other words, without an input video to start with.
$ ffmpeg -f lavfi -i rgbtestsrc -t 30 -pix_fmt yuv420p clip09.mpg

$ ffmpeg -f lavfi -i testsrc=duration=20:size=1280x720:rate=30 -vcodec mpeg2video clip10.mpg