ffmpeg filter参数设置格式

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.

Documentation ¶

Refer to the FFmpeg filters documentation for more information and examples for each filter. This wiki page is for user contributed examples and tips, and contributions to this page are encouraged.

Examples

Scaling

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 FFmpeg documentation for additional information.

Speed up your video

See How to speed up / slow down a video for examples.

Filtergraph,Chain,Filter relationship

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

As are:

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

multiple input overlay in 2x2 grid

Here four inputs are filtered together using the -filter_complex option. In this case all of the inputs are the -f lavfi -i testsrc (the testsrc source filter) but could be other inputs.

Within the filtergraph the first input is padded to the right and bottom by double its height, and the other three inputs are individually filtered using hflipnegate, and edgedetect.

The overlay video filter is then used multiple times for placement of each input. The offsets used in the overlay filter arrange the inputs into a grid shape.

ffmpeg -f lavfi -i testsrc -f lavfi -i testsrc -f lavfi -i testsrc -f lavfi -i testsrc -filter_complex \
"[0:v]pad=iw*2:ih*2[a]; \
 [1:v]negate[b]; \
 [2:v]hflip[c]; \
 [3:v]edgedetect[d]; \
 [a][b]overlay=w[x]; \
 [x][c]overlay=0:h[y]; \
 [y][d]overlay=w:h[out]" -map "[out]" -c:v ffv1 -t 5 multiple_input_grid.avi

Be aware that frames are taken from each input video in timestamp order, so it is a good idea to pass all overlay inputs through a setpts=PTS-STARTPTS filter to have them begin in the same zero timestamp, such as[0:v]hflip,setpts=PTS-STARTPTS[a];[1:v]setpts=PTS-STARTPTS[b];[a][b]overlay.

Escaping characters

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\,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,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. See Notes on filtergraph escaping for more information.

Burnt in Timecode

Using the drawtext video filter.

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

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="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" -vf "$filter" "$codec" -an $out_file")

echo "${command_line[@]}"
"${command_line[@]}"
exit

Note that the filtergraph spans more than one line. The echo command shows the full command as it is executed. Useful for debugging.

The array in the $command_line variable helps avoid loss of the quotes which occurs otherwise. Other shells may behave differently.

Test Source

The testsrc source filter generates a test video pattern showing a color pattern, a scrolling gradient, and a timestamp. This is useful for testing purposes.

This example will create a 10 second output, 30 fps (300 frames total), with a frame size of 1280x720:

ffmpeg -f lavfi -i testsrc=duration=10:size=1280x720:rate=30 output.mpg

Another example using the smptebars source filter:

ffmpeg -f lavfi -i "smptebars=duration=5:size=1280x720:rate=30" output.mp4

ffplay can also be used to view the resulting filtergraph:

ffplay -f lavfi -i "testsrc=duration=10:size=1280x720:rate=30"

Other Filter Examples

Developing your own Filters

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要重新连接视频流,可以使用FFmpeg的`avformat_open_input`函数,并通过设置`AVFormatContext`的一些参数来实现。以下是一些常用的参数设置: ```cpp #include <libavformat/avformat.h> // 创建AVFormatContext对象 AVFormatContext* formatContext = avformat_alloc_context(); // 设置重连参数 formatContext->interrupt_callback.callback = [](void*){ return 0; }; // 设置中断回调函数,返回0表示继续执行 formatContext->interrupt_callback.opaque = nullptr; // 回调函数的私有数据,可以设置为nullptr // 打开视频流 int ret = avformat_open_input(&formatContext, m_url, nullptr, nullptr); if (ret < 0) { // 打开失败处理 avformat_free_context(formatContext); return; } // 启用自动重连 formatContext->flags |= AVFMT_FLAG_AUTO_BSF; // 启用自动比特流过滤器(Bitstream Filter) // ... // 使用formatContext进行后续操作,如读取视频帧等 ``` 在上述代码中,我们首先创建一个`AVFormatContext`对象,并使用`avformat_alloc_context`函数进行分配。 然后,我们设置了重连参数。通过设置`formatContext->interrupt_callback.callback`为一个返回0的回调函数,表示在需要中断时返回,从而实现重新连接的目的。同时,我们将`formatContext->interrupt_callback.opaque`设置为`nullptr`,即回调函数的私有数据为空。 接下来,我们使用`avformat_open_input`函数打开视频流。如果打开失败,可以根据需要进行相应的错误处理。 最后,我们启用了自动重连功能,通过设置`formatContext->flags |= AVFMT_FLAG_AUTO_BSF`来启用自动比特流过滤器(Bitstream Filter)。 请注意,以上代码只是一个示例,并不包含完整的错误处理和视频流读取逻辑。在实际应用中,你需要根据具体情况进行适当的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值