ffmpeg流媒体视频合并(画中画)效果并推流

通过ffmpeg工具将多个视频流进行处理,实现画中画效果(效果如下图),并推流到服务器。

Ffmpeg -rtsp_transport tcp -i “[视频源1]” -rtsp_transport tcp -i “[视频源2]” -filter_complex “[1]scale=iw/2:ih/2[pip];[0][pip]overlay=main_w-overlay_w-10:main_h-overlay_h-10” -f flv -r 25 -s 1920*1080 -an rtmp://localhost:1935/live/room

我的两个视频源是摄像头源,如果是本地文件,可以直接这样写。

Ffmpeg -i a.mp4 -i b.mp4 -filter_complex “[1]scale=iw/2:ih/2[pip];[0][pip]overlay=main_w-overlay_w-10:main_h-overlay_h-10” -f flv -r 25 -s 1920*1080 -an rtmp://localhost:1935/live/room

推流命令详解

-rtsp_transport tcp -i “rtsp://admin:dayou123@172.16.3.234/h264/1/main/av_stream”

-rtsp_transport 传输类型:tcp -i 视频源:“rtsp://admin:dayou123@172.16.3.234/h264/1/main/av_stream” rtsp_transport tcp -i “rtsp://admin:dayou123@172.16.3.234/h264/1/main/av_stream”

第二个视频源,参数意义同上

filter_complex “[1]scale=iw/2:ih/2[pip];[0][pip]overlay=main_w-overlay_w-10:main_h-overlay_h-10”

此部分为实现画中画命令(流合并)

filter_complex 说明后边双引号中为filter(过滤器)相关操作。

[0] 表示第一个源(-i) [1] 表示第二个源(-i) 以此类推 [1]scale=iw/2:ih/2[pip] 缩放视频源[1]为源宽(iw)二分之一,高(ih)二分之一。得到的结果为[pip] [0][pip]overlay=main_w-overlay_w-10:main_h-overlay_h-10 将处理后的视频源[1],也就是[pip],覆盖到视频源[0]上。 main_w 为 [0] 宽度 main_h 为 [0] 高度 overlay_w 为 [pip] 宽度 overlay_h 为 [pip] 高度

main_w-overlay_w-10 main_h-overlay_h-10

这个就是在计算上图 A 点坐标,该坐标确定了[pip]在[0]中的位置。

经验分享 程序员 微信小程序 职场和发展