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

Can I convert WebM videos into mp4? #37

Closed nuthinking closed 8 months ago

nuthinking commented 8 months ago

I record videos in WebM using MediaRecorder. I wonder if, perhaps with the combination of VideoDecoder, it would be possible to re-encode them into mp4 file without having to use ffmpeg for the conversion. 🤔

nuthinking commented 8 months ago

Currently looking for an webm-demuxer....

Vanilagy commented 8 months ago

A webm-demuxer would be the cleanest solution, but this should still be somewhat possible even without FFmpeg. I'd spawn a hidden <video> element, and then play that back. Then you can call captureStream, and then create a MediaStreamTrackProcessor:

new MediaStreamTrackProcessor({ track: videoTrack });

Then, similarly to how I do in my demo, you'll get individual VideoFrame instances out! Which is great, because those hold unencoded video data which you can then simply reencode as usual and pipe into mp4-muxer. You'll need to make sure to iron out the details to make sure you don't miss any frames in the input video, and you may need to have some custom timestamp logic, but this should still give you good results! FFmpeg should be the last resort :)

nuthinking commented 8 months ago

So, no way to use MediaStreamTrackProcessor directly with a MediaRecorder? The playback capture is way too hackish :)

Vanilagy commented 8 months ago

Oh yeah wait I read over that. Why don't you record directly into mp4? Or simply skip MediaRecorder altogether?

nuthinking commented 8 months ago

Yeah, if the recording performances of a canvas via WebCodecs are comparable to the ones of MediaRecorder. That would make a lot of sense. I will try!

Vanilagy commented 8 months ago

What are you recording? Just a canvas, right?

nuthinking commented 8 months ago

Yes, only thing I had to handle was that the canvas renders at much higher fps than the video I want to export. Thanks for the idea!