How adriansajjan/webcodecs-samples works
Project type and purpose
• This is a browser-based, single-page application (SPA) built with Vite and TypeScript.
• It provides four main multimedia features using the WebCodecs and Web APIs:
• Screen recording (in MP4 or WebM)
• Audio extraction from video files
• Video cropping
• Custom video playback with demuxing and rendering
File structure
• Top-level:
• index.html, main.ts – bootstraps the SPA
• router.ts + src/routes – maps URL paths to feature modules
• src/features – each feature lives in its own subfolder
· audio-extractor/extract.ts
· screen-recorder/recorder.ts, worker.ts, codec/*, constants/*
· video-cropper/cropper.ts
· video-player/player.ts, demuxer.ts, renderer/*, worker.ts, constants/*
• src/shared – cross-feature utilities and type definitions
• No backend: all processing runs in the browser or dedicated Web Workers.
• Configuration: Vite (vite.config.js), TypeScript (tsconfig.json), package.json for dependencies.
README
• Project title “Webcodecs Screen Recorder” confirms focus on WebCodecs-based media manipulation in the browser.
Main components and interactions
a. Components
1. User (actor)
2. Browser / SPA shell
3. Router & Route modules (router.ts + src/routes)
4. Feature modules (UI + controller logic)
• ScreenRecorder
• AudioExtractor
• VideoCropper
• VideoPlayer
5. Web Workers
• screen-recorder worker (worker.ts)
• video-player worker (worker.ts)
6. Codec implementations (MP4, WebM)
7. Shared utilities (utils.ts) & types
8. External Web APIs
• MediaDevices.getDisplayMedia
• WebCodecs API (VideoEncoder, AudioEncoder, VideoDecoder, AudioDecoder)
• Canvas2D or WebGL for rendering
• Web Audio API
9. Local file storage / Download API
b. Relationships & Data Flows
1. User → SPA → Router → Feature UI
2. ScreenRecorder UI → Recorder.ts
• Recorder.ts invokes getDisplayMedia, pipes MediaStream → Worker via postMessage
• Worker loads appropriate codec (MP4 or WebM) → uses WebCodecs encoders → outputs byte chunks back to main thread → triggers download
3. AudioExtractor UI → extract.ts
• Reads input file → uses WebCodecs decoders → extracts audio track → re-encodes (if needed) → provides audio file
4. VideoCropper UI → cropper.ts
• Loads source video → uses HTMLVideoElement + Canvas2D to crop frames → reassembles via WebCodecs encoders → download
5. VideoPlayer UI → player.ts
• Loads video file → demuxer.ts splits streams → Worker decodes frames via WebCodecs decoders → sends frames to renderer (2D or WebGL) and audio packets to Web Audio API
c. Architectural patterns & principles
• Feature-based modularization: each capability lives in its own folder with its own entrypoint and worker.
• Separation of concerns:
• UI logic in controllers (player.ts, recorder.ts, etc.)
• Heavy CPU work in Web Workers
• Codec logic abstracted behind base.ts and specific implementations
• Shared types/utilities in src/shared
• Event-driven data flow: uses constants/events.ts to decouple modules.
d. Key technologies
• TypeScript & Vite (module bundling)
• WebCodecs API (Video/AudioEncoder, Decoder)
• Web Workers for off-main-thread processing
• Canvas2D or WebGL for rendering video frames
• Web Audio API for playback
• MediaDevices API for screen capture