【发布时间】:2010-09-20 06:08:38
【问题描述】:
我想在不使用任何服务器端脚本的情况下将嵌入视频的第一帧捕获为图像。大概是用javascript,有可能吗?
【问题讨论】:
标签: javascript html image video
我想在不使用任何服务器端脚本的情况下将嵌入视频的第一帧捕获为图像。大概是用javascript,有可能吗?
【问题讨论】:
标签: javascript html image video
实际上,您可以使用 HTML5。看看这个链接:HTML5 Video Destruction。
它每 33 毫秒将视频帧复制到另一个画布中。您可以尝试一下,看看是否可以在视频开始播放时捕获第一帧。
如果你愿意,我可以进一步研究(这让我着迷)
编辑:天哪,这太酷了。我刚刚想出了一个解决方案。转至sambro.is-super-awesome.com/videofirstframe/
您需要在 Google Chrome 中打开它。 Firefox 不支持 mp4(我认为)。
我第一次用 HTML5 做任何事情,我不能等到这在大多数浏览器中 :)
编辑编辑:好的,我也上传了这个视频的 .ogg 版本,并设置了我的网络服务器以正确处理视频类型,Firefox 也应该在这个小例子中工作。
EDIT EDIT EDIT: 挑剔的人想要源代码,这里是:
// Create a video element.
var vid = document.createElement("video");
// We don't want it to start playing yet.
vid.autoplay = false;
vid.loop = false;
// No need for user to see the video itself.
vid.style.display = "none";
// This will fire when there's some data loaded for the video, should be at least 1 frame here.
vid.addEventListener("loadeddata", function()
{
// Let's wait another 100ms just in case?
setTimeout(function()
{
// Create a canvas element, this is what user sees.
var canvas = document.createElement("canvas");
// Set it to same dimensions as video.
canvas.width = vid.videoWidth;
canvas.height = vid.videoHeight;
// Put it on page.
document.getElementById("done").innerHTML = "";
document.getElementById("done").appendChild(canvas);
// Get the drawing context for canvas.
var ctx = canvas.getContext("2d");
// Draw the current frame of video onto canvas.
ctx.drawImage(vid, 0, 0);
// Done!
});
}, false);
// Have to include .ogv for firefox. I don't think this is working atm because my webserver isn't serving up
// videos properly.
if(BrowserDetect.browser == "Firefox")
{
var source = document.createElement("source");
source.src = "BigBuckBunny_640x360.ogv";
source.type = "video/ogg";
vid.appendChild(source);
}
else
{
var source = document.createElement("source");
source.src = "BigBuckBunny_640x360.mp4";
source.type = "video/mp4";
vid.appendChild(source);
}
// Add video to document to start loading process now.
document.body.appendChild(vid);
【讨论】: