diff --git a/dev/convert.html b/dev/convert.html index 24afb95..4830820 100644 --- a/dev/convert.html +++ b/dev/convert.html @@ -24,7 +24,7 @@ chunked: true, chunkSize: 2**20 }); - const outputFormat = new Mediabunny.Mp4OutputFormat({}); + const outputFormat = new Mediabunny.Mp3OutputFormat({}); const button = document.createElement('button'); button.textContent = 'Cancel'; @@ -41,7 +41,7 @@ target }), audio: { - discard: true, + //discard: true, //codec: 'opus', //bitrate: 128000, //numberOfChannels: 1, @@ -94,8 +94,8 @@ //height: 100, }), trim: { - start: 0, - end: 20 + //start: 0, + //end: 20 }, }); console.log(conversion); diff --git a/docs/guide/media-sources.md b/docs/guide/media-sources.md index 5889b30..9b281f7 100644 --- a/docs/guide/media-sources.md +++ b/docs/guide/media-sources.md @@ -123,6 +123,7 @@ const sampleSource = new VideoSampleSource({ }); await sampleSource.add(videoSample); +videoSample.close(); // If it's not needed anymore // You may optionally force samples to be encoded as key frames: await sampleSource.add(videoSample, { keyFrame: true }); @@ -285,6 +286,7 @@ const sampleSource = new AudioSampleSource({ }); await sampleSource.add(audioSample); +audioSample.close(); // If it's not needed anymore ``` ### `AudioBufferSource` diff --git a/package-lock.json b/package-lock.json index 0655037..9c91c82 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mediabunny", - "version": "1.7.2", + "version": "1.7.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "mediabunny", - "version": "1.7.2", + "version": "1.7.3", "license": "MPL-2.0", "workspaces": [ "packages/*" @@ -5900,9 +5900,9 @@ } }, "node_modules/mediabunny": { - "version": "1.7.1", - "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.7.1.tgz", - "integrity": "sha512-y9s+Vf6TLhXeVjvlJFSmHRrwUQi2CJCMhxthm6nfhBO1XHJhcOqNOxu4CjhYyjoWgVZB5pkPKyWgZxpPOZfglQ==", + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/mediabunny/-/mediabunny-1.7.2.tgz", + "integrity": "sha512-M84Ice7IR1OpWmmDoUh691Hjpf+w2UTt+VWUH0WkiF5MdfFZ63OAF2TmOhbAEVZyvQ088xlvmaT01t00Qgy/QA==", "license": "MPL-2.0", "peer": true, "workspaces": [ @@ -9017,7 +9017,7 @@ }, "packages/mp3-encoder": { "name": "@mediabunny/mp3-encoder", - "version": "1.7.2", + "version": "1.7.3", "license": "MPL-2.0", "devDependencies": { "@types/emscripten": "^1.40.1" diff --git a/package.json b/package.json index 709c529..d258406 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "mediabunny", "author": "Vanilagy", - "version": "1.7.2", + "version": "1.7.3", "description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.", "type": "module", "workspaces": [ diff --git a/packages/mp3-encoder/package.json b/packages/mp3-encoder/package.json index 14f4776..961c5d2 100644 --- a/packages/mp3-encoder/package.json +++ b/packages/mp3-encoder/package.json @@ -1,7 +1,7 @@ { "name": "@mediabunny/mp3-encoder", "author": "Vanilagy", - "version": "1.7.2", + "version": "1.7.3", "description": "MP3 encoder extension for Mediabunny, based on LAME.", "main": "./dist/bundles/mediabunny-mp3-encoder.mjs", "module": "./dist/bundles/mediabunny-mp3-encoder.mjs", diff --git a/src/media-source.ts b/src/media-source.ts index 3860552..1f7295a 100644 --- a/src/media-source.ts +++ b/src/media-source.ts @@ -323,17 +323,17 @@ class VideoEncoderWrapper { if (this.customEncoder) { this.customEncoderQueueSize++; - const promise = this.customEncoderCallSerializer - .call(() => this.customEncoder!.encode(videoSample, finalEncodeOptions)) - .then(() => { - this.customEncoderQueueSize--; - if (shouldClose) { - videoSample.close(); - } - }) - .catch((error: Error) => { - this.encoderError ??= error; + // We clone the sample so it cannot be closed on us from the outside before it reaches the encoder + const clonedSample = videoSample.clone(); + + const promise = this.customEncoderCallSerializer + .call(() => this.customEncoder!.encode(clonedSample, finalEncodeOptions)) + .then(() => this.customEncoderQueueSize--) + .catch((error: Error) => this.encoderError ??= error) + .finally(() => { + clonedSample.close(); + // `videoSample` gets closed in the finally block at the end of the method }); if (this.customEncoderQueueSize >= 4) { @@ -440,6 +440,7 @@ class VideoEncoderWrapper { void this.muxer!.addEncodedVideoPacket(this.source._connectedTrack!, packet, meta); }, error: (error) => { + error.stack = new Error().stack; // Provide a more useful stack trace this.encoderError ??= error; }, }); @@ -483,7 +484,6 @@ class VideoEncoderWrapper { checkForEncoderError() { if (this.encoderError) { - this.encoderError.stack = new Error().stack; // Provide a more useful stack trace throw this.encoderError; } } @@ -939,17 +939,17 @@ class AudioEncoderWrapper { if (this.customEncoder) { this.customEncoderQueueSize++; - const promise = this.customEncoderCallSerializer - .call(() => this.customEncoder!.encode(audioSample)) - .then(() => { - this.customEncoderQueueSize--; - if (shouldClose) { - audioSample.close(); - } - }) - .catch((error: Error) => { - this.encoderError ??= error; + // We clone the sample so it cannot be closed on us from the outside before it reaches the encoder + const clonedSample = audioSample.clone(); + + const promise = this.customEncoderCallSerializer + .call(() => this.customEncoder!.encode(clonedSample)) + .then(() => this.customEncoderQueueSize--) + .catch((error: Error) => this.encoderError ??= error) + .finally(() => { + clonedSample.close(); + // `audioSample` gets closed in the finally block at the end of the method }); if (this.customEncoderQueueSize >= 4) { @@ -1128,6 +1128,7 @@ class AudioEncoderWrapper { void this.muxer!.addEncodedAudioPacket(this.source._connectedTrack!, packet, meta); }, error: (error) => { + error.stack = new Error().stack; // Provide a more useful stack trace this.encoderError ??= error; }, }); @@ -1266,7 +1267,6 @@ class AudioEncoderWrapper { checkForEncoderError() { if (this.encoderError) { - this.encoderError.stack = new Error().stack; // Provide a more useful stack trace throw this.encoderError; } }