Skip to content

Commit

Permalink
feat: add mp4-muxer and move mp4-wasm to its own encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed Apr 14, 2023
1 parent 85a3d3e commit 0693ad0
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 102 deletions.
77 changes: 48 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"canvas-screenshot": "^4.1.0",
"gifenc": "^1.0.3",
"h264-mp4-encoder": "^1.0.12",
"mp4-muxer": "^1.0.2",
"webm-muxer": "^2.2.3"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions src/encoders/Encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class Encoder {

async getFileHandle(name, options) {
if (this.directoryHandle) {
return await this.directoryHandle.getFileHandle(name, {
create: true,
});
return await this.directoryHandle.getFileHandle(name, { create: true });
}

if (!("showSaveFilePicker" in window)) return;
Expand Down
59 changes: 59 additions & 0 deletions src/encoders/MP4WasmEncoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import MP4Wasm from "./mp4.embed.js"; // mp4-wasm

import Encoder from "./Encoder.js";

let mp4wasm;

class MP4WasmEncoder extends Encoder {
static supportedExtensions = ["mp4"];
static supportedTargets = ["in-browser"];

static defaultOptions = {
extension: MP4WasmEncoder.supportedExtensions[0],
groupOfPictures: 20,
flushFrequency: 10,
};

get frameMethod() {
return "bitmap";
}

constructor(options) {
super({ ...MP4WasmEncoder.defaultOptions, ...options });
}

async init(options) {
super.init(options);

mp4wasm ||= await MP4Wasm(); // { wasmBinary }
console.log(mp4wasm);

this.encoder = mp4wasm.createWebCodecsEncoder({
// codec: "avc1.420034", // Baseline 4.2
codec: "avc1.4d0034", // Main 5.2
width: this.width,
height: this.height,
fps: this.frameRate,
encoderOptions: {
framerate: this.frameRate,
...this.encoderOptions,
},
});
}

async encode(frame) {
await this.encoder.addFrame(frame);
}

async stop() {
let buffer = await this.encoder.end();

return buffer;
}

async dispose() {
this.encoder = null;
}
}

export default MP4WasmEncoder;

0 comments on commit 0693ad0

Please sign in to comment.