|Version 12 (modified by nichot20, 5 years ago) (diff)|
FFmpeg Filtering Guide
FFmpeg has access to many filters and more are added on a regular basis. To see what filters are available with your build see ffmpeg -filters. Refer to the libavfilter documentation for more information and examples. This wiki page is for user contributed examples and tips. Contributions to this page are encouraged.
Starting with something simple. Resize a 640x480 input to a 320x240 output.
ffmpeg -i input -vf scale=iw/2:-1 output
iw is input width. In this example the input width is 640. 640/2 = 320. The -1 tells the scale filter to preserve the aspect ratio of the output, so in this example the scale filter will choose a value of 240. See the libavfilter documentation for additional information.
Speed up your video
Increase the speed of a 20 minute video to fit 1 minute output. 1 minute / 20 minutes = 0.05.
ffmpeg -i input -vf setpts=0.05*PTS output
Note that this method will drop frames to achieve your desired speed.
What follows the -vf in an ffmpeg command line is a Filtergraph description. This filtergraph may contain a number of chains, each of which may contain a number of filters.
Whilst a full filtergraph description can be complicated, it is possible to simplify it for simpler graphs provided ambiguity is avoided.
Remembering that filters in a chain are separated by commas "," chains by a semicolon ";" and that if an input or output is not specified it is assumed to come from the preceding or sent to the following item in the chain.
The following are equivalent:-
ffmpeg -i input -vf [in]scale=iw/2:-1[out] output ffmpeg -i input -vf scale=iw/2:-1 output # the input and output are implied without ambiguity
ffmpeg -i input -vf [in]yadif=0:0:0[middle];[middle]scale=iw/2:-1[out] output # 2 chains form, one filter per chain, chains linked by the [middle] pad ffmpeg -i input -vf [in]yadif=0:0:0,scale=iw/2:-1[out] output # 1 chain form, with 2 filters in the chain, linking implied ffmpeg -i input -vf yadif=0:0:0,scale=iw/2:-1 output # the input and output are implied without ambiguity
As described in the documentation, it can be necessary to escape commas "," that need to appear in some arguments, for example the select filter:-
ffmpeg -i input -vf select='eq(pict_type\,I)' output #to select only I frames
However an alternative, which also allows for white space within the filtergraph, and which may assist in clarity of reading complex graphs, is to enclose the whole filtergraph within double quotes " " thus:-
ffmpeg -i input -vf "select=eq(pict_type,I)" output #to select only I frames ffmpeg -i input -vf "yadif=0:-1:0, scale=iw/2:-1" output # deinterlace then resize
Note that the examples given in the documentation mix and match the use of "full quoting" and "\" escaping, and that use of unusual shells may upset escaping.
Burnt in Timecode
PAL 25fps non drop frame
ffmpeg -i in.mp4 -vf "drawtext=fontfile=/usr/share/fonts/truetype/DroidSans.ttf: timecode='09\:57\:00\:00': r=25: \ x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1" -an -y out.mp4
NTSC drop frame
(change the : to a ; before the frame count)_________________________________________________________ \ ffmpeg -i in.mp4 -vf "drawtext=fontfile=/usr/share/fonts/truetype/DroidSans.ttf: timecode='09\:57\:00\;00': r=25: \ x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1" -an -y out.mp4
Scripting your command line parameters
If building complex filtergraphs the command line can get very messy so it can help to break things down into manageable pieces. However one needs to be careful when joining them all together to avoid issues due to your shell and escaped characters.
The following example shows a sample bash script containing a filtergraph of one chain with three filters; yadif, scale and drawtext.
#!/bin/bash # ffmpeg test script path="/path/to/file/" in_file="in.mp4" out_file="out.mp4" cd $path filter="-vf \"yadif=0:-1:0, scale=400:226, drawtext=fontfile=/usr/share/fonts/truetype/DroidSans.ttf: \ text='tod- %X':x=(w-text_w)/2:y=H-60 :fontcolor=white :box=1:boxcolor=0x00000000@1\"" codec="-vcodec libx264 -pix_fmt yuv420p -b:v 700k -r 25 -maxrate 700k -bufsize 5097k" command_line="ffmpeg -i $in_file $filter $codec -an $out_file" echo $command_line eval $command_line exit
Note that the double quotes " around the whole filtergraph have been escaped \" and the filtergraph spans more than one line, the echo command shows the full command as it is executed. Useful for debugging.
The eval invocation of the $command_line variable is required to avoid loss of the embedded escaped quotes which occurs if it is absent. Other shells may behave differently.
List of Filters
Filters bundled with libavfilter as of 2.50.0:
Filters: aconvert Convert the input audio to sample_fmt:channel_layout:packed_fmt. aformat Convert the input audio to one of the specified formats. anull Pass the source unchanged to the output. aresample Resample audio data. ashowinfo Show textual information for each audio frame. earwax Widen the stereo image. pan Remix channels with coefficients (panning) volume Change input volume. abuffer Buffer audio frames, and make them accessible to the filterchain. aevalsrc Generate an audio signal generated by an expression. amovie Read audio from a movie source. anullsrc Null audio source, return empty audio frames. abuffersink Buffer audio frames, and make them available to the end of the filter graph. anullsink Do absolutely nothing with the input audio. blackframe Detect frames that are (almost) black. boxblur Blur the input. copy Copy the input video unchanged to the output. crop Crop the input video to width:height:x:y. cropdetect Auto-detect crop size. delogo Remove logo from input video. deshake Stabilize shaky video. drawbox Draw a colored box on the input video. fade Fade in/out input video fieldorder Set the field order. fifo Buffer input images and send them when they are requested. format Convert the input video to one of the specified pixel formats. gradfun Debands video quickly using gradients. hflip Horizontally flip the input video. hqdn3d Apply a High Quality 3D Denoiser. lut Compute and apply a lookup table to the RGB/YUV input video. lutrgb Compute and apply a lookup table to the RGB input video. lutyuv Compute and apply a lookup table to the YUV input video. mp libmpcodecs wrapper. negate Negate input video. noformat Force libavfilter not to use any of the specified pixel formats for the input to the next filter. null Pass the source unchanged to the output. overlay Overlay a video source on top of the input. pad Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black). pixdesctest Test pixel format definitions. scale Scale the input video to width:height size and/or convert the image format. select Select frames to pass in output. setdar Set the frame display aspect ratio. setpts Set PTS for the output video frame. setsar Set the pixel sample aspect ratio. settb Set timebase for the output link. showinfo Show textual information for each video frame. slicify Pass the images of input video on to next video filter as multiple slices. split Pass on the input to two outputs. transpose Transpose input video. unsharp Sharpen or blur the input video. vflip Flip the input video vertically. yadif Deinterlace the input image buffer Buffer video frames, and make them accessible to the filterchain. color Provide an uniformly colored input, syntax is: [color[:size[:rate]]] mandelbrot Mandelbrot fractal renderer movie Read from a movie source. mptestsrc Generate various test pattern. nullsrc Null video source, return unprocessed video frames. rgbtestsrc Generate RGB test pattern. testsrc Generate test pattern. buffersink Buffer video frames, and make them available to the end of the filter graph. nullsink Do absolutely nothing with the input video.
Other filters created by users:
- https://github.com/indiefan/FFmpeg-Filters an updated vf_fade filter.