|Version 17 (modified by llogan, 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 30fps 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=30: \ 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.63.100 (as configured with --enable-gpl). Filters relying on external libraries, such as frei0r, are not listed here.
aconvert A->A Convert the input audio to sample_fmt:channel_layout:packed_fmt. aformat A->A Convert the input audio to one of the specified formats. amerge AA->A Merge two audio streams into a single multi-channel stream. anull A->A Pass the source unchanged to the output. aresample A->A Resample audio data. ashowinfo A->A Show textual information for each audio frame. asplit A->AA Pass on the audio input to two outputs. astreamsync AA->AA Copy two streams of audio data in a configurable order. earwax A->A Widen the stereo image. pan A->A Remix channels with coefficients (panning). silencedetect A->A Detect silence. volume A->A Change input volume. abuffer |->A Buffer audio frames, and make them accessible to the filterchain. aevalsrc |->A Generate an audio signal generated by an expression. amovie |->A Read audio from a movie source. anullsrc |->A Null audio source, return empty audio frames. abuffersink A->| Buffer audio frames, and make them available to the end of the filter graph. anullsink A->| Do absolutely nothing with the input audio. blackdetect V->V Detect video intervals that are (almost) black. blackframe V->V Detect frames that are (almost) black. boxblur V->V Blur the input. copy V->V Copy the input video unchanged to the output. crop V->V Crop the input video to width:height:x:y. cropdetect V->V Auto-detect crop size. delogo V->V Remove logo from input video. deshake V->V Stabilize shaky video. drawbox V->V Draw a colored box on the input video. fade V->V Fade in/out input video. fieldorder V->V Set the field order. fifo V->V Buffer input images and send them when they are requested. format V->V Convert the input video to one of the specified pixel formats. gradfun V->V Debands video quickly using gradients. hflip V->V Horizontally flip the input video. hqdn3d V->V Apply a High Quality 3D Denoiser. lut V->V Compute and apply a lookup table to the RGB/YUV input video. lutrgb V->V Compute and apply a lookup table to the RGB input video. lutyuv V->V Compute and apply a lookup table to the YUV input video. mp V->V Apply a libmpcodecs filter to the input video. negate V->V Negate input video. noformat V->V Force libavfilter not to use any of the specified pixel formats for the input to the next filter. null V->V Pass the source unchanged to the output. overlay VV->V Overlay a video source on top of the input. pad V->V Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black). pixdesctest V->V Test pixel format definitions. scale V->V Scale the input video to width:height size and/or convert the image format. select V->V Select frames to pass in output. setdar V->V Set the frame display aspect ratio. setfield V->V Force field for the output video frame. setpts V->V Set PTS for the output video frame. setsar V->V Set the pixel sample aspect ratio. settb V->V Set timebase for the output link. showinfo V->V Show textual information for each video frame. slicify V->V Pass the images of input video on to next video filter as multiple slices. split V->VV Pass on the input to two outputs. thumbnail V->V Select the most representative frame in a given sequence of consecutive frames. tinterlace V->V Perform temporal field interlacing. transpose V->V Transpose input video. unsharp V->V Sharpen or blur the input video. vflip V->V Flip the input video vertically. yadif V->V Deinterlace the input image. cellauto |->V Create pattern generated by an elementary cellular automaton. color |->V Provide an uniformly colored input, syntax is: [color[:size[:rate]]]. frei0r_src |->V Generate a frei0r source. life |->V Create life. mandelbrot |->V Render a Mandelbrot fractal. movie |->V Read from a movie source. mptestsrc |->V Generate various test pattern. nullsrc |->V Null video source, return unprocessed video frames. rgbtestsrc |->V Generate RGB test pattern. testsrc |->V Generate test pattern. buffersink V->| Buffer video frames, and make them available to the end of the filter graph. nullsink V->| Do absolutely nothing with the input video. buffer |->V Buffer video frames, and make them accessible to the filterchain.
Other filters created by users:
- https://github.com/indiefan/FFmpeg-Filters an updated vf_fade filter.