Create a mosaic out of several input videos

2023-10-27

转自:https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos

Overview

One of the great features of ffmpeg filtering library is the ability to create overlays, allowing users to put one video over another. We can also use this feature to implement the mosaic video output, usually used in security surveillance systems. An example of what we are going to achieve in this tutorial is displayed in the following screenshot:

We can see one video, displaying 4 different inputs at the same time. This can be done using the following ffmpeg command line (which we'll explain in detail):

ffmpeg
	-i 1.avi -i 2.avi -i 3.avi -i 4.avi
	-filter_complex "
		nullsrc=size=640x480 [base];
		[0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft];
		[1:v] setpts=PTS-STARTPTS, scale=320x240 [upperright];
		[2:v] setpts=PTS-STARTPTS, scale=320x240 [lowerleft];
		[3:v] setpts=PTS-STARTPTS, scale=320x240 [lowerright];
		[base][upperleft] overlay=shortest=1 [tmp1];
		[tmp1][upperright] overlay=shortest=1:x=320 [tmp2];
		[tmp2][lowerleft] overlay=shortest=1:y=240 [tmp3];
		[tmp3][lowerright] overlay=shortest=1:x=320:y=240
	"
	-c:v libx264 output.mkv

The command line above, if written in the shell directly, should look like this of course:

ffmpeg -i 1.avi -i 2.avi -i 3.avi -i 4.avi -filter_complex "nullsrc=size=640x480 [base]; [0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft]; [1:v] setpts=PTS-STARTPTS, scale=320x240 [upperright]; [2:v] setpts=PTS-STARTPTS, scale=320x240 [lowerleft]; [3:v] setpts=PTS-STARTPTS, scale=320x240 [lowerright]; [base][upperleft] overlay=shortest=1 [tmp1]; [tmp1][upperright] overlay=shortest=1:x=320 [tmp2]; [tmp2][lowerleft] overlay=shortest=1:y=240 [tmp3]; [tmp3][lowerright] overlay=shortest=1:x=320:y=240" -c:v libx264 output.mkv

You can see the final video at the following link: http://www.youtube.com/watch?v=ix2HxIfo4WY

Detailed explanation

Let's explain how exactly does this work, so you can create your own variants of overlay filter usage. First of all get yourself familiar with the overlay filter and all of its options. Also, pay a very close attention to the provided examples section.

What we need to do, in this tutorial, is to overlay 4 input videos on top of the blank background video. The end result should look like this:

The filter graph, for this particular case, looks something like this:

Now, let's get back to the command line we've used and lets explain it line by line:

ffmpeg
	-i 1.avi -i 2.avi -i 3.avi -i 4.avi
	-filter_complex "
		nullsrc=size=640x480 [base];
		[0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft];
		[1:v] setpts=PTS-STARTPTS, scale=320x240 [upperright];
		[2:v] setpts=PTS-STARTPTS, scale=320x240 [lowerleft];
		[3:v] setpts=PTS-STARTPTS, scale=320x240 [lowerright];
		[base][upperleft] overlay=shortest=1 [tmp1];
		[tmp1][upperright] overlay=shortest=1:x=320 [tmp2];
		[tmp2][lowerleft] overlay=shortest=1:y=240 [tmp3];
		[tmp3][lowerright] overlay=shortest=1:x=320:y=240
	"
	-c:v libx264 output.mkv

First, we created a background for our output video, using nullsrc filter, which has the dimension of 640x480 pixels (4 videos of 320x240 pixels) and we tagged it with the name "base". Then, we also tagged each ffmpeg input to be able to reference them later in the filter graph. The way we did it was like this:

[0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft]

The stream specifier "[0:v]" is telling ffmpeg to use the video stream from the first input. After we specified the input we want, we also made sure that PTS of that video starts from zero, using setpts filter with "setpts=PTS-STARTPTS". We also did the same for all the other video inputs to make sure everything will be in sync. After that, we scaled our input video to the appropriate size and finally tagged it with a descriptive name like "upperleft".

After tagging, we started to overlay videos, one by one. First, we took the "base" video (our empty background video) and then overlaid the "upperleft" video on top of it:

[base][upperleft] overlay=shortest=1 [tmp1];

We used "shortest=1" here to specify that we want the output video to stop when the shortest input video stops. Note that we didn't specify any coordinates (x, y) so the upperleft video would be located at (0, 0) i.e. in the upper-left area of the output video. After all that, we tagged that temporary step with the name "tmp1", because we will need to overlay the next input video on that temporary result:

[tmp1][upperright] overlay=shortest=1:x=320 [tmp2];

That line specifies that the "upperright" video should be overlaid on top of the "tmp1" video, which we produced in the previous step. Here we specified the output x position to 320 pixels, in order to move the video to the right. Also, we did not specify any y position so it defaults to y=0.

Same thing was used in the subsequent lines, but there is one important thing to notice for the last line:

[tmp3][lowerright] overlay=shortest=1:x=320:y=240

Here, we didn't specify the tag name for our result, because we want our result to be actual output of the whole filter graph that we created so far. Also note there is no semi-column at the end of that line.

Final word

That's pretty much it. If it sounds easy, that's because great people behind FFmpeg project have worked hard to provide you with such an amazing tool :) One way to say "Thank You" to them is by considering a donation to FFmpeg project ;)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Create a mosaic out of several input videos 的相关文章

  • 如何在 PHP 中运行 ffmpeg 命令

    我需要在 PHP 中运行 ffmpeg 命令 但是 php ffmpeg 不再支持最新版本并且已经过时 我可以知道在网络文件中运行 ffmpeg 命令的替代方法吗 PHP Javascript jQuery 我尝试exec and shel
  • 如何在Android项目中使用libffmpeg.so?

    我正在尝试在 Android 中创建一个屏幕录制应用程序 为此 我使用 FFmpeg 我已经创建了 libffmpeg so 文件 现在我想在 Android 项目中使用相同的方法来调用它的本机函数 我怎样才能做到这一点 本教程提供了有关此
  • Python 用静态图像将 mp3 转换为 mp4

    我有x文件包含一个列表mp3我想转换的文件mp3文件至mp4文件带有static png photo 似乎这里唯一的方法是使用ffmpeg但我不知道如何实现它 我编写了脚本来接受输入mp3文件夹和一个 png photo 然后它将创建新文件
  • 如何从 Linux 命令行获取视频文件的分辨率(宽度和高度)?

    我一直在挖掘 mplayer mencoder 和 ffmpeg 文档 但我似乎无法想出anything 我对输出格式不是特别挑剔 因为我可以使用正则表达式将其拉出来 我只是似乎无法首先获取数据 Use ffprobe https ffmp
  • 在 ffmpeg 中,如何使用scale2ref 过滤器缩放 dvdsub 字幕以匹配视频大小?

    我有一个从直播电视录制的 mpeg 文件 其中包含视频以及多个音频和字幕流 我的最终目标是能够创建较小的视频文件 因为 mpeg 文件大小为数 GB 我在这条道路上的第一步只是能够选择视频 音频和字幕流中的每一个并将它们复制到 mkv 文件
  • OpenCV IP 相机应用程序崩溃 [h264 @ 0xxxxx] 访问单元中缺少图片

    我在 cpp 中有一个 opencv 应用程序 它使用 opencv 的简单结构捕获视频流并将其保存到视频文件中 它与我的网络摄像头完美配合 但是 当我运行它从 IP 摄像机捕获流时 它可能会在大约十秒后崩溃 我的编译命令是 g O3 IP
  • Python 子进程(ffmpeg)仅在我按 Ctrl-C 程序时启动?

    我正在尝试使用 Cygwin 和 Python 2 7 并行运行一些 ffmpeg 命令 这大概是我所拥有的 import subprocess processes set commands ffmpeg i input mp4 outpu
  • 在 macOS 上为 MoviePy 安装 ffmpeg 失败并出现 SSL 错误

    我正在尝试编写一个 Python 程序 在 Mac OS 10 11 16 上使用 MoviePy 将 MP4 文件转换为 GIF 我用 import moviepy editor as mp 我收到一条错误消息 说我需要打电话imagei
  • 使用FFMpeg确定视频类型,然后进行转换?

    我正在尝试以编程方式确定文件的真实类型 看来我必须使用 FFMPeg 来实现这一点 我想确定上传的文件实际上是否是 MP4 或 FLV 对于 Flash 视频 或 WebM 对于 HTML5 我知道 FFMPeg 中的 i 运算符 但我不知
  • Bash 脚本:自动为 mpeg-dash 进行 ffmpeg 编码

    我正在编写一个 bash 文件来创建视频编码和串联 以供 dash 实时流媒体使用 基本上 它读取输入视频文件夹 将所有视频编码为三种分辨率格式 然后将它们连接起来创建三个适应集 DIAGRAM 该脚本检查 fps 一致性 如果输入不是 1
  • 转换为 JPEG 时 HEIC 切片损坏

    我在将 HEIC 图像转换为 jpeg 时遇到问题 HEIC 文件是使用运行最新 iOS 公共测试版的 iPhone 拍摄的图像 我正在使用诺基亚提供的库 https github com nokiatech heif 要解析文件并从 HE
  • Node.js - 将数据缓冲到 Ffmpeg

    我使用 Node js 和 Ffmpeg 来创建动画 因为我试图避免第三方 avi mp4 解析器 所以我决定将动画输出为原始 rgb24 数据文件 然后使用一些程序将其转换为 mp4 文件 我发现 Ffmpeg 是免费且开源的 它完全可以
  • 如何在服务器上使用 ffmpeg 从 WebRTC 流获取音频和视频

    我正在尝试从 WebRTC 流获取音频和视频 并在 ubuntu 服务器上使用 ffmpeg 处理它 转码或转储 我天真地期望它能简单地解释 WebRTC 提供的 sdp 但我错了 我怀疑 ffmpeg 无法发回答案 sdp 必须手动完成
  • Android 中的 FFMpeg jni?

    我已经构建了 Bambuser http bambuser com opensource 提供的 FFMPEG 可执行文件和库 所以我设法构建了 Android 可执行文件和库 如何在 Eclipse 项目中链接这些库并从 Java 调用
  • 如何在 RTMP 流中嵌入 pic_timing SEI 挂钟时间码?

    我需要将我的桌面流式传输到 AWS MediaLive 服务 并且根据要求 我必须在流中包含挂钟时间码 AWS 支持人员善意地通知我 对于 h 264 编码流 我需要提供时间码作为 pic timing SEI 消息 我在 Windows
  • 如何启用 FFMPEG 日志记录?

    我想调试 ffmpeg 我添加以下代码来打印日志 av log s AV LOG PANIC fmt or printf msg 但这行不通 没有任何调试信息 然后我启用调试构建选项 export COMMON FF CFG FLAGS C
  • FFMPEG:将 YUV 数据转储到 AVFrame 结构中

    我正在尝试转储YUV420数据进入AVFrameFFMPEG 的结构 从下面的链接 http ffmpeg org doxygen trunk structAVFrame html http ffmpeg org doxygen trunk
  • 无法从 webm 视频中提取 webp 中的帧

    我无法从 8K webp 视频中提取帧 我正在使用 ffmpeg 来提取帧 这是我正在使用的命令 ffmpeg i content to extract webm frame 2d webp 我得到的输出文件中没有任何数据 但是 当我导出为
  • ffmpeg h264 问题:在 XP 上“找不到预设文件...”

    我有 XP 因为我不熟悉编译 所以我下载了 ffmpeg 的 win32 静态版本 svn r26251 我想调整 mp4 最初为 1280x720 视频的大小 以获得较小的文件大小 但质量大致相同 我的命令是 ffmpeg i ma mp
  • PowerShell 脚本 ffmpeg

    作为一名优秀的 Windows 系统管理员 我终于开始学习 PowerShell 话虽这么说 我不知道我在做什么 惊讶 惊讶 我认为远离生产环境 在家里使用 PowerShell 对我来说将是一次很好的学习经历 最近 我开始使用 FFMPE

随机推荐

  • java知识点回顾(4):equals方法重写相关知识点

    和equals 区别 是运算符 如果是基本数据类型 则比较存储的值 如果是引用数据类型 则比较所指向对象的地址值 equals是Object类中的方法 所有java类都直接或者间接继承object类 它比较的是所指向的对象的地址值 一般情况
  • NLP扎实基础2:Word2vec模型CBOW Pytorch复现

    Word2vec模型简介请参考 NLP扎实基础1 Word2vec模型Skip Gram Pytorch复现 CBOW模型可以参考论文 Mikolov Tomas et al Efficient estimation of word rep
  • Hadoop Shell常用命令

    Hadoop Shell命令在管理HDFS的时候还是比较常用的 Hadoop Shell命令与shell命令极为相似 但是方便查询 在这里总结分享 大家enjoy 1 cat 语法格式 hadoop fs cat URI URI 含义 将路
  • MySql 之建表语句

    create database book db use book db create table book b number int primary key b name varchar 20 b auther varchar 20 b p
  • 2021年全国省市县行政区划道路水系shp矢量数据(路网:国道省道县道乡道城市一级二级三级四级高速铁路 水系:全国水系一级二级四级五级河流 行政边界:省市县行政区划界线)

    全国电子地图省市县行政区划道路水系shp数据 1 数据来源 地理信息中心 2 时间跨度 无 3 区域范围 全国 4 指标说明 行政边界数据截止2020年12月31日GCS WGS 1984坐标系 路网 水系为2018年版GCS WGS 19
  • JVM学习笔记

    本文部分引自张凯大神的博客 Java虚拟机 JVM 你只要看这一篇就够了 Java 内存区域与内存溢出异常 Java 程序员不需要像C C 开发者一样去为每一个 new 操作去写配对的 delete free 代码 不容易出现内存泄漏和内存
  • 14_Nginx正则表达式_动静分离配置

    文章目录 正则语法说明 pcretest 工具 动静分离 正则语法说明 转义符号 取消元字符的特殊含义 分组与取值 开头 png jpg gif jpeg bmp rewrite reg expr new expr 正则表达式 加 前缀 r
  • AI小项目

    闯红灯检测项目 给定特定场景 检测非机动车是否闯红灯 并形成完整证据链 1 首先检测三种物体 person bicycle motorbike 拟采用Yolov5进行检测 Yolov5在目标检测领域中十分常用 官方Yolov5包括四个版本
  • Qt中MVC模式分析与使用

    Qt中MVC模式实际上是MVD 如下图所示 QListView QTreeView QTableView都用到了MVD模式 Model和View都交由Delegate集中处理 与QListWidget QTreeWidget QTableW
  • 本地音乐如何导入apple_如何将自己的音乐添加到Apple Music

    本地音乐如何导入apple Apple Music has been available to the public for just about a month now and so far the service looks like
  • Python实战项目:20个非常实用的编程练习

    Python实战项目 20个非常实用的编程练习 在学习Python编程语言时 实战项目是必不可少的 在此 我为大家整理了20个非常实用的Python项目 旨在帮助大家更好地学习Python 通过这些实战项目的练习 你可以提高自己的编程水平
  • JS逆向之巨量创意signature签名

    文章目录 目标网站 接口分析 定位 signature生成位置 补环境还原js 编码测试 往期逆向文章推荐 JS逆向之百度翻译 JS逆向解析之有道翻译 JS逆向之企名科技 JS逆向之人口流动态势 js逆向系列之猿人学爬虫第12题 js逆向系
  • 小程序WebSocket详解

    1 什么是WebSocket WebSocket是一种用于在Web浏览器和服务器之间进行双向通信的协议 而小程序WebSocket是在小程序中使用WebSocket协议进行双向数据通信的一种技术 它可以在单个TCP连接上进行全双工通信 实现
  • 国产集成开发环境工具 CEC-IDE

    本周 国内首款适配国产操作系统 自主可控的集成开发环境工具 CEC IDE 终于开放下载了 公开报道显示 这款集成开发环境工具由数字广东公司联合麒麟软件打造 于今年 6 月份首次亮相 本周 软件上线仅几天内就在知乎和 GitHub 上引发了
  • 数据挖掘的研究背景

    数据挖掘是一门研究如何从大量的数据中发现有用的信息和知识的学科 数据挖掘的研究背景可以归纳为以下几点 数据爆炸 随着信息技术的发展 数据的生成速度越来越快 数据量越来越大 人们希望能够从中发现有用的信息和知识 决策支持 数据挖掘可以帮助人们
  • K8S滚动升级

    K8S滚动升级 对于多实例服务 滚动更新采用对各个实例逐批次进行单独更新而非同一时刻对所有实例进行全部更新 来达到不中断服务的更新升级方式 对于Kubernetes集群来说 一个service可能有多个pod 滚动升级 Rolling up
  • Python服务器监测测试策略与工具:确保应用的高可用性!

    在构建高可用性的应用程序时 服务器监测测试是至关重要的一环 Python作为一种强大的编程语言 提供了丰富的工具和库来帮助我们进行服务器监测测试 本文将介绍一些关键的策略和工具 帮助你确保应用的高可用性 1 监测策略的制定 首先 你需要定义
  • 华为OD机试 - 篮球比赛(Java)

    题目描述 篮球 5V5 比赛中 每个球员拥有一个战斗力 每个队伍的所有球员战斗力之和为该队伍的总体战斗力 现有10个球员准备分为两队进行训练赛 教练希望2个队伍的战斗力差值能够尽可能的小 以达到最佳训练效果 给出10个球员的战斗力 如果你是
  • 使用hashcat找回office文档密码

    原文已经发技术栈 Word软件是目前世界上使用最为广泛的办公文字处理软件之一 在国内应该有超过90 的用户在使用它 政府 企业公司以及个人都喜欢用Word文件来处理工作和个人事务 而在使用Word文件来保存文件的内容时 根据不同的安全需要
  • Create a mosaic out of several input videos

    转自 https trac ffmpeg org wiki Create 20a 20mosaic 20out 20of 20several 20input 20videos Overview One of the great featur