phoboslab / jsmpeg

MPEG1 Video Decoder in JavaScript
MIT License
6.3k stars 1.43k forks source link

Audio subtitles support? #431

Closed bil-ash closed 1 month ago

bil-ash commented 2 months ago

Does this library support audio subtitle selection and playing? Basically, in a video I want the user to be able to select one out of the two audio languages and play audio for that language only. Does this library have that capability? If yes, @phoboslab please provide the code for selection.

phoboslab commented 1 month ago

No. There are few parts missing for that:

As far as I know, subtitles are usually carried in a private stream. You can ask the demuxer to forward these packets to your subtitle decoder: player.demuxer.connect(JSMpeg.Demuxer.TS.STREAM.PRIVATE_1, yourSubtitleDecoderInstance);

Also, you may want to decode the Program Stream Map, to figure out what stream is what (i.e. which of the audio streams is what language and what subtitles are present). Again, you can ask the demuxer to forward PSM packets (JSMpeg.Demuxer.TS.STREAM.PROGRAM_MAP), but there's no decoder for it yet.

The decoded subs could either be rendered directly into the canvas (not that easy for WebGL contexts) or layered on top as a simple HTML element.

PRs welcome!

bil-ash commented 1 month ago

I was talking about audio subtitles(AUDIO_1,AUDIO_2, etc ) only and not text subtitle. From https://github.com/phoboslab/jsmpeg/blob/924acfbd96fdf15e6748d1368a36d79d8f4cecf6/src/ts.js#L219 , I can understand that AUDIO_1 maps to 0xC0. How to understand AUDIO_2, AUDIO_3, etc maps to what?

phoboslab commented 1 month ago

You are talking about audio streams then :)

AUDIO_2 would be 0xC1.

The MPEG Standard defines anything between 0xC0 and 0xDF (inclusive) to be possible audio streams (see http://dvdnav.mplayerhq.hu/dvdinfo/pes-hdr.html), so in theory your 2nd audio stream could have any of those IDs. Though sensible encoders will put the 2nd stream in 0xC1, 3rd in 0xC2 etc.

bil-ash commented 1 month ago

Thanks for the info