You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been playing around with saving what is seen on a canvas into a video file using the new WebCodec APIs.
I have been successful at creating a webm file using the webm-muxer project.
But I'm now looking into generating a mp4 instead.
This is the code for using webm-muxer to record a htmlcanvas into a webm file.
importWebMMuxerfrom'webm-muxer'// to get showSaveFilePicker, and the VideoEncoder types, install and add this to tsconfig.json:// "types": ["@types/wicg-file-system-access", "@types/dom-webcodecs"],typeCurrentRecordingCallback=()=>voidletstopRecordingCallback: CurrentRecordingCallback|undefinedletrenderFrame=falseexportinterfaceRecordOptions{canvas: HTMLCanvasElementwidth: numberheight: number}exportasyncfunctionrecord(options: RecordOptions){letfileHandle=awaitwindow.showSaveFilePicker({suggestedName: `myvideo.webm`,types: [{description: 'Video File',accept: {'video/webm': ['.webm']},},],})letfileWritableStream=awaitfileHandle.createWritable()letmuxer=newWebMMuxer({target: fileWritableStream,video: {//codec: 'V_AV1',codec: 'V_VP9',width: options.width,height: options.height,},})letvideoEncoder=newVideoEncoder({output: (chunk,meta)=>muxer.addVideoChunk(chunk,meta),error: (e)=>console.error(e),})videoEncoder.configure({// av01 is the codec AV1// 0 indicates Profile 0// 16M indicates level identifier 16 (Level 6.0), can also be 8M, 4M// 08 indicates the bit depth (8-bit)//codec: 'av01.0.31M.10',//codec: 'av01.0.16M.08',codec: 'vp09.00.10.08',width: options.width,height: options.height,displayWidth: options.width,displayHeight: options.height,bitrateMode: 'variable',bitrate: 20_000_000,framerate: 60,// bitrateMode: 'constant',latencyMode: 'quality',// hardwareAcceleration: 'prefer-hardware',})conststartTime=performance.now()letcurrentFrame=0constfps=60constframeTime=1/fpsconstframeTimeMicroSeconds=Math.floor(frameTime*1_000_000)constrender=(timestamp: number=performance.now())=>{timestamp=currentFrame*frameTimeMicroSecondsconstduration=frameTimeMicroSecondsconstframe=newVideoFrame(options.canvas,{displayWidth: options.width,displayHeight: options.height,
timestamp,
duration,})// create a keyframe every 150 framesconstkeyFrame=currentFrame%150==0videoEncoder.encode(frame,{ keyFrame })frame.close()currentFrame++renderFrame&&requestAnimationFrame(render)}stopRecordingCallback=async()=>{constduration=performance.now()-startTimerenderFrame=falsestopRecordingCallback=undefinedawaitvideoEncoder.flush()videoEncoder.close()muxer.finalize()fileWritableStream.close()}// start renderingrenderFrame=truerender()returnstopRecordingCallback}exportasyncfunctionstopRecording(){if(stopRecordingCallback){awaitstopRecordingCallback()}}
I know how to reconfigure the VideoEncoder to output h264 data instead, but I'm not sure on how to setup the mp4 muxer and pass data to it, as I do above with muxer.addVideoChunk
How could I do something like this using your library?
The text was updated successfully, but these errors were encountered:
Hi!
I have been playing around with saving what is seen on a canvas into a video file using the new WebCodec APIs.
I have been successful at creating a webm file using the
webm-muxer
project.But I'm now looking into generating a mp4 instead.
This is the code for using webm-muxer to record a htmlcanvas into a webm file.
I know how to reconfigure the VideoEncoder to output h264 data instead, but I'm not sure on how to setup the mp4 muxer and pass data to it, as I do above with
muxer.addVideoChunk
How could I do something like this using your library?
The text was updated successfully, but these errors were encountered: