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 的相关文章

  • 如何使用 ffmpeg 将两个视频/音频流混合为一个

    我有两个视频 v1 flv 和 v2 flv 想要创建 v3 flv 其中包含来自 v1 flv 的视频流以及来自 v1 flv 和 v2 flv 的 混合 音频流 使用 ffmpeg 命令可以实现类似的操作吗 谢谢 我认为使用 ffmpe
  • 如何让 Python 找到 ffprobe?

    I have ffmpeg and ffprobe安装在我的 mac macOS Sierra 上 并且我已将它们的路径添加到 PATH 中 我可以从终端运行它们 我正在尝试使用ffprobe使用以下代码获取视频文件的宽度和高度 impor
  • 转换为 JPEG 时 HEIC 切片损坏

    我在将 HEIC 图像转换为 jpeg 时遇到问题 HEIC 文件是使用运行最新 iOS 公共测试版的 iPhone 拍摄的图像 我正在使用诺基亚提供的库 https github com nokiatech heif 要解析文件并从 HE
  • 如何在服务器上使用 ffmpeg 从 WebRTC 流获取音频和视频

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

    我正在尝试转储YUV420数据进入AVFrameFFMPEG 的结构 从下面的链接 http ffmpeg org doxygen trunk structAVFrame html http ffmpeg org doxygen trunk
  • id3 图像编辑后播放 mp3 时遇到问题

    由于硬件限制 我们生产的软件试图确保导入到其库中的任何音频文件 准备复制到硬件上 都是可接受的比特率 最近 我们开始使用 FFmpeg 将许多不同的音频类型转换为 mp3 以便在我们的硬件上导入和使用它们 虽然转换工作正常并且 mp3 文件
  • FFmpeg 缩放不是平滑中心(而是锯齿形)

    我尝试执行基本操作zoompan https www ffmpeg org ffmpeg all html zoompan with FFmpeg 我有一个输入图像 png 1280x720 并从中创建一个 8 秒的视频 mp4 320x1
  • 如何从 ffmpeg 中打开的文件获取流信息?

    我正在尝试使用 ffmpeg 读取视频文件 我有与其旧版本相对应的工作代码 并开始尝试升级到最新的构建版本 将所有这些已弃用的函数替换为其实际的类似函数 但是我遇到了问题 似乎没有检索到任何流 并且视频负载停止在轨道中 这是我正在使用的代码
  • FFMPEG Seeking 带来音频伪影

    我正在使用 ffmpeg 实现音频解码器 在读取音频甚至搜索已经可以工作时 我无法找到一种在搜索后清除缓冲区的方法 因此当应用程序在搜索后立即开始读取音频时 我没有任何工件 avcodec flush buffers似乎对内部缓冲区没有任何
  • 有没有更有效的方法通过ffmpeg批量添加水印和加入视频?

    我有这个批处理文件 使用 ffmpeg 在我的视频中添加徽标 然后添加简介 但需要 10 小时到一天的时间 具体取决于我需要添加水印的数量 是否有更有效的方法来实现此目的 视频有时具有不同的分辨率 因此我无法删除到 1280 720 尺寸的
  • 视频文件转换/转码 Google App Engine

    我想启动一个云计算项目 其简单任务是 接收上传的视频文件 对它们进行一些转码 转换 允许用户下载 流式传输生成的文件 我刚在想ffmpeg作为集成在的外部命令行工具Java Google App engine Application 由于很
  • 使用 ffmpeg 库以可变帧率模式将一系列图像(cv::Mat)保存到 mp4 文件,如何设置 pts?

    在C 代码中 我可以正确保存一系列图像 opencv的cv Mat 到 mp4 文件 使用ffmpeg图书馆 请参阅此处的问题和答案 当我尝试将多个 RGB 数据保存到 output mp4 文件时 avformat write heade
  • C#中图像制作视频的工作方式

    有人有已知的可靠方法来从一系列图像文件创建视频吗 在你因为我在发布问题之前没有寻找答案而对我进行批评之前 以及在你发出诸如 使用 FFMPEG 之类的简单消息之前 请阅读此消息的其余部分 我正在尝试从一系列图像 jpg bmp 等 创建视频
  • ffprobe show_frames 用于多个视频

    有什么方法可以在一个文件中同时查看多个视频的帧吗 我知道如何在一个视频中做到这一点 ffprobe show frameshttp myvirtualdirectory myvideo mp4 http myvirtualdirectory
  • 使用 ImageMagick 有效地将线扫描图像拼接在一起

    我正在寻找线扫描相机的替代品 用于体育计时 或者更确切地说 用于需要确定位置的部分 我发现普通工业相机可以轻松与商业相机解决方案的速度相匹配 每秒 gt 1000 帧 对于我的需求来说 通常计时的准确性并不重要 重要的是运动员的相对位置 我
  • 尽管重新采样音频过滤器,FFmpeg 对 ts 和 m3u8 文件的切割不准确

    我需要准确地寻找并剪切视频 一些在线资源说将 ss 放在源的前面或后面 结果对我来说是一样的 在下面的示例中 开始时间准确 但持续时间不准确 ffmpeg y ss 00 00 05 t 00 00 05 i output ts 5s wa
  • FFmpeg 代码无法在用于缩略图提取的 http url 上工作

    我正在尝试从 sharepoint 2013 视频库中提取缩略图 我找到了一个可以使用 ffmpeg 提取的链接 这是链接 如何将视频的第一帧保存为图像 https stackoverflow com questions 3575311 h
  • 使用 libavcodec 提取音频样本

    我对如何从 AVFrame 中的数据提取双值感到困惑 我正在尝试提取帧 我尝试检查用 CPython 编写的 av 模块背后的源代码 尤其是 AudioFrame 来尝试了解它从何处解码样本 https github com PyAV Or
  • 为什么opencv videowriter这么慢?

    你好 stackoverflow 社区 我有一个棘手的问题 我需要你的帮助来了解这里发生了什么 我的程序从视频采集卡 Blackmagic 捕获帧 到目前为止 它工作得很好 同时我用 opencv cv imshow 显示捕获的图像 它也工
  • 无法将 .ogg 文件转换为 .mp3 或其他文件格式

    我正在尝试将 ogg 音频文件转换为 mp3 或其他可以在 ios 设备中播放的音频文件格式 但 ogg 文件没有被转换为其他格式 如 mp3 和 caf 我正在 Android 设备中测试转换 这是我的 ffmpeg 命令参数 Comma

随机推荐

  • 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