gpt4 book ai didi

Javascript 如何从视频中提取帧?

转载 作者:行者123 更新时间:2023-11-28 15:44:37 29 4
gpt4 key购买 nike

您好,我有以下 JS 代码可以从文件输入创建视频:

<canvas  id="prevImgCanvas">Your browser does not support the HTML5 canvas tag.</canvas> 

<input id="videoage" type="file" name="video" class="chooseNail" accept="video/*" style="display:none;" onchange="loadSnippetThumb(event)">
<label for="videoage" id="labelvideo" >Choose Video</label>

var loadSnippetThumb = function(event) {
var c = document.getElementById("prevImgCanvas");
var context = c.getContext('2d');
var url = URL.createObjectURL(event.target.files[0]);
var video = document.getElementById('video222');
video.src = url;
video.autoPlay = true;

}

这将创建一个在屏幕上播放的视频,用户已选择使用文件输入上传该视频。我不想让视频显示在屏幕上,我只是希望能够挑选出视频的随机帧并将其显示在 Canvas 上。我怎样才能做到这一点?谢谢。

最佳答案

seekedloadeddata 事件上添加监听器,并在 loadeddata 事件中设置 currentTime 以获得正确的 video.durationseeked 事件此时将被调用并绘制您的视频帧:

var video = document.createElement("video");

var canvas = document.getElementById("prevImgCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

video.addEventListener('loadeddata', function() {
reloadRandomFrame();
}, false);

video.addEventListener('seeked', function() {
var context = canvas.getContext('2d');
context.drawImage(video, 0, 0, canvas.width, canvas.height);
}, false);

var playSelectedFile = function(event) {
var file = this.files[0];
var fileURL = URL.createObjectURL(file);
video.src = fileURL;
}

var input = document.querySelector('input');
input.addEventListener('change', playSelectedFile, false);

function reloadRandomFrame() {
if (!isNaN(video.duration)) {
var rand = Math.round(Math.random() * video.duration * 1000) + 1;
video.currentTime = rand / 1000;
}
}
<input type="file" accept="video/*" />
<input type="submit" onClick="reloadRandomFrame()" value="load random frame" /><br/>
<canvas id="prevImgCanvas">Your browser does not support the HTML5 canvas tag.</canvas>

JSFiddle 中的相同代码 here

关于Javascript 如何从视频中提取帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43007634/

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