I want to capture a frame from video every 5 seconds.
(我想每5秒从视频中捕获一帧。)
This is my JavaScript code:
(这是我的JavaScript代码:)
video.addEventListener('loadeddata', function() {
var duration = video.duration;
var i = 0;
var interval = setInterval(function() {
video.currentTime = i;
generateThumbnail(i);
i = i+5;
if (i > duration) clearInterval(interval);
}, 300);
});
function generateThumbnail(i) {
//generate thumbnail URL data
var context = thecanvas.getContext('2d');
context.drawImage(video, 0, 0, 220, 150);
var dataURL = thecanvas.toDataURL();
//create img
var img = document.createElement('img');
img.setAttribute('src', dataURL);
//append img in container div
document.getElementById('thumbnailContainer').appendChild(img);
}
The problem I have is the 1st two images generated are the same and the duration-5 second image is not generated.
(我的问题是,第一次生成的两个图像是相同的,而持续时间为5秒的图像却没有生成。)
I found out that the thumbnail is generated before the video frame of the specific time is displayed in< video>
tag.(我发现缩略图是在< video>
标签中显示特定时间的视频帧之前生成的。)
For example, when video.currentTime = 5
, image of frame 0s is generated.
(例如,当video.currentTime = 5
,将生成帧0s的图像。)
(然后视频帧跳到时间5s。)
So whenvideo.currentTime = 10
, image of frame 5s is generated.(因此,当video.currentTime = 10
,将生成帧5s的图像。)