javascript - 如何从组件 <video></video> 获取帧

标签 javascript html video

我想从 HTML 获取框架 <video></video> javascript 中的组件,以便我可以处理它们然后输出到 Canvas

最佳答案

看看这个代码笔:Demo

var videoId = 'video';
    var scaleFactor = 0.25;
    var snapshots = [];

    /**
     * Captures a image frame from the provided video element.
     *
     * @param {Video} video HTML5 video element from where the image frame will be captured.
     * @param {Number} scaleFactor Factor to scale the canvas element that will be return. This is an optional parameter.
     *
     * @return {Canvas}
     */
    function capture(video, scaleFactor) {
        if (scaleFactor == null) {
            scaleFactor = 1;
        }
        var w = video.videoWidth * scaleFactor;
        var h = video.videoHeight * scaleFactor;
        var canvas = document.createElement('canvas');
        canvas.width = w;
        canvas.height = h;
        var ctx = canvas.getContext('2d');
        ctx.drawImage(video, 0, 0, w, h);
        return canvas;
    }

    /**
     * Invokes the <code>capture</code> function and attaches the canvas element to the DOM.
     */
    function shoot() {
        var video = document.getElementById(videoId);
        var output = document.getElementById('output');
        var canvas = capture(video, scaleFactor);
        canvas.onclick = function() {
            window.open(this.toDataURL(image/jpg));
        };
        snapshots.unshift(canvas);
        output.innerHTML = '';
        for (var i = 0; i < 4; i++) {
            output.appendChild(snapshots[i]);
        }
    }

(function() {
  var captureit = document.getElementById('cit');
  captureit.click();
})();

将视频网址更改为:http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4并且您有一个现场演示示例

关于javascript - 如何从组件 <video></video> 获取帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55314695/

相关文章:

html - 如何制作圆形图像

javascript - 如何防止 Cufon 在子菜单中被继承?

javascript - 如何调用使用对象语法定义的对象中的同级方法?

html - CSS 网格最小跨度 3,如果类对象只是网格行中的对象,则填充行中的所有网格元素

html - 将边框半径和填充添加到单个表格行

javascript - 看不见时暂停/恢复 Youtube 视频(滚动)

iphone - 在 HTML 5 网页中播放来自 iPad 的本地视频

javascript - 模态打开时在 Bootstrap 模态窗口中自动播放视频

javascript - 在 jQuery 中选择单选按钮后锁定选择元素的方法

javascript - 像普通子方法一样通过子对象访问父原型(prototype)方法