Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proof of Concept] Video Export using Web Codec API #178

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

lucasdinonolte
Copy link
Contributor

@lucasdinonolte lucasdinonolte commented Jun 5, 2023

This PR explores a way to use the Web Codec API to export video. This is not only faster than using the JS implementation we’re currently using, but also opens up more export formats (including H.264 codec in MP4 container).

This is in the early stages of exploring. Video formats, codecs and container are a fairly complex topic and I feel like I'm just starting to understand.

What this does

  • Adds VideoWriter class to mechanic core, replacing the previous WebmWriter
    • uses Web Codec API to export video to MP4 (using H.264 Codec) or WebM (using VP9 Codec)
    • falls back to the pure JS WebmWriter for browsers that don’t support web codec API
    • errors for non-supported browsers if a user wants to export MP4 (as their currently is no JS fallback for the H.264 codec)
  • Allows animated design function to specify their animationFormat to set the desired export
    • allowed values webm and mp4 (defaults to webm, as we have a fallback for non-supported browsers)
  • Allows animated design functions to specify their desired animationBitrate for export
    • If non is set (default) mechanic will calculate a high quality bitrate from width, height and framerate

What could be done

  • Add video export presets
    • Right now it only provides the options mp4 and webm
    • ... but this could easily be extended to provide presets like "High Quality MP4", "Small File Size Mp4", etc.
  • Discuss fallbacks for non-supported browsers
    • Web Codec API is still a working draft and browser support outside of Chrome is not looking too good
    • For webm video we can fallback to the WebmWriter that was used previously
    • For mp4 video we could investigate some of the WASM based H.264 encoders
  • Discuss if we want the export format as an option on the design function or if we should let the end user choose the export format

Test Driving

  • fillAnimatedCanvas is set up to export MP4 video

There’s probably a million more things to discuss that I can't even begin to think of, as video-formats, containers, and codecs are quite the rabbit hole. I tried to leave extensive code comments to document what is going on and why certain decisions were taken. But please challenge everything I've done 😊

Caveats

Not all browsers come with full support for the web codec API, Firefox for example has no support for the Web Codec API currently at all. But there's also differences in Chromium based browsers: Chromium (as it's used on Linux or in headless scenarios) only comes with built-in support for open codecs and lacks proprietary codecs. This means no h.264 in Chromium. Google Chrome however has full support including proprietary codecs.

However, when using Chrome, the licensing of h.264 is covered by Chrome's license and we’re good to use it.

Alternatives

I did a test drive using ffmpeg-wasm locally and it works across browsers (including headless Chromium), so this could be a feasible alternative to relying on the Web Codec API. However ffmpeg-wasm comes at the cost of downloading ~25MB of WebAssembly code to the user's browser first.

As h.264 is a proprietary codec there is some confusion around whether we'd be allowed to embed it (in the form of ffmpeg-wasm). There is some indication that we might don’t want to do this (or that we could do it, but need to inform our users about this, so they don’t get into trouble).

For references see:

@netlify
Copy link

netlify bot commented Jun 5, 2023

Deploy Preview for dsi-logo-maker ready!

Name Link
🔨 Latest commit 084be95
🔍 Latest deploy log https://app.netlify.com/sites/dsi-logo-maker/deploys/647f6fa7ab337f00084cae81
😎 Deploy Preview https://deploy-preview-178--dsi-logo-maker.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@lucasdinonolte lucasdinonolte changed the title Video Export using Web Codec API [Proof of Conept] Video Export using Web Codec API Jun 5, 2023
@lucasdinonolte lucasdinonolte changed the title [Proof of Conept] Video Export using Web Codec API [Proof of Concept] Video Export using Web Codec API Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant