如何在不重新编码的情况下合并两个视频 [英] How to Merge two videos without re-encoding

查看:34
本文介绍了如何在不重新编码的情况下合并两个视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并两个视频而无需重新编码.

I am trying to Merge two video without re-encoding them.

目前我使用的方法也非常耗时和资源.我只想合并而不重新编码它们.目前我正在使用

Currently i use a approach which is too much time consuming and resource as well. I just want to merge without re-encoding them. Currently i am using

        exec ( "cpulimit -l 90 ffmpeg -i $filename1 -qscale 0  $intermediate1 &> stream1.log" );
        exec ( "cpulimit -l 90 ffmpeg -i $filename2 -qscale 0  $intermediate2 &> stream2.log" );
        $output = '/var/www/html/myserver/merg/'.uniqid().'_merge.'.$ext;
        exec ( "cpulimit -l 90 cat $intermediate1 $intermediate2 | ffmpeg -i - -qscale 0 $output &> stream3.log" );

以上需要很多时间..我想要一个快速的方法.

Above takes a lot of time.. I want a quick way to do it.

推荐答案

具有相同编解码器的文件的串联:

ffmpeg 中有两种方法可以用来连接同类型的文件:concat demuxer &concat 协议

There are two methods within ffmpeg that can be used to concatenate files of the same type: the concat demuxer & the concat protocol

分路器更灵活——它需要相同的编解码器,但可以使用不同的容器格式;并且它可以与任何容器格式一起使用,而 concat 协议仅适用于少数几个容器.

The demuxer is more flexible – it requires the same codecs, but different container formats can be used; and it can be used with any container formats, while the concat protocol only works with a select few containers.

concat 分离器说明:

创建一个名为 vidlist.txt 的文本文件,格式如下:

create a text file named vidlist.txt in the following format:

file '/path/to/clip1'
file '/path/to/clip2'
file '/path/to/clip3'

请注意,这些可以是相对路径或绝对路径.

Note that these can be either relative or absolute paths.

然后发出命令:

ffmpeg -f concat -safe 0 -i vidlist.txt -c 复制输出

如果不是很清楚,请将输出替换为您希望生成的视频文件名(无论是 output.mp4、output.mkv、output.avi) ffmpeg 将使用 由扩展名指示的容器.

In case it's not abundantly clear, replace output with the video filename you wish to produce (whether that be output.mp4, output.mkv, output.avi) ffmpeg will utilize the container indicated by the extension.

文件将按照它们在 vidlist.txt 中出现的顺序流式复制到输出容器中.复制编解码器"极快.

The files will be stream copied in the order they appear in the vidlist.txt into the output container. the "copy codec" is blazing fast.

请注意,尽管文档说如果路径是相对的,则不需要 -safe 0,但我的测试表明这是必需的.这可能会因您的 ffmpeg 版本而异.

Note that although the docs say you don't need -safe 0 if the paths are relative, my testing indicates it's a requirement. It's possible that this may vary with your version of ffmpeg.

文档中提供了自动生成文件的提示.

注意:所有剪辑必须已经存在,否则命令将失败,因为在读取整个列表之前不会开始解码.

Note: All the clips must already exist or the command will fail because decoding won't start until the whole list is read.

concat 协议说明:

ffmpeg -i "concat:video1.ts|video2.ts|video3.ts" -c 复制 output.ts

注意:如上所述,concat 协议在其支持的流和容器方面受到严重限制,因此我从不使用它.以上内容仅用于尝试创建一个彻底的答案.concat demuxer 是大多数项目的更好选择.

Note: as mentioned above the concat protocol is severely limited in what streams and containers it supports so I never use it. The above is only included in an attempt to create a thorough answer. The concat demuxer is a far better choice for most projects.

另一个建议:我个人更喜欢使用 Matroska 容器,因为它具有灵活性和低开销,并使用 mkvmerge -o output.mkv input1.mkv + input2 加入具有相同编码的视频.mkv

An alternative suggestion: Personally I prefer using the Matroska container due to it's flexibility and low overhead and join videos with the same encoding using mkvmerge -o output.mkv input1.mkv + input2.mkv

具有不同编解码器的文件的串联:

如果您的剪辑没有对音频和视频使用相同的编解码器和/或具有不同的速率,则在加入之前您会卡住重新编码为中间文件,众所周知,这既耗费时间又耗费资源.

If your clips don't use the same codecs for audio and video and/or have different rates, your stuck re-encoding to intermediate files prior to joining which as we all know is both time and resource consuming.

请注意,特殊字符 可能会破坏某些内容,因此如果您在你的文件名中有这些你需要处理它们.

Note that special characters can break things so if you have these in your filenames you'll need to deal with them.

来源:体验

https://ffmpeg.org/ffmpeg-formats.html

这篇关于如何在不重新编码的情况下合并两个视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆