【发布时间】:2020-07-25 12:24:59
【问题描述】:
这是我从 Canvas 播放视频中捕获图像的代码:
let drawImage = function(time) {
prevCtx.drawImage(videoPlayer, 0, 0, w, h);
requestAnimationFrame(drawImage);
}
requestAnimationFrame(drawImage);
let currIndex = 0;
setInterval(function () {
if(currIndex === 30) {
currIndex = 0;
console.log("Finishing video...");
videoWorker.postMessage({action : "finish"});
} else {
console.log("Adding frame...");
// w/o this `toDataURL` this loop runs at 30 cycle / second
// so that means, this is the hot-spot and needs optimization:
const base64img = preview.toDataURL(mimeType, 0.9);
videoWorker.postMessage({ action: "addFrame", data: base64img});
currIndex++;
}
}, 1000 / 30)
目标是在每 30 帧(应该是 1 秒)时触发转码添加的帧。
这里的问题是preview.toDataURL(mimeType, 0.9); 至少增加了 1 秒,如果没有它,日志显示currIndex === 30 每秒都会被触发。能够捕获至少约 30 FPS 图像的最佳方法是什么。 从 HTML Canvas 捕获图像的最快方法是什么,它不会成为实时视频转码过程的瓶颈?
【问题讨论】:
-
.toBlob怎么样?.captureStream()看起来也很有前途...
标签: javascript performance animation html5-canvas html5-video