Skip to content

The free and privacy-friendly screen recorder with no limits 🎥

WebCodecs Recorder: Stable MP4 pipeline with MediaBunny, fixed timestamps, smaller file sizes, and cleanup

Alyssa X0d661169fdc5
diff --git a/src/pages/Background/recording/recordingHelpers.js b/src/pages/Background/recording/recordingHelpers.js
index 4bd1ed4..03170ab 100644
--- a/src/pages/Background/recording/recordingHelpers.js
+++ b/src/pages/Background/recording/recordingHelpers.js
@@ -71,7 +71,7 @@ export const handleRecordingComplete = async () => {
tab.url.includes("recorder.html")
) {
// FLAG: For testing purposes -> comment to debug
- //removeTab(recordingTab);
+ removeTab(recordingTab);
}
}
});
@@ -95,7 +95,7 @@ export const handleRecordingError = async (request) => {
const { region } = await chrome.storage.local.get(["region"]);
if (recordingTab && !region) {
// FLAG: For testing purposes -> comment to debug
- //removeTab(recordingTab);
+ removeTab(recordingTab);
}
chrome.storage.local.set({ recordingTab: null });
discardOffscreenDocuments();
diff --git a/src/pages/EditorWebCodecs/Sandbox.jsx b/src/pages/EditorWebCodecs/Sandbox.jsx
index 05df521..973f9e3 100644
--- a/src/pages/EditorWebCodecs/Sandbox.jsx
+++ b/src/pages/EditorWebCodecs/Sandbox.jsx
@@ -93,6 +93,28 @@ const Sandbox = () => {
break;
}
+ case "compress-video": {
+ const rawBlob = await fetch(message.base64).then((r) => r.blob());
+
+ const compressed = await reencodeVideo(
+ ffmpegInstance.current,
+ rawBlob,
+ null,
+ (progress) =>
+ sendMessage({
+ type: "compression-progress",
+ progress: Math.round(progress * 100),
+ })
+ );
+ const base64 = await toBase64(compressed);
+ sendMessage({
+ type: "updated-blob",
+ base64,
+ topLevel: true,
+ });
+ break;
+ }
+
case "base64-to-blob": {
// Decode the base64 blob (type might be wrong or missing)
const rawBlob = await fetch(message.base64).then((r) => r.blob());
@@ -102,8 +124,6 @@ const Sandbox = () => {
const looksMp4 = header === "ftyp";
if (looksMp4) {
- console.log("Already MP4, skipping conversion.");
- // ⬆️ Already MP4 → skip all conversion
sendMessage({
type: "updated-blob",
base64: message.base64,
diff --git a/src/pages/Recorder/Recorder.jsx b/src/pages/Recorder/Recorder.jsx
index 9928d4e..bd47f03 100644
--- a/src/pages/Recorder/Recorder.jsx
+++ b/src/pages/Recorder/Recorder.jsx
@@ -18,15 +18,10 @@ const chunksStore = localforage.createInstance({
document.body.style.willChange = "contents";
-// ============================================================
-// DEBUG FLAG + LOGGER
-// ============================================================
-// Flip this to true locally when you want super detailed logs.
-// You can also override from the devtools console via:
+// Debug flag for logging
// window.SCREENITY_DEBUG_RECORDER = true;
-//const DEBUG_RECORDER =
-// typeof window !== "undefined" ? !!window.SCREENITY_DEBUG_RECORDER : false;
-const DEBUG_RECORDER = true;
+const DEBUG_RECORDER =
+ typeof window !== "undefined" ? !!window.SCREENITY_DEBUG_RECORDER : false;
const logPrefix = "[Screenity Recorder]";
@@ -290,7 +285,7 @@ const Recorder = () => {
videoBitsPerSecond,
});
- // FLAG: FORCE OLD MediaRecorder for testing
+ // FLAG: Force old MediaRecorder for testing
const FORCE_MEDIARECORDER = false;
const canUseWebCodecs =
diff --git a/src/pages/Recorder/recorderConfig.js b/src/pages/Recorder/recorderConfig.js
index 7d7171a..a6172bf 100644
--- a/src/pages/Recorder/recorderConfig.js
+++ b/src/pages/Recorder/recorderConfig.js
@@ -10,15 +10,15 @@ export const MIME_TYPES = [
export function getBitrates(quality) {
switch (quality) {
case "4k":
- return { audio: 256000, video: 35000000 };
+ return { audio: 192000, video: 20000000 };
case "1080p":
- return { audio: 192000, video: 16000000 };
- case "720p":
return { audio: 128000, video: 8000000 };
+ case "720p":
+ return { audio: 128000, video: 5000000 };
case "480p":
- return { audio: 96000, video: 4000000 };
+ return { audio: 96000, video: 3000000 };
case "360p":
- return { audio: 96000, video: 2000000 };
+ return { audio: 96000, video: 1500000 };
case "240p":
return { audio: 64000, video: 1000000 };
default:
diff --git a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old.ts b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old.ts
deleted file mode 100644
index 1f62198..0000000
--- a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old.ts
+++ /dev/null
@@ -1,276 +0,0 @@
-/*!
- * Screenity WebCodecs Recorder
- * Copyright (c) 2025 Serial Labs Ltd.
- *
- * This file is part of Screenity and is licensed under the GNU GPLv3.
- * See the LICENSE file in the project root for details.
- *
- * This module implements complex WebCodecs-based recording logic
- * for video/audio sync, muxing, and safe timestamp management.
- * If you reuse or modify this file in another project, GPLv3
- * obligations (including copyleft and attribution) apply.
- */
-// @ts-nocheck
-
-import {
- Output,
- Mp4OutputFormat,
- BufferTarget,
- EncodedVideoPacketSource,
- EncodedAudioPacketSource,
- EncodedPacket,
-} from "mediabunny";
-
-interface Mp4MuxerWrapperOptions {
- width: number;
- height: number;
- fps: number;
- videoBitrate: number;
- audioBitrate: number;
- videoCodec?: string;
- audioCodec?: string;
- onChunk: (
- chunk: Uint8Array,
- timestampUs: number | null
- ) => void | Promise<void>;
-}
-
-export class Mp4MuxerWrapper {
- private options: Mp4MuxerWrapperOptions;
-
- private output: Output;
- private target: BufferTarget;
-
- private videoSource: EncodedVideoPacketSource;
- private audioSource: EncodedAudioPacketSource | null = null;
- private videoCodec: string;
- private audioCodec: string;
-
- private ftypChunk: Uint8Array | null = null;
- private moovChunk: Uint8Array | null = null;
- private headerEmitted = false;
-
- private pendingMoof: Uint8Array | null = null;
- private pendingMoofTimestampSeconds: number | null = null;
- private fragmentTimestampOffsetSeconds: number | null = null;
- private videoTimestampOffsetUs: number | null = null;
- private audioTimestampOffsetUs: number | null = null;
- private lastVideoTimestampUs = 0;
- private lastAudioTimestampUs = 0;
- private pausedOffsetUs: number;
-
- private started = false;
-
- constructor(options: Mp4MuxerWrapperOptions) {
- this.options = options;
-
- this.target = new BufferTarget();
- this.videoCodec = options.videoCodec || "avc";
- this.audioCodec = options.audioCodec || "aac";
- this.pausedOffsetUs = 0;
-
- const minFragmentDuration = Math.max(
- 0.2,
- (this.options.fps ? 1 / this.options.fps : 1 / 30) * 6
- );
-
- this.output = new Output({
- format: new Mp4OutputFormat({
- fastStart: "in-memory",
- minimumFragmentDuration: minFragmentDuration,
- onFtyp: (data) => {
- this.ftypChunk = this.cloneChunk(data);
- this.emitHeaderIfReady();
- },
- onMoov: (data) => {
- this.moovChunk = this.cloneChunk(data);
- this.emitHeaderIfReady();
- },
- onMoof: (data, _pos, timestampSeconds = 0) => {
- this.pendingMoof = this.cloneChunk(data);
- this.pendingMoofTimestampSeconds = this.normalizeFragmentTimestamp(
- timestampSeconds
- );
- },
- onMdat: (data) => {
- if (!this.pendingMoof) {
- return;
- }
- const fragment = this.concatChunks([
- this.pendingMoof,
- this.cloneChunk(data),
- ]);
- const timestampUs =
- this.pendingMoofTimestampSeconds != null
- ? Math.round(this.pendingMoofTimestampSeconds * 1e6)
- : null;
- this.emitChunk(fragment, timestampUs);
- this.pendingMoof = null;
- this.pendingMoofTimestampSeconds = null;
- },
- }),
- target: this.target,
- });
-
- this.videoSource = new EncodedVideoPacketSource(this.videoCodec);
- this.output.addVideoTrack(this.videoSource, {
- frameRate: this.options.fps,
- });
- }
-
- enableAudio() {
- if (!this.audioSource) {
- this.audioSource = new EncodedAudioPacketSource(this.audioCodec);
- this.output.addAudioTrack(this.audioSource);
- }
- }
-
- addVideoChunk(chunk: EncodedVideoChunk, meta: any) {
- const packet = this.buildPacketFromChunk(chunk, meta, "video");
- return this.videoSource.add(packet, meta);
- }
-
- addAudioChunk(chunk: EncodedAudioChunk, meta: any) {
- if (!this.audioSource) {
- console.warn("[MUXER] audioSource missing — ignoring audio packet");
- return;
- }
- const packet = this.buildPacketFromChunk(chunk, meta, "audio");
- return this.audioSource.add(packet, meta);
- }
-
- async start() {
- if (this.started) return;
- await this.output.start();
- this.started = true;
- }
-
- async finalize() {
- await this.output.finalize();
-
- const data = this.target.buffer;
- if (data && data.byteLength > 0) {
- this.emitChunk(new Uint8Array(data), null);
- }
- }
-
- setPausedOffset(offsetUs: number) {
- this.pausedOffsetUs = offsetUs;
- }
-
- setAudioOffset(offsetUs: number) {
- this.audioTimestampOffsetUs = offsetUs;
- }
-
- private emitHeaderIfReady() {
- if (this.headerEmitted || !this.ftypChunk || !this.moovChunk) {
- return;
- }
- const header = this.concatChunks([this.ftypChunk, this.moovChunk]);
- this.headerEmitted = true;
- this.emitChunk(header, 0);
- }
-
- private emitChunk(chunk: Uint8Array, timestampUs: number | null) {
- try {
- const result = this.options.onChunk?.(chunk, timestampUs ?? null);
- if (result && typeof (result as Promise<unknown>).then === "function") {
- Promise.resolve(result).catch((err) => {
- console.error("[MUXER] onChunk rejected", err);
- });
- }
- } catch (err) {
- console.error("[MUXER] onChunk error", err);
- }
- }
-
- private cloneChunk(data: Uint8Array) {
- return new Uint8Array(data);
- }
-
- private concatChunks(chunks: Uint8Array[]) {
- const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
- const combined = new Uint8Array(total);
- let offset = 0;
- for (const chunk of chunks) {
- combined.set(chunk, offset);
- offset += chunk.byteLength;
- }
- return combined;
- }
-
- private buildPacketFromChunk(
- chunk: EncodedVideoChunk | EncodedAudioChunk,
- meta: any,
- kind: "video" | "audio"
- ) {
- const data = new Uint8Array(chunk.byteLength);
- chunk.copyTo(data);
- const relativeTimestampUs = this.normalizeSampleTimestamp(
- kind,
- chunk.timestamp,
- chunk.duration || 0
- );
- const durationSeconds = (chunk.duration || 0) / 1e6;
-
- return new EncodedPacket(
- data,
- chunk.type,
- relativeTimestampUs / 1e6,
- durationSeconds
- );
- }
-private normalizeSampleTimestamp(
- kind: "video" | "audio",
- timestampUs?: number,
- durationUs: number = 0
-) {
- const offsetKey =
- kind === "video" ? "videoTimestampOffsetUs" : "audioTimestampOffsetUs";
- const lastKey =
- kind === "video" ? "lastVideoTimestampUs" : "lastAudioTimestampUs";
-
- // Fallback when encoder doesn’t give timestamps
- if (typeof timestampUs !== "number") {
- const prev = (this as any)[lastKey] ?? 0;
- const next = prev + durationUs;
- (this as any)[lastKey] = next;
- return next;
- }
-
- let baseOffset = (this as any)[offsetKey] as number | null;
-
- // If we explicitly set an audio offset, prefer that
- if (kind === "audio") {
- if (this.audioTimestampOffsetUs == null) {
- this.audioTimestampOffsetUs = timestampUs;
- }
- baseOffset = this.audioTimestampOffsetUs;
- } else if (baseOffset == null) {
- // First sample for this track: establish base offset
- baseOffset = timestampUs;
- (this as any)[offsetKey] = baseOffset;
- }
-
- const relative = Math.max(
- 0,
- timestampUs - baseOffset - this.pausedOffsetUs
- );
-
- (this as any)[lastKey] = relative;
- return relative;
-}
-
- private normalizeFragmentTimestamp(timestampSeconds: number | null | undefined) {
- if (timestampSeconds == null) {
- return null;
- }
- if (this.fragmentTimestampOffsetSeconds == null) {
- this.fragmentTimestampOffsetSeconds = timestampSeconds;
- }
- return Math.max(
- 0,
- timestampSeconds - this.fragmentTimestampOffsetSeconds
- );
- }
-}
diff --git a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old2.ts b/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old2.ts
deleted file mode 100644
index bbecbc3..0000000
--- a/src/pages/Recorder/webcodecs/Mp4MuxerWrapper-old2.ts
+++ /dev/null
@@ -1,252 +0,0 @@
-/*!
- * Screenity WebCodecs Recorder
- * Copyright (c) 2025 Serial Labs Ltd.
- *
- * This file is part of Screenity and is licensed under the GNU GPLv3.
- * See the LICENSE file in the project root for details.
- *
- * This module implements complex WebCodecs-based recording logic
- * for video/audio sync, muxing, and safe timestamp management.
- * If you reuse or modify this file in another project, GPLv3
- * obligations (including copyleft and attribution) apply.
- */
-// @ts-nocheck
-
-import {
- Output,
- Mp4OutputFormat,
- StreamTarget,
- StreamTargetChunk,
- EncodedVideoPacketSource,
- EncodedAudioPacketSource,
- EncodedPacket,
-} from "mediabunny";
-
-interface Mp4MuxerWrapperOptions {
- width: number;
- height: number;
- fps: number;
- videoBitrate: number;
- audioBitrate: number;
- videoCodec?: string;
- audioCodec?: string;
- onChunk: (
- chunk: Uint8Array,
- timestampUs: number | null
- ) => void | Promise<void>;
-}
-
-export class Mp4MuxerWrapper {
- private options: Mp4MuxerWrapperOptions;
-
- private output: Output;
- private target: StreamTarget;
-
- private videoSource: EncodedVideoPacketSource;
- private audioSource: EncodedAudioPacketSource | null = null;
- private videoCodec: string;
- private audioCodec: string;
-
- // We no longer manually track ftyp/moov/moof/mdat here. StreamTarget
- // will give us the final MP4 in order, chunk by chunk.
- private headerEmitted = false;
-
- private fragmentTimestampOffsetSeconds: number | null = null;
- private videoTimestampOffsetUs: number | null = null;
- private audioTimestampOffsetUs: number | null = null;
- private lastVideoTimestampUs = 0;
- private lastAudioTimestampUs = 0;
- private pausedOffsetUs: number;
-
- private started = false;
-
- constructor(options: Mp4MuxerWrapperOptions) {
- this.options = options;
-
- this.videoCodec = options.videoCodec || "avc";
- this.audioCodec = options.audioCodec || "aac";
- this.pausedOffsetUs = 0;
-
- const minFragmentDuration = Math.max(
- 0.2,
- (this.options.fps ? 1 / this.options.fps : 1 / 30) * 6
- );
-
- // StreamTarget: lets us stream bytes out incrementally instead of
- // ever materializing a single huge ArrayBuffer in memory.
- const writable = new WritableStream<StreamTargetChunk>({
- write: (chunk) => {
- // In fragmented fast-start mode, Mp4OutputFormat writes in
- // append-only order, so simply appending all Uint8Arrays in
- // arrival order yields a valid fMP4 file.
- //
- // Your existing IndexedDB “chunksStore” can keep doing:
- // chunks.push(chunk.data)
- // and later:
- // new Blob(chunks, { type: "video/mp4" })
- //
- // We don’t have an exact fragment timestamp here, so we just
- // pass null. If you really want, you can plumb something like
- // this.lastVideoTimestampUs instead.
- this.emitChunk(chunk.data, null);
- },
- });
-
- this.target = new StreamTarget(writable, {
- // Chunked mode: accumulate into reasonably sized chunks before
- // emitting, to keep `onChunk` frequency sane.
- chunked: true,
- chunkSize: 2 ** 20, // 1 MiB; tweak if you want bigger/smaller IDB chunks
- });
-
- this.output = new Output({
- target: this.target,
- format: new Mp4OutputFormat({
- // Key part: fragmented fast-start → append-only writes,
- // suitable for streaming / simple concatenation.
- fastStart: "fragmented",
- minimumFragmentDuration: minFragmentDuration,
- }),
- });
-
- this.videoSource = new EncodedVideoPacketSource(this.videoCodec);
- this.output.addVideoTrack(this.videoSource, {
- frameRate: this.options.fps,
- });
- }
-
- enableAudio() {
- if (!this.audioSource) {
- this.audioSource = new EncodedAudioPacketSource(this.audioCodec);
- this.output.addAudioTrack(this.audioSource);
- }
- }
-
- addVideoChunk(chunk: EncodedVideoChunk, meta: any) {
- const packet = this.buildPacketFromChunk(chunk, meta, "video");
- return this.videoSource.add(packet, meta);
- }
-
- addAudioChunk(chunk: EncodedAudioChunk, meta: any) {
- if (!this.audioSource) {
- console.warn("[MUXER] audioSource missing — ignoring audio packet");
- return;
- }
- const packet = this.buildPacketFromChunk(chunk, meta, "audio");
- return this.audioSource.add(packet, meta);
- }
-
- async start() {
- if (this.started) return;
- await this.output.start();
- this.started = true;
- }
-
- async finalize() {
- // Important: with StreamTarget this *no longer* allocates a giant
- // ArrayBuffer. It just finishes writing and closes the stream.
- await this.output.finalize();
- // Nothing else to emit here: all data (including header + tail)
- // has already gone through the WritableStream → onChunk path.
- }
-
- setPausedOffset(offsetUs: number) {
- this.pausedOffsetUs = offsetUs;
- }
-
- setAudioOffset(offsetUs: number) {
- this.audioTimestampOffsetUs = offsetUs;
- }
-
- private emitChunk(chunk: Uint8Array, timestampUs: number | null) {
- try {
- const result = this.options.onChunk?.(chunk, timestampUs ?? null);
- if (result && typeof (result as Promise<unknown>).then === "function") {
- Promise.resolve(result).catch((err) => {
- console.error("[MUXER] onChunk rejected", err);
- });
- }
- } catch (err) {
- console.error("[MUXER] onChunk error", err);
- }
- }
-
- private buildPacketFromChunk(
- chunk: EncodedVideoChunk | EncodedAudioChunk,
- meta: any,
- kind: "video" | "audio"
- ) {
- const data = new Uint8Array(chunk.byteLength);
- chunk.copyTo(data);
-
- const relativeTimestampUs = this.normalizeSampleTimestamp(
- kind,
- chunk.timestamp,
- chunk.duration || 0
- );
- const durationSeconds = (chunk.duration || 0) / 1e6;
-
- return new EncodedPacket(
- data,
- chunk.type,
- relativeTimestampUs / 1e6,
- durationSeconds
- );
- }
-
- private normalizeSampleTimestamp(
- kind: "video" | "audio",
- timestampUs?: number,
- durationUs: number = 0
- ) {
- const offsetKey =
- kind === "video" ? "videoTimestampOffsetUs" : "audioTimestampOffsetUs";
- const lastKey =
- kind === "video" ? "lastVideoTimestampUs" : "lastAudioTimestampUs";
-
- // Fallback when encoder doesn’t give timestamps
- if (typeof timestampUs !== "number") {
- const prev = (this as any)[lastKey] ?? 0;
- const next = prev + durationUs;
- (this as any)[lastKey] = next;
- return next;
- }
-
- let baseOffset = (this as any)[offsetKey] as number | null;
-
- // If we explicitly set an audio offset, prefer that
- if (kind === "audio") {
- if (this.audioTimestampOffsetUs == null) {
- this.audioTimestampOffsetUs = timestampUs;
- }
- baseOffset = this.audioTimestampOffsetUs;
- } else if (baseOffset == null) {
- // First sample for this track: establish base offset
- baseOffset = timestampUs;
- (this as any)[offsetKey] = baseOffset;
- }
-
- const relative = Math.max(
- 0,
- timestampUs - baseOffset - this.pausedOffsetUs
- );
-
- (this as any)[lastKey] = relative;
- return relative;
- }
-
- private normalizeFragmentTimestamp(
- timestampSeconds: number | null | undefined
- ) {
- if (timestampSeconds == null) {
- return null;
- }
- if (this.fragmentTimestampOffsetSeconds == null) {
- this.fragmentTimestampOffsetSeconds = timestampSeconds;
- }
- return Math.max(
- 0,
- timestampSeconds - this.fragmentTimestampOffsetSeconds
- );
- }
-}
\ No newline at end of file
diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder-old.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder-old.js
deleted file mode 100644
index cd5b7b2..0000000
--- a/src/pages/Recorder/webcodecs/WebCodecsRecorder-old.js
+++ /dev/null
@@ -1,654 +0,0 @@
-/*!
- * Screenity WebCodecs Recorder
- * Copyright (c) 2025 Serial Labs Ltd.
- *
- * This file is part of Screenity and is licensed under the GNU GPLv3.
- * See the LICENSE file in the project root for details.
- *
- * This module implements complex WebCodecs-based recording logic
- * for video/audio sync, muxing, and safe timestamp management.
- * If you reuse or modify this file in another project, GPLv3
- * obligations (including copyleft and attribution) apply.
- */
-import { Mp4MuxerWrapper } from "./Mp4MuxerWrapper";
-
-export class WebCodecsRecorder {
- constructor(stream, options) {
- this.stream = stream;
- this.options = options;
-
- this.debug = options.debug ?? false;
- this.log = (...args) => this.debug && console.log(...args);
- this.warn = (...args) => this.debug && console.warn(...args);
- this.err = (...args) => this.debug && console.error(...args);
-
- this.videoTrack = null;
- this.audioTrack = null;
-
- this.videoProcessor = null;
- this.audioProcessor = null;
-
- this.videoReader = null;
- this.audioReader = null;
-
- this.videoEncoder = null;
- this.audioEncoder = null;
-
- this.muxer = null;
-
- this.running = false;
-
- this.actualWidth = 0;
- this.actualHeight = 0;
-
- this.targetWidth = 0;
- this.targetHeight = 0;
-
- this.videoTimestampOffsetUs = null;
- this.videoFallbackStartMs = null;
- this.selectedVideoCodec = null;
- this.firstVideoFrame = undefined;
- this.frameCount = 0;
-
- this.resizeCanvas = null;
- this.resizeCtx = null;
-
- this.justResumed = false;
- this.paused = false;
- this.pauseStartUs = null;
- this.totalPausedDurationUs = 0;
-
- this._startPromise = null;
- this._startResolve = null;
-
- this._prebufferedAudio = [];
- this._audioReady = false;
-
- this._stopping = false;
- this._videoLoopPromise = null;
- this._audioLoopPromise = null;
- }
-
- async _probeRealResolution() {
- const processor = new MediaStreamTrackProcessor({ track: this.videoTrack });
- const reader = processor.readable.getReader();
-
- const { value: frame } = await reader.read();
- if (!frame) throw new Error("Cannot probe frame resolution");
-
- const width = frame.codedWidth;
- const height = frame.codedHeight;
-
- this.log("[WCR] Probed resolution:", width, "x", height);
- frame.close();
- reader.releaseLock();
-
- return { width, height };
- }
-
- async start() {
- this.videoTimestampOffsetUs = null;
- this.firstVideoFrame = undefined;
- this._stopping = false;
-
- if (this.running) return this._startPromise;
-
- this._startPromise = new Promise((resolve) => {
- this._startResolve = resolve;
- });
-
- this.running = true;
- try {
- this.videoTrack = this.stream.getVideoTracks()[0] || null;
- this.audioTrack = this.stream.getAudioTracks()[0] || null;
-
- this.log(
- "[WCR] videoTrack",
- this.videoTrack,
- "state=",
- this.videoTrack?.readyState
- );
- this.log(
- "[WCR] audioTrack",
- this.audioTrack,
- "state=",
- this.audioTrack?.readyState
- );
-
- if (!this.videoTrack) {
- const err = new Error("WebCodecsRecorder: No video track");
- this.options.onError?.(err);
- this.running = false;
-
- if (this._startResolve) {
- this._startResolve(false);
- this._startResolve = null;
- }
-
- this._startPromise = null;
- return false;
- }
-
- try {
- const { width, height } = await this._probeRealResolution();
- this.actualWidth = width;
- this.actualHeight = height;
-
- let targetWidth = Math.min(width, 1920);
- let targetHeight = Math.round((height / width) * targetWidth);
- if (targetHeight > 1080) {
- targetHeight = 1080;
- targetWidth = Math.round((width / height) * targetHeight);
- }
- this.targetWidth = targetWidth;
- this.targetHeight = targetHeight;
-
- this.log("[WCR] Target:", this.targetWidth, "x", this.targetHeight);
-
- const fps = this.options.fps || 30;
- const safeBitrate = this.options.videoBitrate || 10_000_000;
-
- const videoConfig = await this.chooseVideoEncoderConfig({
- width: this.targetWidth,
- height: this.targetHeight,
- fps,
- bitrate: safeBitrate,
- });
- this.selectedVideoCodec = videoConfig.codec;
-
- const audioConfig = await this.prepareAudioEncoderConfig();
- this._pendingAudioConfig = audioConfig;
-
- this.log("[WCR] Audio config:", audioConfig);
-
- if (!audioConfig) {
- this.warn("[WCR] No audio available");
- this.options.audioBitrate = undefined;
- this.options.enableAudio = false;
- this._audioReady = false;
- }
-
- this.videoProcessor = new MediaStreamTrackProcessor({
- track: this.videoTrack,
- });
- this.videoReader = this.videoProcessor.readable.getReader();
-
- if (audioConfig && this.audioTrack) {
- this.audioProcessor = new MediaStreamTrackProcessor({
- track: this.audioTrack,
- });
- this.audioReader = this.audioProcessor.readable.getReader();
- }
-
- if (this.audioReader && this.options.enableAudio) {
- this.log("[WCR] start buffering audio");
- this._audioLoopPromise = this.readAudioLoop();
- }
-
- this.log("[WCR] creating muxer...");
- this.muxer = new Mp4MuxerWrapper({
- width: this.targetWidth,
- height: this.targetHeight,
- fps,
- videoBitrate: this.options.videoBitrate,
- audioBitrate: this.options.audioBitrate,
- videoCodec: videoConfig.containerCodec,
- audioCodec: this.options.enableAudio ? "aac" : undefined,
- onChunk: this.options.onChunk,
- });
-
- if (this.options.enableAudio) {
- this.muxer.enableAudio();
- }
-
- await this.muxer.start();
- this.log("[WCR] muxer started");
-
- await this.initVideoEncoder(videoConfig.config, videoConfig.codec);
-
- await Promise.resolve();
-
- setTimeout(() => {
- this._videoLoopPromise = this.readVideoLoop();
-
- if (this._startResolve) {
- this._startResolve(true);
- this._startResolve = null;
- }
- }, 30);
-
- this.log("[WCR] start() done");
- } catch (err) {
- this.err("[WCR] start() failed:", err);
- this.options.onError?.(err);
- this.running = false;
- this.cleanup();
-
- if (this._startResolve) {
- this._startResolve(false);
- this._startResolve = null;
- }
- }
- } catch (err) {
- this.err("[WCR] start() outer error:", err);
- this.options.onError?.(err);
- this.running = false;
- this.cleanup();
-
- if (this._startResolve) {
- this._startResolve(false);
- this._startResolve = null;
- }
- }
-
- return this._startPromise;
- }
-
- async stop() {
- this.log("[WCR] stop()", this.frameCount);
-
- if (this._stopping) {
- this.log("[WCR] stop() already in progress");
- return;
- }
- this._stopping = true;
-
- if (!this.running) {
- this.log("[WCR] stop() called but recorder not running");
- this._stopping = false;
- return;
- }
-
- // Soft stop: let loops finish current work
- this.running = false;
- this.paused = true;
-
- try {
- const loopPromises = [];
- if (this._videoLoopPromise) loopPromises.push(this._videoLoopPromise);
- if (this._audioLoopPromise) loopPromises.push(this._audioLoopPromise);
-
- if (loopPromises.length) {
- this.log("[WCR] Waiting for read loops to finish before flush...");
- await Promise.race([
- Promise.allSettled(loopPromises),
- new Promise((res) => setTimeout(res, 500)), // safety timeout
- ]);
- }
- } catch (err) {
- this.err("[WCR] Error while waiting for loops:", err);
- }
-
- try {
- if (this.videoEncoder && this.videoEncoder.state !== "closed") {
- await this.videoEncoder.flush();
- }
- if (this.audioEncoder && this.audioEncoder.state !== "closed") {
- await this.audioEncoder.flush();
- }
- } catch (err) {
- this.err("[WCR] flush error:", err);
- this.options.onError?.(err);
- }
-
- try {
- if (this.muxer) {
- await this.muxer.finalize();
- if (this.options.onFinalized) {
- await this.options.onFinalized();
- }
- }
- } catch (err) {
- this.err("[WCR] muxer.finalize:", err);
- this.options.onError?.(err);
- }
-
- this.cleanup();
-
- if (this.options.onStop) {
- await this.options.onStop();
- }
-
- this._stopping = false;
- }
-
- pause() {
- if (!this.running || this.paused) return;
- this.paused = true;
- this.pauseStartUs = performance.now() * 1000;
- }
-
- resume() {
- if (!this.running || !this.paused) return;
-
- const nowUs = performance.now() * 1000;
- this.totalPausedDurationUs += nowUs - this.pauseStartUs;
-
- this.paused = false;
- this.justResumed = true;
- this.muxer?.setPausedOffset(this.totalPausedDurationUs);
- }
-
- cleanup() {
- this.log("[WCR] cleanup");
-
- try {
- this.videoReader?.releaseLock();
- } catch {}
- try {
- this.audioReader?.releaseLock();
- } catch {}
-
- try {
- this.videoEncoder?.close();
- } catch {}
- try {
- this.audioEncoder?.close();
- } catch {}
-
- this.videoProcessor = null;
- this.audioProcessor = null;
- this.videoReader = null;
- this.audioReader = null;
- this.videoEncoder = null;
- this.audioEncoder = null;
- this.videoTrack = null;
- this.audioTrack = null;
-
- this.videoTimestampOffsetUs = null;
- this.videoFallbackStartMs = null;
- this.firstVideoFrame = undefined;
- this.selectedVideoCodec = null;
-
- this.resizeCanvas = null;
- this.resizeCtx = null;
- this._startPromise = null;
- this._startResolve = null;
-
- this._videoLoopPromise = null;
- this._audioLoopPromise = null;
- }
-
- async initVideoEncoder(config, codecLabel) {
- this.log("[WCR] initVideoEncoder()", codecLabel);
-
- this.videoEncoder = new VideoEncoder({
- output: (chunk, meta = {}) => {
- if (this.debug) {
- this.log("[WCR] VIDEO", chunk.type, chunk.timestamp, chunk.duration);
- }
- const normalizedMeta = { ...meta };
- const decoderConfig = normalizedMeta.decoderConfig
- ? { ...normalizedMeta.decoderConfig }
- : {};
-
- decoderConfig.codec = decoderConfig.codec || codecLabel;
- decoderConfig.codedWidth = decoderConfig.codedWidth || this.targetWidth;
- decoderConfig.codedHeight =
- decoderConfig.codedHeight || this.targetHeight;
- normalizedMeta.decoderConfig = decoderConfig;
-
- this.muxer.addVideoChunk(chunk, normalizedMeta);
- },
- error: (err) => {
- this.err("[WCR] VideoEncoder error:", err);
- this.options.onError?.(err);
- },
- });
-
- this.videoEncoder.configure(config);
- this.log("[WCR] VideoEncoder configured");
- }
-
- async initAudioEncoder(config) {
- if (!config) return;
-
- this.audioEncoder = new AudioEncoder({
- output: (chunk, meta) => {
- if (this.debug) this.log("[WCR] AUDIO");
- this.muxer.addAudioChunk(chunk, meta);
- },
- error: (err) => {
- this.err("[WCR] AudioEncoder error:", err);
- this.options.onError?.(err);
- },
- });
-
- this.audioEncoder.configure(config);
-
- if (this.muxer && this.videoTimestampOffsetUs != null) {
- this.muxer.setAudioOffset(this.videoTimestampOffsetUs);
- }
- }
-
- async prepareAudioEncoderConfig() {
- if (!this.audioTrack) return null;
-
- const settings = this.audioTrack.getSettings();
- const sampleRate = settings.sampleRate || 48000;
- const numberOfChannels = settings.channelCount || 2;
-
- const candidateConfig = {
- codec: "mp4a.40.2",
- sampleRate,
- numberOfChannels,
- bitrate: this.options.audioBitrate || 128000,
- };
-
- try {
- const support = await AudioEncoder.isConfigSupported(candidateConfig);
- if (!support.supported) {
- this.warn("[WCR] AAC unsupported");
- return null;
- }
- return support.config || candidateConfig;
- } catch {
- this.warn("[WCR] AAC probe failed");
- return null;
- }
- }
-
- async chooseVideoEncoderConfig({ width, height, fps, bitrate }) {
- const base = {
- width,
- height,
- framerate: fps,
- bitrate,
- bitrateMode: "constant",
- latencyMode: "realtime",
- };
-
- const candidates = [
- { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-hardware" },
- { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-hardware" },
- { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-hardware" },
- { codec: "avc1.64002A", containerCodec: "avc", hw: "prefer-software" },
- { codec: "avc1.4D401F", containerCodec: "avc", hw: "prefer-software" },
- { codec: "avc1.42E01E", containerCodec: "avc", hw: "prefer-software" },
- ];
-
- for (const c of candidates) {
- const config = { ...base, codec: c.codec, hardwareAcceleration: c.hw };
- try {
- const test = new VideoEncoder({
- output() {},
- error() {},
- });
- test.configure(config);
- test.close();
- this.log("[WCR] Selected encoder:", c.codec, c.hw);
- return {
- config,
- codec: c.codec,
- containerCodec: c.containerCodec,
- };
- } catch (e) {}
- }
- throw new Error("WebCodecsRecorder: No supported H.264 encoder");
- }
-
- getVideoTimestampUs(frame) {
- if (typeof frame.timestamp === "number") {
- if (this.videoTimestampOffsetUs === null) {
- this.videoTimestampOffsetUs = frame.timestamp;
- }
-
- const baseUs = Math.max(
- 0,
- Math.round(frame.timestamp - this.videoTimestampOffsetUs)
- );
-
- return Math.max(0, baseUs - this.totalPausedDurationUs);
- }
-
- const now = performance.now();
- if (!this._lastFrameTime) this._lastFrameTime = now;
- const delta = now - this._lastFrameTime;
- this._lastFrameTime = now;
-
- if (delta > 160 && !this._warnedBackpressure) {
- this._warnedBackpressure = true;
- this.warn("[WCR] overload", delta);
- this.options.onError?.({ type: "encoder-overload", delta });
- }
- if (this.videoFallbackStartMs === null) {
- this.videoFallbackStartMs = now;
- }
-
- const baseUs = Math.max(
- 0,
- Math.round((now - this.videoFallbackStartMs) * 1000)
- );
-
- return Math.max(0, baseUs - this.totalPausedDurationUs);
- }
-
- async readVideoLoop() {
- this.log("[WCR] video loop start");
- if (!this.videoReader || !this.videoEncoder) return;
-
- const ensureResizeCanvas = () => {
- if (!this.resizeCanvas) {
- this.resizeCanvas = document.createElement("canvas");
- this.resizeCanvas.width = this.targetWidth;
- this.resizeCanvas.height = this.targetHeight;
- this.resizeCtx = this.resizeCanvas.getContext("2d");
- this.log("[WCR] resize canvas created");
- }
- };
-
- try {
- while (this.running) {
- if (this.paused) {
- await new Promise((r) => setTimeout(r, 10));
- continue;
- }
-
- const { value: frame, done } = await this.videoReader
- .read()
- .catch(() => ({ done: true }));
-
- if (done || !frame) break;
-
- if (frame.codedWidth === 0 || frame.codedHeight === 0) {
- this.warn("[WCR] zero-size frame");
- this.options.onError?.({ type: "video-lost" });
- }
-
- ensureResizeCanvas();
-
- this.resizeCtx.drawImage(
- frame,
- 0,
- 0,
- this.targetWidth,
- this.targetHeight
- );
-
- const resized = new VideoFrame(this.resizeCanvas, {
- timestamp: frame.timestamp,
- });
-
- const timestampUs = this.getVideoTimestampUs(frame);
-
- if (this.firstVideoFrame === undefined) {
- this.firstVideoFrame = true;
- this.videoTimestampOffsetUs = frame.timestamp;
- this.log("[WCR] baseline:", this.videoTimestampOffsetUs);
-
- if (this.muxer)
- this.muxer.setAudioOffset(this.videoTimestampOffsetUs);
-
- if (!this.audioEncoder && this._pendingAudioConfig) {
- await this.initAudioEncoder(this._pendingAudioConfig);
- await Promise.resolve();
- for (const buf of this._prebufferedAudio) {
- this.audioEncoder.encode(buf);
- buf.close?.();
- }
- this._prebufferedAudio.length = 0;
- this._audioReady = true;
- }
- }
-
- const needsKey =
- this.frameCount === 0 ||
- this.justResumed ||
- this.firstVideoFrame === true;
- this.justResumed = false;
-
- this.videoEncoder.encode(resized, { keyFrame: needsKey });
- this.frameCount++;
-
- resized.close();
- frame.close();
- }
- } catch (err) {
- this.err("[WCR] video loop error:", err);
- }
-
- this.log("[WCR] video loop exit", this.frameCount);
- }
-
- async readAudioLoop() {
- while (this.paused && this.running) {
- await new Promise((r) => setTimeout(r, 10));
- }
- if (!this.audioReader) return;
- this.log("[WCR] audio loop start");
-
- try {
- while (this.running) {
- const { value: audioData, done } = await this.audioReader
- .read()
- .catch(() => ({ done: true }));
-
- if (done || !audioData) break;
- if (!this.audioTrack || this.audioTrack.readyState === "ended") {
- this.warn("[WCR] audio lost");
- this.options.onError?.({ type: "audio-lost" });
- break;
- }
-
- if (!this._audioReady) {
- this._prebufferedAudio.push(audioData);
- continue;
- }
-
- try {
- this.audioEncoder.encode(audioData);
- } catch (err) {
- audioData.close?.();
- this.options.onError?.(err);
- break;
- }
-
- audioData.close?.();
- }
- } catch (err) {
- this.err("[WCR] audio loop error:", err);
- }
-
- this.log("[WCR] audio loop exit");
- }
-}
diff --git a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
index 4e69ceb..986bda2 100644
--- a/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
+++ b/src/pages/Recorder/webcodecs/WebCodecsRecorder.js
@@ -520,44 +520,21 @@ export class WebCodecsRecorder {
this.targetHeight
);
- const fps = this.options.fps || 30;
- const durUs = Math.round(1e6 / fps);
+ if (!this.startTimeUs) this.startTimeUs = performance.now() * 1000;
- // Monotonic, frame-index-based timestamp to avoid drift/WebCodecs bugs
- const tsUs = this._videoFrameIndex * durUs;
- this._videoFrameIndex++;
+ const nowUs = performance.now() * 1000;
+ const tsUs = nowUs - this.startTimeUs;
const resized = new VideoFrame(this.resizeCanvas, {
timestamp: tsUs,
- duration: durUs, // <-- ADD THIS
});
- if (this.firstVideoFrame === undefined) {
- this.firstVideoFrame = true;
- this.frameCount = 0;
-
- if (this.muxer) {
- this.muxer.setAudioOffset(0);
- }
-
- if (!this.audioEncoder && this._pendingAudioConfig) {
- await this.initAudioEncoder(this._pendingAudioConfig);
- for (const buf of this._prebufferedAudio) {
- this.audioEncoder.encode(buf);
- buf.close?.();
- }
- this._prebufferedAudio.length = 0;
- this._audioReady = true;
- }
- }
-
// Keyframe only first frame and after resume
const keyFrame = this.frameCount === 0 || this.justResumed;
this.justResumed = false;
this.videoEncoder.encode(resized, {
timestamp: tsUs,
- duration: durUs,
keyFrame,
});
diff --git a/src/pages/Sandbox/context/ContentState.jsx b/src/pages/Sandbox/context/ContentState.jsx
index a5058a8..7f99b53 100644
--- a/src/pages/Sandbox/context/ContentState.jsx
+++ b/src/pages/Sandbox/context/ContentState.jsx
@@ -265,7 +265,36 @@ const ContentState = (props) => {
? withBlob
: new Blob(videoChunks.current, { type: "video/webm; codecs=vp8,opus" });
- if (blob.type === "video/mp4" || /\.mp4$/i.test(blob.name || "")) {
+ if (blob.type === "video/mp4") {
+ //const TOO_BIG_BYTES = 200 * 1024 * 1024;
+ // const TOO_BIG_BYTES = 0;
+ // if (blob.size > TOO_BIG_BYTES) {
+ // // Convert Blob → base64 first so we can pass to sandbox
+ // const reader = new FileReader();
+ // reader.onloadend = () => {
+ // const base64 = reader.result;
+
+ // setContentState((prev) => ({
+ // ...prev,
+ // base64,
+ // compressing: true,
+ // mp4ready: false,
+ // ready: false,
+ // rawBlob: prev.rawBlob || blob,
+ // }));
+
+ // // Send to sandbox for compression
+ // sendMessage({
+ // type: "compress-video",
+ // base64,
+ // topLevel: true,
+ // });
+ // };
+ // reader.readAsDataURL(blob);
+
+ // return;
+ // }
+
setContentState((prev) => ({
...prev,
blob: blob,
@@ -652,9 +681,7 @@ const ContentState = (props) => {
const onMessage = async (event) => {
if (event.data.type === "updated-blob") {
const base64 = event.data.base64;
- // const blob = new Blob([base64ToUint8Array(base64)], {
- // type: "video/mp4",
- // });
+
const blob = base64ToUint8Array(base64);
const wasCropping = contentState.cropping;
@@ -720,9 +747,7 @@ const ContentState = (props) => {
}
} else if (event.data.type === "download-mp4") {
const base64 = event.data.base64;
- // const blob = new Blob([base64ToUint8Array(base64)], {
- // type: "video/mp4",
- // });
+
const blob = base64ToUint8Array(base64);
// Download the blob
const url = URL.createObjectURL(blob);
diff --git a/src/pages/Sandbox/layout/player/RightPanel.js b/src/pages/Sandbox/layout/player/RightPanel.js
index cb34c19..b39325c 100644
--- a/src/pages/Sandbox/layout/player/RightPanel.js
+++ b/src/pages/Sandbox/layout/player/RightPanel.js
@@ -180,10 +180,13 @@ const RightPanel = () => {
() => {
const blob = contentStateRef.current.rawBlob;
const url = window.URL.createObjectURL(blob);
+
+ const ext = blob.type.includes("mp4") ? "mp4" : "webm";
+
chrome.downloads.download(
{
url: url,
- filename: "raw-recording.webm",
+ filename: `raw-recording.${ext}`,
},
() => {
window.URL.revokeObjectURL(url);