捕获嵌入视频的第一帧

时间:2010-09-20 06:08:38

标签: javascript html image video

我想在不使用任何服务器端脚本的情况下将嵌入视频的第一帧捕获为图像。可能用javascript,有可能吗?

1 个答案:

答案 0 :(得分:17)

实际上,非常确定您可以使用HTML5。请看一下这个链接:HTML5 Video Destruction

它每33毫秒将视频帧复制到另一个画布中。你可以玩这个,看看你是否可以在视频开始运行时捕获第一帧。

如果你愿意,我可以进一步研究这个问题(它让我着迷)

编辑:哦,我的上帝,这很酷。我想出了一个解决方案。转到sambro.is-super-awesome.com/videofirstframe/

您需要在Google Chrome中打开它。 Firefox不支持mp4(我认为)。

我第一次使用HTML5做过任何事情,我不能等到大多数浏览器出现这种情况:)

编辑编辑:好的我也上传了此视频的.ogg版本,并设置了我的网络服务器以正确处理视频类型,Firefox也应该在这个小例子中工作。

编辑编辑编辑:Nitpickers想在这里找到源代码,好在这里:

// 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);