ffmpeg将tile拼成完整的视频

如果视频按照2x2被切割成4个tile,那么如何利用ffmpeg将它们拼成原来的视频呢?
利用ffmpeg中的filter就可以。

Complex filtergraphs

Complex filtergraphs are those which cannot be described as simply a linear processing chain applied to one stream.
This is the case, for example, when the graph has more than one input and/or output, or when output stream type is different from input.
They can be represented with the following diagram:

Complex filtergraphs

Complex filtergraphs are configured with the -filter_complex option.
Note that this option is global, since a complex filtergraph, by its nature, cannot be unambiguously associated with a single stream or file.
The -lavfi option is equivalent to -filter_complex.

ffmpeg -i video.mkv -i image.png -filter_complex '[0:v][1:v]overlay[out]'
 -map '[out]' out.mkv

Here [0:v] refers to the first video stream in the first input file, which is linked to the first (main) input of the overlay filter.
Similarly the first video stream in the second input is linked to the second (overlay) input of overlay.

Filtergraph description

A filtergraph is a directed graph of connected filters.
It can contain cycles, and there can be multiple links between a pair of filters.
Each link has one input pad on one side connecting it to one filter from which it takes its input, and one output pad on the other side connecting it to one filter accepting its output.
Each filter in a filtergraph is an instance of a filter class registered in the application, which defines the features and the number of input and output pads of the filter.
A filter with no input pads is called a "source", and a filter with no output pads is called a "sink".

Filtergraph syntax

A filtergraph consists of a sequence of filterchains.
A sequence of filterchains is represented by a list of ";"-separated filterchain descriptions.

用分号分割。

A filter is represented by a string of the form:

[in_link_1]...[in_link_N]filter_name@id=arguments[out_link_1]...[out_link_M]

比如:

-filter_complex 
"[0:v]pad=7680:4320:0:0[out0];
[out0][1:v]overlay=x=3840:y=0[out1];
[out1][2:v]overlay=x=0:y=2160[out2];
[out2][3:v]overlay=x=3840:y=2160" 

pad filter

Add paddings to the input image, and place the original input at the provided x, y coordinates.
width, w
height, h
Specify an expression for the size of the output image with the paddings added.
If the value for width or height is 0, the corresponding input size is used for the output.
The width expression can reference the value set by the height expression, and vice versa.
The default value of width and height is 0.

确定输出的image的宽高。

x
y
Specify the offsets to place the input image at within the padded area, with respect to the top/left border of the output image.
The x expression can reference the value set by the y expression, and vice versa.
The default value of x and y is 0.

If x or y evaluate to a negative number,
they’ll be changed so the input image is centered on the padded area.

确定将input image放在padded area的放置的坐标。

举例:

-filter_complex 
"[0:v]pad=7680:4320:0:0[out0];
...

overlay filter

Overlay one video on top of another.
It takes two inputs and has one output.
The first input is the "main" video on which the second input is overlaid.

overlay可以将一个视频放置在另外一个视频上。
它有两个输入参数和一个输出参数。
第一个输入参数是主video,第2个输入放置在第一个上面。

x
y
Set the expression for the x and y coordinates of the overlaid video on the main video.
Default value is "0" for both expressions.
In case the expression is invalid, it is set to a huge value (meaning that the overlay will not be displayed within the output visible area).

举例:

[out0][1:v]overlay=x=3840:y=0[out1]

将后边视频放置在前边视频的(3840, 0)位置上。

使用ffmpeg将tile拼成完整的视频

./ffmpeg  
-i tile0.mp4 
-i tile1.mp4 
-i tile2.mp4 
-i tile3.mp4 
-filter_complex 
"[0:v]pad=7680:4320:0:0[out0];
[out0][1:v]overlay=x=3840:y=0[out1];
[out1][2:v]overlay=x=0:y=2160[out2];
[out2][3:v]overlay=x=3840:y=2160" 
-vcodec libx264 
-crf 18 
-an 
-y 
out/output.mp4

滤波器的操作分成4步:

  • 将tile0.mp4放置在7680*4320padded area的(0, 0)处, 输出为out0。
[0:v]pad=7680:4320:0:0[out0]
  • 在padded area上放置tile1,坐标是(3840, 0)
[out0][1:v]overlay=x=3840:y=0[out1];
  • 在padder area上放置tile2,坐标是(0, 2160)
[out1][2:v]overlay=x=0:y=2160[out2];
  • 在padded area上放置tile3,坐标为(3840, 2160)
[out2][3:v]overlay=x=3840:y=2160

以上4个滤波器连在一起,就将tile拼成了完整的视频。

References:

https://ffmpeg.org/ffmpeg.html
https://ffmpeg.org/ffmpeg-filters.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,900评论 0 10
  • 我还是比较喜欢港股中的部分公司。在全球低利率、资产荒的背景下,我们曾经对全球各种资产类别进行过系统梳理,发现港股是...
    Leon77阅读 293评论 0 0
  • “victory!!!”看着屏幕上的胜利弹窗,摘下耳机,发现窗外早已漆黑一片了。我看看显示器上的时间,才5:13。...
    yf_wkwk阅读 369评论 0 0
  • 夜色悄上树梢头,美瞳印上月圆愁。 睫毛轻合微风颤,口红染尽风尘花。 闭眼看透世上人,轻扬嘴角笑不语。 又是一年归家...
    清风莫回阅读 228评论 0 0
  • 最近在看一本书,你是否相信精神有问题的人,也许并不是精神问题,而是他们发现了另一个世界,另一个我们不知道的世界。 ...
    SNoug_kyo阅读 237评论 0 0

友情链接更多精彩内容