Closed chipweinberger closed 8 months ago
got it working. needed to use file.save
, and copy some more options around.
this.exportVideo = async () => {
// Initialization
let w = 320;
let h = 240;
let fps = 60;
let durationInSeconds = 2;
let totalFrames = fps * durationInSeconds;
// MP4Box Setup
let file = MP4Box.createFile();
let trackOptions = {
timescale: 1_000_000,
width: w,
height: h,
nb_samples: totalFrames,
codec: 'avc1.42001E',
};
let sampleOptions = { duration: 1_000_000 / fps };
let trackId = null;
// Configure Video Encoder
let videoEncoder = new VideoEncoder({
output: (chunk, config) => {
let buffer = new ArrayBuffer(chunk.byteLength);
chunk.copyTo(buffer);
if (trackId === null) {
trackOptions.avcDecoderConfigRecord = config.decoderConfig.description;
trackId = file.addTrack(trackOptions);
}
sampleOptions.dts = chunk.timestamp * 1_000;
sampleOptions.cts = chunk.timestamp * 1_000;
sampleOptions.is_sync = chunk.type === 'key';
file.addSample(trackId, buffer, sampleOptions);
},
error: error => console.error('VideoEncoder error:', error)
});
await videoEncoder.configure({
codec: 'avc1.42001E',
width: w,
height: h,
bitrate: 1_000_000,
framerate: fps
});
// Render and Encode Frames
for (let frameIndex = 0; frameIndex < totalFrames; frameIndex++) {
this.setTime(frameIndex / fps);
this.draw();
let bitmap = await createImageBitmap(gl.canvas);
let videoFrame = new VideoFrame(bitmap, { timestamp: 1_000 * frameIndex / fps});
videoEncoder.encode(videoFrame);
videoFrame.close();
bitmap.close();
}
// Wait for the encoder to finish
await videoEncoder.flush();
// Save the file
file.save('exportedVideo.mp4');
};
version: 0.5.2
Are there any steps I am missing? It's not working. The output file is 9 bytes!
I am doing this in the web browser. Thanks for this great library.
Basic steps:
I've looked here: https://github.com/gpac/mp4box.js/issues/243
THIS CODE DOES NOT WORK