FFMpeg 将 AVI 转换为 MP4 - 音频/视频不同步
我有一个应用程序,可以让用户通过网络摄像头录制视频。用户录制 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事:
-strictexperimental
开关。由于您遇到了音频同步问题,因此这是一个明显的候选问题区域。如果可用,请考虑使用 libfaac。 (您很可能必须构建自己的 FFmpeg 编译。)-threads 64
并不会让它运行得更快,无论您有多少个内核。遵循普遍接受的约定:# cores * 3 / 2。因此,如果您有 4 个核心,则桅杆为4 * 3 / 2 = 6
。我自己对-threads
的实验显示,每个线程超过 4-6 个时,回报率会递减,超过 12-16 个线程时,回报率会为零(没有任何好处)。充其量,64 个线程不会产生任何影响。在最坏的情况下,它会变慢或降低结果的质量。-copyts
选项。 “将时间戳从输入复制到输出。”-async
开关。A couple of things:
-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.)-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 is4 * 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.-copyts
option. "Copy timestamps from input to output."-async
switch.我在 avi 或 dv 上也遇到了类似的音频同步问题 --> Linux 下使用 mencoder 制作 mp4。
简单的解决方案是首先提取音频,然后将 avi 转换为 mp4,最后一步将两个轨道混合在一起。可以使用 shellscript avi2mp4.sh 来完成,如下所示:
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: