【问题标题】:Capture first frame of an embedded video捕获嵌入视频的第一帧
【发布时间】:2010-09-20 06:08:38
【问题描述】:

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

【问题讨论】:

    标签: javascript html image video


    【解决方案1】:

    实际上,您可以使用 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);
    

    【讨论】:

    • 请查看这个...如果您找到解决方案,请回复
    • @hari:请注意,这需要非常最新的浏览器。
    • No problem Crowder ......我只是想让它在这种情况下工作,即使旧版浏览器不支持
    • 我附上了如何做到这一点的演示,请查看我的更新答案。
    • 嗨,Sam,您能否解释一下它是如何工作的以及它是否适用于 youtube 嵌入式视频。你能把视频也发一下吗
    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多