javascript HTML5 视频的多个音轨
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7880878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple audio tracks for HTML5 video
提问by Emphram Stavanger
I'm building a video for my website with HTML5. Ideally, I'd have only one silent video file, and five different audio tracks in different languages that sync up with the video.
我正在使用 HTML5 为我的网站制作视频。理想情况下,我只有一个无声视频文件,以及与视频同步的五个不同语言的不同音轨。
Then I'd have a button that allows users to switch between audio tracks, even as the video is playing; and the correct audio track would come to life (without the video pausing or starting over or anything; much like a DVD audio track selection).
然后我有一个按钮,允许用户在音轨之间切换,即使视频正在播放;并且正确的音轨会栩栩如生(没有视频暂停或重新开始或任何事情;就像 DVD 音轨选择一样)。
I can do this quite simply in Flash, but I don't want to. There has to be a way to do this in pure HTML5 or HTML5+jQuery. I'm thinking you'd play all the audio files at 0 volume, and only increase the volume of the active track... but I don't know how to even do that, let alone handle it when the user pauses or rewinds the video...
我可以在 Flash 中很简单地做到这一点,但我不想这样做。必须有一种方法可以在纯 HTML5 或 HTML5+jQuery 中做到这一点。我想你会以 0 音量播放所有音频文件,只增加活动曲目的音量......但我什至不知道如何做到这一点,更不用说在用户暂停或倒带时处理它了该视频...
Thanks in advance!
提前致谢!
采纳答案by derobert
If you're willing to let all five tracks download, why not just mux them into the video? Videos are not limited to a single audio track (even AVI could do multiple audio tracks). Then syncing should be handled for you. You'd just enable/disable the audio tracks as needed.
如果您愿意让所有五首曲目都下载,为什么不将它们混入视频中呢?视频不限于单个音轨(即使 AVI 也可以做多个音轨)。然后应该为您处理同步。您只需根据需要启用/禁用音轨。
回答by Brad
Synchronization between audio and video is far more complex than simply starting the audio and video at the same time. Sound cards will playback at slightly different rates. (What is 44.1 kHz to me, might actually be 44.095 kHz to you.)
音频和视频之间的同步远比简单地同时启动音频和视频复杂得多。声卡将以略有不同的速率播放。(对我来说 44.1 kHz 是什么,对你来说实际上可能是 44.095 kHz。)
Often, the video is synchronized to the audio stream, but the player is what handles this. By loading up multiple objects for playback, you are effectively pulling them out of sync.
通常,视频与音频流同步,但播放器是处理这个的。通过加载多个对象进行播放,您可以有效地使它们不同步。
The only way this is going to work is if you can find a way to control the different audio streams from the video player. I don't know if this is possible.
唯一可行的方法是您是否可以找到一种方法来控制来自视频播放器的不同音频流。我不知道这是否可能。
Instead, I propose that you encode the video multiple times, with the different streams. You can use FFMPEG for this, and even automate the process, depending on your workflow. Switching among streams becomes a problem, but most video players are robust enough to guess the byte offset in the file, when given the bitrate.
相反,我建议您使用不同的流对视频进行多次编码。您可以为此使用 FFMPEG,甚至可以根据您的工作流程自动化该过程。在流之间切换成为一个问题,但大多数视频播放器足够强大,可以在给定比特率时猜测文件中的字节偏移量。
If you only needed two languages, you could simply adjust the balance between a left and right stereo audio channel.
如果您只需要两种语言,您可以简单地调整左右立体声音频通道之间的平衡。
回答by Nek
It is doable with Web Audio API.
它可以通过 Web Audio API 实现。
Part of my program listens to video element events and stops or restarts audio tracks created using web audio API. This gives me an ability to turn on and off any of the tracks in perfect sync.
我的程序的一部分侦听视频元素事件并停止或重新启动使用网络音频 API 创建的音轨。这使我能够以完美同步的方式打开和关闭任何曲目。
There are some drawbacks.
有一些缺点。
There is no Web Audio API support in Internet Explorers except for Edge.
除 Edge 外,Internet Explorer 中不支持 Web Audio API。
The technique works with buffered audio only and that's limiting. There are some problems with large files: https://bugs.chromium.org/p/chromium/issues/detail?id=71704
该技术仅适用于缓冲音频,这是有限制的。大文件有一些问题:https: //bugs.chromium.org/p/chromium/issues/detail?id=71704