FFMpeg 将 AVI 转换为 MP4 - 音频/视频不同步

发布于 2024-10-20 07:38:14 字数 1379 浏览 0 评论 0原文

我有一个应用程序,可以让用户通过网络摄像头录制视频。用户录制 3 个剪辑,然后将其组合成一个视频进行播放。

我遇到了第三个剪辑的音频和视频在转换为 MP4 后有时不同步的问题。

现在该过程的工作原理如下 -

使用 Mencoder 组合 AVI 视频文件并在两者之间插入过渡视频来组合 avi 文件。

mencoder.exe -oac copy -ovc copy -idx -o test.avi c:\temp\*.avi 

我收到此错误:

Muxer frame buffer cannot allocate memory!

1 duplicate frame(s)! 
1 duplicate frame(s)! 
1 duplicate frame(s)!
1 duplicate frame(s)! 
1 duplicate frame(s)! 
1 duplicate frame(s)!

但是,视频已组合为 avi,并且此 AVI 会在不进行播放的情况下进行播放任何音频同步问题。

使用 mencoder 重新索引

mencoder.exe -idx TEST.AVI 

此时视频仍可以正常播放 AVI 格式。

使用 ffmpeg 批处理文件将 AVI 转换为 MP4:

set FFMPEG_DATADIR=c:\Presets
ffmpeg.exe -i "test.avi" -s 640x480 -y -strict experimental -acodec aac \
   -ab 128k -ac 2 -ar 48000 -vcodec libx264 -vpre medium -vpre ipod640 -r 24 \
   -g 48 -b 520000 -threads 64 "out.mp4"

这一切都必须使用 Windows 中的命令行工具以编程方式完成(无视频编辑 GUI 软件)。

以下是 avi 和 mp4 文件示例。音频/视频同步发生在 1:03 左右。 AVI 不会不同步,而 mp4 则会。

http://trtemp.s3.amazonaws.com/new.avi

http://trtemp.s3.amazonaws.com/new.mp4

有没有人有任何建议来解决此问题?

I have an application that lets users record video from their webcams. The users record 3 clips, which are then combined into one video for playback.

I am having issues with the audio and video on the third clip sometimes being out of sync after converting to MP4.

Here's how the process works now-

Combine AVI video files and insert transition videos in between using Mencoder to combine avi files.

mencoder.exe -oac copy -ovc copy -idx -o test.avi c:\temp\*.avi 

I get this error:

Muxer frame buffer cannot allocate memory!

1 duplicate frame(s)! 
1 duplicate frame(s)! 
1 duplicate frame(s)!
1 duplicate frame(s)! 
1 duplicate frame(s)! 
1 duplicate frame(s)!

However, the video is combined to avi and this AVI plays back without any audio sync issues.

Reindex using mencoder

mencoder.exe -idx TEST.AVI 

The video still plays fine at this point as an AVI.

Convert the AVI to MP4 using ffmpeg batch file:

set FFMPEG_DATADIR=c:\Presets
ffmpeg.exe -i "test.avi" -s 640x480 -y -strict experimental -acodec aac \
   -ab 128k -ac 2 -ar 48000 -vcodec libx264 -vpre medium -vpre ipod640 -r 24 \
   -g 48 -b 520000 -threads 64 "out.mp4"

This all has to be done programmatically (no video editing GUI software) using command line tools in Windows.

Here are sample avi and mp4 files. The audio / video sync happens around the 1:03 mark. AVI doesn't get out of sync, while the mp4 does.

http://trtemp.s3.amazonaws.com/new.avi

http://trtemp.s3.amazonaws.com/new.mp4

Does anyone have any suggestions to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-10-27 07:38:14

有几件事:

  • 我认为源复合音频同步并不完全完美。看到原始的三个源剪辑也会很有趣。一般规则是始终从尽可能最高质量的来源开始。
  • 我们是否可以假设源三个剪辑也具有 48kHz 16 位立体声音频?如果那里正在进行转换,那么这可能是一个问题。
  • FFmpeg 的内部 aac 编解码器是实验性的,这就是为什么您必须使用 -strictexperimental 开关。由于您遇到了音频同步问题,因此这是一个明显的候选问题区域。如果可用,请考虑使用 libfaac。 (您很可能必须构建自己的 FFmpeg 编译。)
  • 使用 -threads 64 并不会让它运行得更快,无论您有多少个内核。遵循普遍接受的约定:# cores * 3 / 2。因此,如果您有 4 个核心,则桅杆为 4 * 3 / 2 = 6。我自己对 -threads 的实验显示,每个线程超过 4-6 个时,回报率会递减,超过 12-16 个线程时,回报率会为零(没有任何好处)。充其量,64 个线程不会产生任何影响。在最坏的情况下,它会变慢或降低结果的质量。
  • 尝试使用 -copyts 选项。 “将时间戳从输入复制到输出。”
  • 使用 -async 开关。

音频同步方法。
“拉伸/挤压”音频流
为了匹配时间戳,参数
是每秒的最大样本数
音频被改变。 -异步1
是一种特殊情况,只有开始
音频流的修正
没有任何后续更正。

A couple of things:

  • I don't find the source composite audio sync to be exactly perfect. It would be interesting to also see the original three source clips. A general rule is to always start with the highest quality source possible.
  • Can we assume the source three clips also have 48kHz 16-bit stereo audio too? If there is a conversion going on there then maybe this is an issue.
  • FFmpeg's internal aac CODEC is experimental, and that is why you have to have that -strict experimental switch. Since you are having audio sync issues, this is an obvious candidate problem area. Consider using libfaac if available. (You very well may have to build your own FFmpeg compile.)
  • Using -threads 64 is not going to make it go faster no matter how many cores you have. Follow the generally accepted convention of # cores * 3 / 2. So, if you have 4 cores, the mast is 4 * 3 / 2 = 6. My own experiments with the -threads shows diminishing rates of return with each thread over 4-6 which zeros out (no benefit whatsoever) over 12-16 threads. At best, 64 threads will no have no impact. At worst, it will be slower or degrading the quality of your result.
  • Experiment with the -copyts option. "Copy timestamps from input to output."
  • Experiment with the -async switch.

Audio sync method.
"Stretches/squeezes" the audio stream
to match the timestamps, the parameter
is the maximum samples per second by
which the audio is changed. -async 1
is a special case where only the start
of the audio stream is corrected
without any later correction.

风情万种。 2024-10-27 07:38:14

我在 avi 或 dv 上也遇到了类似的音频同步问题 --> Linux 下使用 mencoder 制作 mp4。
简单的解决方案是首先提取音频,然后将 avi 转换为 mp4,最后一步将两个轨道混合在一起。可以使用 shellscript avi2mp4.sh 来完成,如下所示:

#!/bin/bash
# convert avi files to mp4 with correct sync of video and audio.
# usage: avi2mp4 <sourcefile> <targetfile>
audiopart=$2.audiotmp.mp3
videopart=$2.videotmp.mp4
mencoder -of rawaudio -oac lavc -lavcopts acodec=mp2:abitrate=128 -ovc copy -o $audiopart $1 
mencoder  -oac mp3lame -ovc x264 -vf yadif=0:harddup -o $videopart  $1 
mencoder -oac copy -ovc copy -audiofile $audiofile -o $2 $videopart
rm $audiopart
rm $videopart

I had similar audio sync problems with avi or dv --> mp4 with mencoder under linux.
Simple solution was to extract audio first, then convert avi to mp4 and as last step mux both tracks together. could be done with a shellscript avi2mp4.sh like this:

#!/bin/bash
# convert avi files to mp4 with correct sync of video and audio.
# usage: avi2mp4 <sourcefile> <targetfile>
audiopart=$2.audiotmp.mp3
videopart=$2.videotmp.mp4
mencoder -of rawaudio -oac lavc -lavcopts acodec=mp2:abitrate=128 -ovc copy -o $audiopart $1 
mencoder  -oac mp3lame -ovc x264 -vf yadif=0:harddup -o $videopart  $1 
mencoder -oac copy -ovc copy -audiofile $audiofile -o $2 $videopart
rm $audiopart
rm $videopart

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文