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 localhost during 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

  1. Analysis (worker/src/analyze.ts). The source Blob is read in slices. A box scanner, a port of the Android one, reads the MP4 structure; mediabunny reads the tracks. Nothing is decoded.
  2. Checks before any decoding (pipeline.ts): the source's codecs with canDecode(), the output configuration with VideoEncoder.isConfigSupported() and AudioEncoder.isConfigSupported(). Nothing is substituted; a refusal is a typed error.
  3. 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). Every VideoFrame and AudioData is 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.
  4. Remux. Packets are copied; nothing is decoded.
  5. Fast start (faststart.ts). The muxer writes the index last. A Blob can be recomposed from slices without copying, so the index is moved to the front by shifting its chunk offsets: [ftyp][moov][mdat].
  6. Validation. The worker reads the result back; OutputValidator (Dart) compares it with what was asked for. Only then is the Blob returned.

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.workerScriptUrlOverride to a same-origin copy.
  • A Content Security Policy must allow the script: worker-src 'self' (or script-src 'self' where worker-src is not set), and blob: in media-src if the app plays the output through objectUrl().
  • Cross-origin isolation (COOP/COEP) is not needed: no SharedArrayBuffer is 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.

Libraries

video_normalizer_web
The web implementation of the video_normalizer plugin, for Chromium-based browsers.