gpt4 book ai didi

javascript - 从JS中的视频中提取帧

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:25:45 29 4
gpt4 key购买 nike

我正在尝试构建一个从 JavaScript 视频中提取帧的函数。这是我想出的代码。该函数接收源和回调。从那里,我用源创建了一个视频,我想以设定的间隔在 Canvas 中绘制视频帧。

不幸的是,返回的帧都是透明图像。

我尝试了一些不同的东西,但我无法让它发挥作用。有人可以帮忙吗?谢谢。

const extractFramesFromVideo = function(src, callback) {
var video = document.createElement('video');
video.src = src;

video.addEventListener('loadeddata', function() {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.setAttribute('width', video.videoWidth);
canvas.setAttribute('height', video.videoHeight);

var frames = [];
var fps = 1; // Frames per seconds to
var interval = 1 / fps; // Frame interval
var maxDuration = 10; // 10 seconds max duration
var currentTime = 0; // Start at 0

while (currentTime < maxDuration) {
video.currentTime = currentTime;
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
var base64ImageData = canvas.toDataURL();
frames.push(base64ImageData);

currentTime += interval;

if (currentTime >= maxDuration) {
console.log(frames);
callback(frames);
}
}
});
}

export default extractFramesFromVideo;

最佳答案

在调整您的代码以等待 seeked 事件并修复一些零碎的地方后,它似乎工作正常:

async function extractFramesFromVideo(videoUrl, fps=25) {
return new Promise(async (resolve) => {

// fully download it first (no buffering):
let videoBlob = await fetch(videoUrl).then(r => r.blob());
let videoObjectUrl = URL.createObjectURL(videoBlob);
let video = document.createElement("video");

let seekResolve;
video.addEventListener('seeked', async function() {
if(seekResolve) seekResolve();
});

video.addEventListener('loadeddata', async function() {
let canvas = document.createElement('canvas');
let context = canvas.getContext('2d');
let [w, h] = [video.videoWidth, video.videoHeight]
canvas.width = w;
canvas.height = h;

let frames = [];
let interval = 1 / fps;
let currentTime = 0;
let duration = video.duration;

while(currentTime < duration) {
video.currentTime = currentTime;
await new Promise(r => seekResolve=r);

context.drawImage(video, 0, 0, w, h);
let base64ImageData = canvas.toDataURL();
frames.push(base64ImageData);

currentTime += interval;
}
resolve(frames);
});

// set video src *after* listening to events in case it loads so fast
// that the events occur before we were listening.
video.src = videoObjectUrl;

});
}

用法:

let frames = await extractFramesFromVideo("https://example.com/video.webm");

关于javascript - 从JS中的视频中提取帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44297164/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com