gpt4 book ai didi

javascript从视频url生成视频缩略图

转载 作者:行者123 更新时间:2023-12-01 00:22:03 24 4
gpt4 key购买 nike

我将从 JavaScript 中的视频网址生成视频缩略图。我需要通过 ajax 完成此操作。所以我遵循了这种方法。

var src = thumbnail; ///video url not youtube or vimeo,just video on server
var video = document.createElement('video');

video.src = src;

video.width = 360;
video.height = 240;

var canvas = document.createElement('canvas');
canvas.width = 360;
canvas.height = 240;
var context = canvas.getContext('2d');

context.drawImage(video, 0, 0, canvas.width, canvas.height);
var dataURI = canvas.toDataURL('image/jpeg');
html += '<figure>';
html += '<img src="' + dataURI + '' + '" alt="' + item.description + '" />';
html += '<figurecaption>'+item.description+'</figurecaption>'
html += '</figure>';

但我得到的只是黑色图像。我猜是因为负载问题,但找不到解决方案。期待您的来信。

问候。

最佳答案

But what I get is just black image. I guess it's because load problem but can't find a solution. Looking forward to hearing from you.

这可能是因为您调用时尚未加载任何帧:

context.drawImage(video, 0, 0, canvas.width, canvas.height);

您应该在收到一些“loadeddata”事件后执行此操作(检查 https://developer.mozilla.org/en-US/docs/Web/Events/loadeddata )

所以你的代码将变成:

var src = thumbnail; ///video url not youtube or vimeo,just video on server
var video = document.createElement('video');

video.src = src;

video.width = 360;
video.height = 240;

var canvas = document.createElement('canvas');
canvas.width = 360;
canvas.height = 240;
var context = canvas.getContext('2d');

video.addEventListener('loadeddata', function() {
context.drawImage(video, 0, 0, canvas.width, canvas.height);
var dataURI = canvas.toDataURL('image/jpeg');
html += '<figure>';
html += '<img src="' + dataURI + '' + '" alt="' + item.description + '" />';
html += '<figurecaption>'+item.description+'</figurecaption>'
html += '</figure>';
});

请注意,这将是异步的,因此您可能必须更改对 html 变量的处理方式,而是设置 image.src = dataURI;,即 image 对 Image DOM 节点(您插入的节点)的引用

关于javascript从视频url生成视频缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40143958/

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