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

你可能感兴趣的:(ffmpeg将tile拼成完整的视频)