首页 > 代码库 > FFmpeg滤镜使用指南

FFmpeg滤镜使用指南

文件夹
1. FFmpeg滤镜文档
2. 演示样例
  2.1 缩放
  2.2 视频加速
  2.3 滤镜图,链和滤镜关系
  2.4 多个输入覆盖同一个2x2 网格
  2.5 转义字符
  2.6 烧录时间码
  2.7 描写叙述命令行參数
  2.8 測试源
3. 滤镜列表
4. 其他滤镜演示样例
5. 开发自己的滤镜

FFmpeg加入了非常多滤镜。查看哪些滤镜有效可用命令:
# ./ffmpeg -filters.
 
1. FFmpeg滤镜文档
很多其它的信息和每一个滤镜的使用演示样例可查看FFmpeg的滤镜文档: 
  http://ffmpeg.org/ffmpeg-filters.html

2. 演示样例
2.1 缩放
将输入的640x480缩小到320x240输出:  
# ./ffmpeg -i input -vf scale=iw/2:-1 output

iw  : 是输入的宽度;在本例中,输入宽度为640. 640/2 = 320. 
-1  : 通知缩放滤镜在输出时保持原始的宽高比,因此本例中缩放滤镜将选择240.

?2.2 视频加速
1. 加速/减慢视频
能够使用 “setpts"(http://ffmpeg.org/ffmpeg.html#asetpts_002c-setpts)滤镜来改变视频速度。

加速视频命令: 
# ./ffmpeg -i input.mkv -filter:v "setpts=0.5*PTS" output.mkv

Note : 这种方法在实现你想要的速度时。会採取丢帧策略。
假设想避免丢帧。能够指定输出的帧率比输入的帧率高的办法。
比如,输入的帧率为4, 指定输出的帧率为4x, 即16fps :
# ./ffmpeg -i input.mkv -r 16 -filter:v "setpts=0.125*PTS" -an output.mkv

减慢视频命令: 
# ./ffmpeg -i input.mkv -filter:v "setpts=2.0*PTS" output.mkv

2. 加速/减慢音频
能够使用" atempo" 音频滤镜来加速或减慢音频。

如双倍速音频命令: 
# ./ffmpeg -i input.mkv -filter:a "atempo=2.0" -vn output.mkv

"atempo"滤镜对音频速度调整限制在0.5 到 2.0 之间。(即半速或倍速)。


假设有必要。能够使用多个atempo滤镜来实现,如以下的命令实现四倍速音频:
# ./ffmpeg -i input.mkv -filter:a "atempo=2.0,atempo=2.0" -vn output.mkv

使用更复杂的滤镜图,能够同一时候加速视频和音频:
# ./ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mkv

2.3 滤镜图,链,和滤镜关系
FFmpeg命令行中,跟在 "-vf"之后的就是一个滤镜图。
滤镜图能够包括多个滤镜链,而每一个滤镜链又能够包括多个滤镜。



尽管一个完整的滤镜图描写叙述非常复杂,但能够简化以避免歧义。
滤镜链使用";"分隔,滤镜链中滤镜使用","分隔。
而且,滤镜链假设没有指定输入或输出,则默认使用前面的滤镜链的输出为输入。并输出给后面的滤镜链做输入。


 
以下的命令行是相等的:
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 
                                                       # 两个链的方式,每一个链一个滤镜,链间使用[middle]填充
ffmpeg -i input -vf [in]yadif=0:0:0,scale=iw/2:-1[out] output                 
                                                       # 一个链的方式,链包括两个滤镜,使用默认链接
ffmpeg -i input -vf yadif=0:0:0,scale=iw/2:-1  output  # 输入输出也使用默认链接

2.4. 多个输入覆盖同一个2x2网格
下例中有四个输入,并使用 -filter_complex滤镜链接。
这个样例中四个输入都是 "-f lavfi -i testsrc", 也能够用别的输入取代。
在滤镜图中,第一个输入被填充到右下角,并设置成高度的两倍。
其他三个输入使用独立的滤镜"hflip, negate,和 edgedetect"。
覆盖视频滤镜被使用多次。后面三个都覆盖到第一个之上。
覆盖滤镜的偏移量在输入上形成一个网格。
# ./ffmpeg -f lavfi -i testsrc -f lavfi -i testsrc -f lavfi -i testsrc -f lavfi -i testsrc 
-filter_complex "[0:0]pad=iw*2:ih*2[a];[1:0]negate[b];[2:0]hflip[c];
[3:0]edgedetect[d];[a][b]overlay=w[x];[x][c]overlay=0:h[y];[y][d]overlay=w:h" 
-y -c:v ffv1 -t 5 multiple_input_grid.avi

2.5 转义字符
如文档中的描写叙述,滤镜间的","分隔符是必须的。但它也会出如今參数中。如以下的"select"滤镜:
# ./ffmpeg -i input -vf select=‘eq(pict_type\,PICT_TYPE_I)‘ output        # to select only I frames

作为一个选择。在滤镜图中相同能够在双引號中使用空格,这样更方便阅读:
# ./ffmpeg -i input -vf "select=eq(pict_type,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

2.6 烧录时间码
使用 "drawtext"视频滤镜。



PAL 25 fps 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 30 fps 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

2.7 描写叙述命令行參数
构建复杂的滤镜图时,会使命令行看起来一团混乱,怎样使滤镜图命令行可管理就变得非常实用了。
以下的样例包括三个滤镜,yadif, scale, 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: 包括整个滤镜图的双引號须要使用转义符 \", 
eval $command_line变量用于避免转义符丢失。


 
2.8 測试源
testsrc源滤镜生成一个測试视频源,包括有颜色模式。灰度值和时间戳。

它在用于測试目的时非常实用。
以下的样例生成一个10秒的输出。30fps,共300帧,帧大小为 1280x720:
# ./ffmpeg -f lavfi -i testsrc=http://www.mamicode.com/duration=10:size=1280x720:rate=30 output.mpg

使用 smptebars源滤镜的样例:
# ./ffmpeg -f lavfi -i "smptebars=duration=5:size=1280x720:rate=30" output.mp4

ffplay相同能用于查看结果滤镜图:
ffplay -f lavfi -i "testsrc=http://www.mamicode.com/duration=10:size=1280x720:rate=30"

3. 滤镜列表
滤镜和libavfilter绑定在一起,如 4.3.100(as configured with --enable-gpl). 
使用外部库的滤镜并未列在这里,如frei0r.能够查看FFMPEG的滤镜文档。
滤镜列表约定:
  T.. = Timeline support
  .S. = Slice threading
  ..C = Commmand support
  A = Audio input/output
  V = Video input/output
  N = Dynamic number and/or type of input/output
  | = Source or sink filter

滤镜:
 ... aconvert         A->A       Convert the input audio to sample_fmt:channel_layout.
 T.. adelay           A->A       延迟一个或多个音频通道
 ... aecho            A->A       给音频加入回音
 ... aeval            A->A       Filter audio signal according to a specified expression.
 T.. afade            A->A       Fade in/out input audio.
 ... aformat          A->A       Convert the input audio to one of the specified formats.
 ... ainterleave      N->A       Temporally interleave audio inputs.
 ... allpass          A->A       Apply a two-pole all-pass filter.
 ... amerge           N->A       Merge two or more audio streams into a single multi-channel stream.
 ... amix             N->A       Audio mixing.
 ... anull            A->A       Pass the source unchanged to the output.
 T.. apad             A->A       Pad audio with silence.
 ... aperms           A->A       Set permissions for the output audio frame.
 ... aphaser          A->A       Add a phasing effect to the audio.
 ... aresample        A->A       重採样音频数据
 ... aselect          A->N       选择音频帧给输出
 ... asendcmd         A->A       Send commands to filters.
 ... asetnsamples     A->A       Set the number of samples for each output audio frames.
 ... asetpts          A->A       Set PTS for the output audio frame.
 ... asetrate         A->A       Change the sample rate without altering the data.
 ... asettb           A->A       Set timebase for the audio output link.
 ... ashowinfo        A->A       Show textual information for each audio frame.
 ... asplit           A->N       Pass on the audio input to N audio outputs.
 ... astats           A->A       Show time domain statistics about audio frames.
 ... astreamsync      AA->AA     Copy two streams of audio data in a configurable order.
 ..C atempo           A->A       Adjust audio tempo.
 ... atrim            A->A       Pick one continuous section from the input, drop the rest.
 ... bandpass         A->A       Apply a two-pole Butterworth band-pass filter.
 ... bandreject       A->A       Apply a two-pole Butterworth band-reject filter.
 ... bass             A->A       Boost or cut lower frequencies.
 ... biquad           A->A       Apply a biquad IIR filter with the given coefficients.
 ... channelmap       A->A       Remap audio channels.
 ... channelsplit     A->N       Split audio into per-channel streams.
 ... compand          A->A       Compress or expand audio dynamic range.
 ... earwax           A->A       Widen the stereo image.
 ... ebur128          A->N       EBU R128 scanner.
 ... equalizer        A->A       Apply two-pole peaking equalization (EQ) filter.
 ... highpass         A->A       Apply a high-pass filter with 3dB point frequency.
 ... join             N->A       Join multiple audio streams into multi-channel output.
 ... lowpass          A->A       Apply a low-pass filter with 3dB point frequency.
 ... pan              A->A       Remix channels with coefficients (panning).
 ... replaygain       A->A       ReplayGain scanner.
 ... silencedetect    A->A       检測静音
 ... treble           A->A       Boost or cut upper frequencies.
 T.C volume           A->A       改变输入音量
 ... volumedetect     A->A       检測音频音量
 ... aevalsrc         |->A       Generate an audio signal generated by an expression.
 ... anullsrc         |->A       Null audio source, return empty audio frames.
 ... sine             |->A       Generate sine wave audio signal.
 ... anullsink        A->|       Do absolutely nothing with the input audio.
 ... alphaextract     V->N       Extract an alpha channel as a grayscale image component.
 ... alphamerge       VV->V      Copy the luma value of the second input into the alpha channel of the first input.
 T.. bbox             V->V       Compute bounding box for each frame.
 ... blackdetect      V->V       Detect video intervals that are (almost) black.
 ... blackframe       V->V       检測差点儿全黑的帧
 TS. blend            VV->V      Blend two video frames into each other.
 T.. boxblur          V->V       Blur the input.
 T.. colorbalance     V->V       Adjust the color balance.
 T.. colorchannelmixer V->V       Adjust colors by mixing color channels.
 T.. colormatrix      V->V       Convert color matrix.
 ... copy             V->V       复制输入视频到输出
 ... crop             V->V       裁剪输入视频
 T.. cropdetect       V->V       自己主动检測裁剪尺寸
 TS. curves           V->V       Adjust components curves.
 T.. dctdnoiz         V->V       Denoise frames using 2D DCT.
 ... decimate         N->V       Decimate frames (post field matching filter).
 ... dejudder         V->V       Remove judder produced by pullup.
 T.. delogo           V->V       去掉输入视频的logo
 ... deshake          V->V       Stabilize shaky video.
 T.. drawbox          V->V       在输入视频上画一个颜色块
 T.. drawgrid         V->V       在输入视频上画一个颜色网格
 T.. edgedetect       V->V       检測并画边缘
 ... elbg             V->V       Apply posterize effect, using the ELBG algorithm.
 ... extractplanes    V->N       Extract planes as grayscale frames.
 .S. fade             V->V       Fade in/out input video.
 ... field            V->V       Extract a field from the input video.
 ... fieldmatch       N->V       Field matching for inverse telecine.
 T.. fieldorder       V->V       Set the field order.
 ... format           V->V       Convert the input video to one of the specified pixel formats.
 ... fps              V->V       强制成常量帧率Force constant framerate.
 ... framepack        VV->V      Generate a frame packed stereoscopic video.
 T.. framestep        V->V       每N帧中选择一帧
 T.. geq              V->V       Apply generic equation to each pixel.
 T.. gradfun          V->V       Debands video quickly using gradients.
 TS. haldclut         VV->V      Adjust colors using a Hald CLUT.
 .S. hflip            V->V       Horizontally flip the input video.
 T.. histeq           V->V       Apply global color histogram equalization.
 ... histogram        V->V       计算并画直方图
 T.. hqdn3d           V->V       Apply a High Quality 3D Denoiser.
 T.C hue              V->V       Adjust the hue and saturation of the input video.
 ... idet             V->V       隔行检測滤镜
 T.. il               V->V       去隔行或隔行场
 ... interlace        V->V       转换逐行视频成隔行视频
 ... interleave       N->V       Temporally interleave video inputs.
 ... kerndeint        V->V       Apply kernel deinterlacing to the input.
 TS. lut3d            V->V       Adjust colors using a 3D LUT.
 T.. lut              V->V       Compute and apply a lookup table to the RGB/YUV input video.
 T.. lutrgb           V->V       Compute and apply a lookup table to the RGB input video.
 T.. lutyuv           V->V       Compute and apply a lookup table to the YUV input video.
 ... mcdeint          V->V       Apply motion compensating deinterlacing.
 ... mergeplanes      N->V       Merge planes.
 ... mp               V->V       Apply a libmpcodecs filter to the input video.
 ... mpdecimate       V->V       Remove near-duplicate frames.
 T.. 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.
 TS. noise            V->V       Add noise.
 ... null             V->V       Pass the source unchanged to the output.
 T.C overlay          VV->V      Overlay a video source on top of the input.
 T.. owdenoise        V->V       Denoise using wavelets.
 ... pad              V->V       Pad the input video.
 ... perms            V->V       Set permissions for the output video frame.
 T.. perspective      V->V       Correct the perspective of video.
 ... phase            V->V       Phase shift fields.
 ... pixdesctest      V->V       Test pixel format definitions.
 T.C pp               V->V       Filter video using libpostproc.
 ... psnr             VV->V      Calculate the PSNR between two video streams.
 ... pullup           V->V       Pullup from field sequence to frames.
 T.. removelogo       V->V       Remove a TV logo based on a mask image.
 TSC rotate           V->V       Rotate the input image.
 T.. sab              V->V       Apply shape adaptive blur.
 ... scale            V->V       Scale the input video size and/or convert the image format.
 ... select           V->N       选择视频帧并传给输出
 ... sendcmd          V->V       Send commands to filters.
 ... separatefields   V->V       Split input video frames into fields.
 ... 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 video output link.
 ... showinfo         V->V       Show textual information for each video frame.
 ... shuffleplanes    V->V       Shuffle video planes
 T.. smartblur        V->V       Blur the input video without impacting the outlines.
 ... split            V->N       Pass on the input to N video outputs.
 T.C spp              V->V       Apply a simple post processing filter.
 ... stereo3d         V->V       Convert video stereoscopic 3D view.
 ... super2xsai       V->V       Scale the input by 2x using the Super2xSaI pixel art algorithm.
 ... swapuv           V->V       Swap U and V components.
 ... telecine         V->V       Apply a telecine pattern.
 ... thumbnail        V->V       Select the most representative frame in a given sequence of consecutive frames.
 ... tile             V->V       Tile several successive frames together.
 ... tinterlace       V->V       Perform temporal field interlacing.
 .S. transpose        V->V       Transpose input video.
 ... trim             V->V       Pick one continuous section from the input, drop the rest.
 T.. unsharp          V->V       Sharpen or blur the input video.
 ... vflip            V->V       Flip the input video vertically.
 T.. vignette         V->V       Make or reverse a vignette effect.
 T.. w3fdif           V->V       Apply Martin Weston three field deinterlace.
 TS. yadif            V->V       对输入图像去隔行
 ... cellauto         |->V       Create pattern generated by an elementary cellular automaton.
 ..C color            |->V       Provide an uniformly colored input.
 ... haldclutsrc      |->V       Provide an identity Hald CLUT.
 ... life             |->V       Create life.
 ... mandelbrot       |->V       Render a Mandelbrot fractal.
 ... mptestsrc        |->V       Generate various test pattern.
 ... nullsrc          |->V       Null video source, return unprocessed video frames.
 ... rgbtestsrc       |->V       Generate RGB test pattern.
 ... smptebars        |->V       Generate SMPTE color bars.
 ... smptehdbars      |->V       Generate SMPTE HD color bars.
 ... testsrc          |->V       Generate test pattern.
 ... nullsink         V->|       Do absolutely nothing with the input video.
 ... avectorscope     A->V       Convert input audio to vectorscope video output.
 ... concat           N->N       Concatenate audio and video streams.
 ... showspectrum     A->V       Convert input audio to a spectrum video output.
 ... showwaves        A->V       Convert input audio to a video output.
 ... amovie           |->N       Read audio from a movie source.
 ... movie            |->N       Read from a movie source.
 ... ffbuffersink     V->|       Buffer video frames, and make them available to the end of the filter graph.
 ... ffabuffersink    A->|       Buffer audio frames, and make them available to the end of the filter graph.
 ... abuffer          |->A       Buffer audio frames, and make them accessible to the filterchain.
 ... buffer           |->V       Buffer video frames, and make them accessible to the filterchain.
 ... abuffersink      A->|       Buffer audio frames, and make them available to the end of the filter graph.
 ... buffersink       V->|       Buffer video frames, and make them available to the end of the filter graph.
 ... afifo            A->A       Buffer input frames and send them when they are requested.
 ... fifo             V->V       Buffer input images and send them when they are requested.

4. 其他滤镜演示样例
Fancy Filtering Examples (http://trac.ffmpeg.org/wiki/FancyFilteringExamples)
– 各种迷幻效果和怪异滤镜的演示样例
 
5. 开发自己的滤镜
See FFmpeg filter HOWTO(http://blog.chinaunix.net/uid-26000296-id-3068068.html) 

FFmpeg滤镜使用指南