pip(picture-in-picture)是指一个画面在另一个画面中显示,那么如果有两个视频,怎么着实现这种效果使其作为一个视频播放呢?

可以使用ffmpeg工具将两个视频合成为一个视频。ffmpeg是一款强大的音视频处理开源工具库。

python 中ffmpeg模块的安装和使用教程 ffmpeg pip_ffmpeg

如上图所示,将两个视频合成一个:

>ffmpeg -ss 0 -t 10 -i input1.mp4 -ss 5.3 -t 10 -i input2.mp4 -filter_complex "[1]scale=448:252[ov1];[0][ov1]overlay=0:main_h-overlay_h" -map 0:v -map 0:a -map 1:v -preset ultrafast -crf 22 -profile:v main -y output.mp4

 

参数解析:

-i 输入文件

-ss 起始时间(单位秒,可以包含小数)

-t  持续时间

-y 表示直接覆盖输出文件(若已存在)

-filter_complex 一般用于处理ffmpeg复杂指令操作,可以在其中对输入流进行裁剪、缩放、设置播放速度等

-map 用于控制在输出文件中对流(音频、视频等)的选择,如-map 0:v 选择第一个输入文件的视频流

-preset 可以简单控制编码效率。可选参数: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow and placebo. 编码加快,意味着信息丢失越严重,输出图像质量越差。

-crf  范围 0-51: 0是编码毫无丢失信息, 23 is 默认, 51 是最差的情况。相对合理的区间是18-28. 值越大,压缩效率越高,但也意味着信息丢失越严重,输出图像质量越差。

-profile:v  H.264有四种画质级别,分别是baseline, extended, main, high

 

 

pip合成通过overlay指令具体实现,它支持很多参数选项。

指定小视频的具体位置:

>ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[1]scale=iw*0.35:ih*0.35[ov1];[0][ov1]overlay=x=10:y=main_h-overlay_h-10"  -y output.mp4

x,y代表了视频位置左上角的坐标,可指定具体值,但常用的下面参数进行处理:

The x, and y expressions can contain the following parameters.
main_w, W
main_h, H
      The main input width and height.
overlay_w, w
overlay_h, h
      The overlay input width and height.

 

很多时候由于两个视频的播放时长是不同的,如果较短的视频播放完成后需要停止播放还是继续播放呢?可以通过eof_action选项设置不同的处理方式。具体说明如下:

eof_action
   The action to take when EOF is encountered on the secondary input; it accepts one of the following values:
   repeat
        Repeat the last frame (the default).
   endall
        End both streams.
   pass
        Pass the main input through.

>ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[1]scale=iw*0.35:ih*0.35[ov1];[0][ov1]overlay=0:main_h-overlay_h:eof_action=pass"  -y output.mp4

 

另外,还可使用shortest选项进行处理,如shortest=1在短视频结束后直接结束视频:

shortest
   If set to 1, force the output to terminate when the shortest input terminates. Default value is 0.
repeatlast
   If set to 1, force the filter to extend the last frame of secondary streams until the end of the primary stream. A value of 0 disables this behavior. Default value is 1.
>ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[1]scale=iw*0.35:ih*0.35[ov1];[0][ov1]overlay=0:main_h-overlay_h:shortest=1"  -y output.mp4

 

也可以直接通过-shortest选项设置:

>ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[1]scale=iw*0.35:-1[ov1];[0][ov1]overlay=0:main_h-overlay_h" -shortest -y output.mp4

 

 

pip合成时在小视频上加边框:

>ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[1]scale=448:252,drawbox=x=0:y=0:w=448:h=252:color=DarkGray@0.8:t=4[ov1];[0][ov1]overlay=0:main_h-overlay_h" -y output.mp4

指令中参数drawbox用于画矩形框,可以指定位置、宽高、颜色、边框宽度等。

python 中ffmpeg模块的安装和使用教程 ffmpeg pip_ffmpeg_02

 

 

三个视频pip合成:

>ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[1]scale=448:252[ov1];[2]scale=216:384[ov2];[0][ov1]overlay=0:main_h-overlay_h[pip0];[pip0][ov2]overlay=main_w-overlay_w:main_h-overlay_h" -y output.mp4

python 中ffmpeg模块的安装和使用教程 ffmpeg pip_图像质量_03

下面指令也可产生上述同样的效果(movie对应的可以是视频、音频、图片)

>ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[1]scale=448:252[ov1];movie=input3.mp4,scale=216:384[ov2];[0][ov1]overlay=0:main_h-overlay_h[pip0];[pip0][ov2]overlay=main_w-overlay_w:main_h-overlay_h" -y output.mp4