码迷,mamicode.com
首页 > 其他好文 > 详细

FFmpeg滤镜使用指南

时间:2014-06-02 05:05:11      阅读:508      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   blog   code   a   

目录
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=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=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滤镜使用指南,布布扣,bubuko.com

FFmpeg滤镜使用指南

标签:des   c   style   blog   code   a   

原文地址:http://blog.csdn.net/fireroll/article/details/27992913

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!