Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple audio tracks for HTML5 video

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.

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).

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...

Thanks in advance!

like image 339
Emphram Stavanger Avatar asked Oct 24 '11 19:10

Emphram Stavanger


People also ask

Can you add multiple audio tracks in video editor?

You may insert any type of sound on either track, for example sound effects in the music track or music in the narration track. You may also insert more than one sound in either track. If your video clip already contains sound, you will have an additional audio source to mix with these two tracks.

Can YouTube video have multiple audio tracks?

YouTube to begin slowly rolling out multi-audio track and captioning features. YouTube has announced that its multi-audio track feature could be rolling out to everyone in the coming year alongside other accessibility features for the platform.


2 Answers

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.)

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.

If you only needed two languages, you could simply adjust the balance between a left and right stereo audio channel.

like image 71
Brad Avatar answered Oct 21 '22 11:10

Brad


It is doable with 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.

There are some drawbacks.

There is no Web Audio API support in Internet Explorers except for Edge.

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

like image 27
Nek Avatar answered Oct 21 '22 13:10

Nek