Search code examples
javascriptnode.jsexpressmp4

Nodejs stream video (mp4) and switch audio tracks


I want to play a video in the browser via nodejs which is on my PC. I do this via express and use this code:

https://medium.com/better-programming/video-stream-with-node-js-and-html5-320b3191a6b6 (Scroll to "The Server").

This is exactly what I need but I want to change the audio track. The mp4 file I am accessing has multiple internal audio tracks (multiple languages). As you can see here you can change the audio track with the VLC Media Player. And as the vlc media player can do this i would implement this with nodejs.

Thanks for help!

vlc media player


Solution

  • You cited some sample server code that delivers a stream containing a .mp4 object. There's nothing much you can do server-side to change the language.

    But you can use client-side Javascript to enumerate the audio tracks in the stream.

    This code, for example, sets track.enabled to true for the track with the German language, and track.enabled to false for the rest. That mutes all audio except the German-language track.

    const videoElement = document.getElementById('videoplayer')
    for (let i = 0; i < videoElement.audioTracks.length; i++) {
      const track = videoElement.audioTracks[i]
      track.enabled = (track.language === 'de-DE')
    }
    

    But you should be careful with this crummy example code; it mutes the entire stream if there's no German track.