如何合并两个音频文件,同时使用ffmpeg保留正确的时间 [英] How to merge two audio files while retaining correct timings with ffmpeg

查看:1065
本文介绍了如何合并两个音频文件,同时使用ffmpeg保留正确的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我不了解ffmpeg,而且大多只是复制命令

Disclaimer: I don't understand ffmpeg and mostly just copy commands

我有两个webm视频文件.它们的持续时间缩短了大约一秒钟.

I have two webm video files. Their duration is off by about a second.

我将它们转换为mp4,一切都很好:

I converted these to mp4 and everything was fine:

ffmpeg -acodec libopus -i 1.webm -r 10 -cpu-used 5 -c:v libx264 -crf 20 -c:a aac -strict experimental -loglevel error 1.mp4

然后我从每个视频中提取了音频,一切都很好.好的,我的意思是,当我自己播放音频文件时,它们与原始视频的时间匹配.我使用以下命令提取音频:

I then extracted the audio from each video and everything was fine. By fine, I mean, when I playback the audio files by themselves they match the timings on the original video. I used this command to extract the audio:

fmpeg -i 1.mp4 -map 0:a -vn -acodec copy 1audio.m4a

我现在有两个音轨,我想将它们合并为一个,我想覆盖"它们,并且我想保持时序与它们相同.

I now have two audio tracks and I want to combine them into one, I want to "overlay" them, AND I want to keep the timings the same as they were.

但是,每当我尝试合并音频时,定时就会关闭. 秒"与原始单个音频文件不匹配.

BUT, whenever I try and combine the audio the timings are off. The "seconds" don't match the original single audio files.

我尝试了以下命令来合并音频:

I have tried these commands to merge the audio:

ffmpeg -i 1audio.m4a -i 2audio.m4a -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map [a] -c:a libfdk_aac mergedaudio.m4a

ffmpeg -i 2audio.m4a -i 1audio.m4a -filter_complex amix=inputs=2:duration=first mergedaudio.m4a

ffmpeg -i 1audio.m4a -i 2audio.m4a -filter_complex amerge -ac 2 -c:a libfdk_aac -vbr 4 mergedaudio.m4a

以上所有命令均导致合并了音频文件...但计时已关闭.

All the above command have resulted in a merged audio file... but the timings are off.

我在做什么错了?

推荐答案

我发现了问题,这个问题有缺陷,但值得回答.

I found the issue and the question is flawed but worth answering.

问题在于,在合并之前,两个音频文件的开始时间戳> 0.

The issue is, before merging, the two audio files had start timestamps > 0.

但是,当文件合并时,它们被置零",因此延迟的启动时间被忽略,音频开始得太早.

But when the files got merged, they got "zeroed", thus their delayed start times were ignored and the audio started too early.

然后的解决方案是再次对音频进行编码,但是这次添加等于开始延迟的延迟,以便在将其清零时获得所需的填充.

The solution then is to encode the audio again, but this time add a delay equal to the start delay so that when it gets zeroed you have the padding you need.

因此,让我们再次尝试一下:

So lets run through that again:

  1. 您从两个acc文件开始,无论出于何种原因,它们的开始时间分别为3秒和5秒.

  1. You start with two acc files which for whatever reason have start times of 3 and 5 seconds respectively.

接下来,您将每个文件归零":

Next you "zero" each file:

ffmpeg -i 1audio.m4a -filter_complex "[0:a]adelay=3000[a0];[a0]amix=inputs=1[out]" -map "[out]" -ac 2 -c:a libfdk_aac 1zeroed.m4a

ffmpeg -i 2audio.m4a -filter_complex "[0:a]adelay=5000[a0];[a0]amix=inputs=1[out]" -map "[out]" -ac 2 -c:a libfdk_aac 2zeroed.m4a

现在,新的音频文件1zeroed和2zeroed的开始时间为0,但是在编码填充过程中添加了音频,因此每次播放时,音频仅在应有的情况下开始.

The new audio files, 1zeroed and 2zeroed now have a 0 start time, but during the encoding padding was added so when each play, the audio only starts when it's supposed to.

现在,当您合并两个文件时,一切都变得非常酷! :-)

Now when you merge the two files, everything is super cool! :-)

ffmpeg -i 1zeroed.m4a -i 2zeroed.m4a -filter_complex amerge -ac 2 -c:a libfdk_aac -vbr 4 mergedaudio.m4a

这篇关于如何合并两个音频文件,同时使用ffmpeg保留正确的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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