如何合并两个视频而无需重新编码 [英] How to Merge two videos without re-encoding

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

问题描述

我正在尝试合并两个视频,而不对它们重新编码.

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多路分配器& 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 copy output

如果不清楚,请用您要产生的视频文件名替换输出(无论是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 copy output.ts

注意:如上所述,concat协议在支持的流和容器方面受到严格限制,因此我从不使用它.以上内容仅包含在尝试创建完整答案的尝试中.对于大多数项目而言,concat多路分配器是一个更好的选择.

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天全站免登陆