Vanilagy / mp4-muxer

MP4 multiplexer in pure TypeScript with support for WebCodecs API, video & audio.
https://vanilagy.github.io/mp4-muxer/demo
MIT License
419 stars 32 forks source link

Opuslib encoded chunks not support #67

Closed k9p5 closed 1 month ago

k9p5 commented 1 month ago

I've been trying to get the muxer work with an emscripten compiled version of opuslib (https://github.com/xiph/opus.git). Adding the chunks using muxer.addVideoChunkRaw works fine, however when I finalize the muxer I get the error:

"mp4-muxer.js?v=a4bfc09b:511 Uncaught (in promise) RangeError: Offset is outside the bounds of the DataView"

The error is happening here: https://github.com/Vanilagy/mp4-muxer/blob/64fc8091db6fcff71d6edcdf247517e5e892a616/build/mp4-muxer.js#L542

Vanilagy commented 1 month ago

As a sidenote, I've also watched Big Buck Bunny hundreds of times. It's part of the trade!

I see the bug, I'll investigate a fix later. It has to do with the description field in the decoderConfig of the meta parameter to addAudioChunkRaw (I assume you mean that method and not addVideoChunkRaw as you said). This field contains metadata for the Opus decoder to properly initialize. The format is described here: https://www.rfc-editor.org/rfc/rfc7845#section-5 This data is naturally created by the browser when using AudioEncoder, but since you're doing this manually, you'll have to manually provide it.

Are you able to extract this data from opuslib? It should be the header found at the start of the first encoded chunk. If you have access to it, pass it using the description field.

k9p5 commented 1 month ago

I don't even think it's in the hundreds any more 🙈

Thank you so much, seems like the metadata field on the addAudioChunkRaw method is not optional.

Vanilagy commented 1 month ago

It is optional, which is why this error is still a bug that I need to fix in the case where you don't pass anything. But you should be passing something! Let me know when you've managed to fix your issue.

k9p5 commented 1 month ago

I was able to fix the error by providing the full meta object including the description as a opus head buffer. It works perfectly now, thanks again

Vanilagy commented 1 month ago

Probably not relevant to you anymore, but I released an update that fixes the error you got with your initial code.