捕获嵌入视频的第一帧

har*_*ari 5 html javascript video image

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

Sam*_*Day 18

实际上,非常确定你可以使用HTML5.看一下这个链接:HTML5视频销毁.

它每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);
Run Code Online (Sandbox Code Playgroud)