video_normalizer_web 0.1.2
video_normalizer_web: ^0.1.2 copied to clipboard
Web implementation of the video_normalizer plugin for Chromium-based browsers, built on WebCodecs inside a dedicated Web Worker.
video_normalizer_web #
The web implementation of video_normalizer, for
Chromium-based browsers. Apps depend on video_normalizer; this package is
endorsed and comes with it.
What it is made of #
| Decoding and encoding | the browser's WebCodecs (VideoDecoder, VideoEncoder, AudioDecoder, AudioEncoder) |
| Reading and writing containers | mediabunny 1.66.5 (MPL-2.0), bundled into the worker |
| Where the work happens | one dedicated Web Worker per request; the page only posts messages |
| Dart side | package:web and dart:js_interop; no dart:html, no dart:js |
| Not used | FFmpeg, WebAssembly, any server |
| Size added to an app | assets/video_normalizer_worker.js, about 585 kB (minified, before the web server's compression) |
Inputs are VideoInput.blob and VideoInput.webFile. The output is a Blob
(result.output.blob); there is no path on the web.
final result = await VideoNormalizer.instance.normalize(
VideoNormalizationRequest(input: VideoInput.webFile(file)),
);
final blob = result.output!.blob!; // upload this
final url = result.output!.objectUrl(); // or play it: <video src=url>
await result.output!.download('lesson.mp4'); // or save it
await result.output!.release(); // revokes the URL, drops the blob
What a browser must have #
normalize() fails with BrowserCapabilityError (browser_capability_missing)
before anything is read when one of these is missing; error.missing names
them:
- a secure context (HTTPS, or
localhostduring development); Worker,VideoDecoder,VideoEncoder,AudioEncoder;- an encoder that accepts H.264 Main Level 3.1 (
avc1.4D001F); - an encoder that accepts AAC-LC (
mp4a.40.2).
The check is made on features, not on the browser's name. Only Chromium has
been tested; see docs/WEB_BROWSERS.md for what
was verified, where, and what is known not to work.
How a job runs #
- Analysis (
worker/src/analyze.ts). The sourceBlobis read in slices. A box scanner, a port of the Android one, reads the MP4 structure; mediabunny reads the tracks. Nothing is decoded. - Checks before any decoding (
pipeline.ts): the source's codecs withcanDecode(), the output configuration withVideoEncoder.isConfigSupported()andAudioEncoder.isConfigSupported(). Nothing is substituted; a refusal is a typed error. - Transcode. Decoded frames go straight to the encoder when they are
already upright, of the output size and BT.709. Otherwise they are drawn
to an
OffscreenCanvas, which scales, rotates and converts colour (including HDR to SDR). EveryVideoFrameandAudioDatais closed as soon as it has been used, and the worker counts them. Audio that is already AAC-LC is copied; other audio is decoded and encoded to AAC-LC. - Remux. Packets are copied; nothing is decoded.
- Fast start (
faststart.ts). The muxer writes the index last. ABlobcan be recomposed from slices without copying, so the index is moved to the front by shifting its chunk offsets:[ftyp][moov][mdat]. - Validation. The worker reads the result back;
OutputValidator(Dart) compares it with what was asked for. Only then is theBlobreturned.
The encoder is configured with latencyMode: 'quality',
hardwareAcceleration: 'no-preference', avc: { format: 'avc' } and an
explicit bitrate and framerate.
The worker is terminated when its request has been answered, has failed or was cancelled. Termination releases every codec, frame and buffer it held.
Hosting #
- The worker script is a package asset. Flutter serves it at
assets/packages/video_normalizer_web/assets/video_normalizer_worker.js. - A worker must come from the page's own origin. An app that serves its
assets from another origin (a CDN) sets
VideoNormalizerWeb.workerScriptUrlOverrideto a same-origin copy. - A Content Security Policy must allow the script:
worker-src 'self'(orscript-src 'self'whereworker-srcis not set), andblob:inmedia-srcif the app plays the output throughobjectUrl(). - Cross-origin isolation (COOP/COEP) is not needed: no
SharedArrayBufferis used.
Diagnostics #
VideoNormalizerWeb.activeWorkerCount is the number of live workers.
(VideoNormalizerPlatform.instance as VideoNormalizerWeb).lastJobReport
holds the worker's counters for the last job: frames decoded, encoded and
dropped, frames left open (always 0), whether the canvas was used, and the
encoder configurations.
Working on the worker #
The TypeScript sources are in worker/. The bundle in assets/ is checked
in, so that using the package needs neither Node.js nor npm.
cd worker
npm ci # the exact versions of package-lock.json
npm run check # tsc --noEmit
npm test # node --test, 11 tests of the pure modules
npm run build # esbuild -> ../assets/video_normalizer_worker.js
Dart tests: flutter test (pure parts, on the VM) and
flutter test --platform chrome (adds the tests that need a browser).
Integration tests run from the example app; see
docs/WEB_BROWSERS.md.
Third-party licences: THIRD_PARTY_NOTICES.md.