ffmpeg常用濾鏡命令

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) 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章