通过文件输入从视频文件创建缩略图

时间:2014-05-13 20:21:24

标签: javascript html5 video thumbnails video-thumbnails

我正在尝试从表单input type='file'创建视频文件(mp4,3gp)中的缩略图预览。许多人说这只能在服务器端完成。我发现这很难相信,因为我刚刚使用HTML5 Canvas和Javascript来看到这个小提琴。

Thumbnail Fiddle

唯一的问题是,这需要视频存在,用户在点击按钮捕捉缩略图之前点击播放。我想知道是否有办法在没有玩家在场且用户点击按钮的情况下获得相同的结果。例如:用户单击文件上载并选择视频文件,然后生成缩略图。欢迎任何帮助/想法!

5 个答案:

答案 0 :(得分:59)

Canvas.drawImage必须基于html内容。

source

here is a simplier jsfiddle

//and code
function capture(){
    var canvas = document.getElementById('canvas');
    var video = document.getElementById('video');
    canvas.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
}

此解决方案的优点是您可以根据视频的时间选择所需的缩略图。

答案 1 :(得分:11)

最近需要这个并进行了一些测试并将其降至最低限度,请参阅https://codepen.io/aertmann/pen/mAVaPx

它有一些限制,但目前相当不错的浏览器支持:Chrome,Firefox,Safari,Opera,IE10,IE11,Android(Chrome),iOS Safari(10 +)。

 video.preload = 'metadata';
 video.src = url;
 // Load video in Safari / IE11
 video.muted = true;
 video.playsInline = true;
 video.play();

答案 2 :(得分:10)

最近需要此功能,因此我编写了一个函数,以接收视频file和所需的timestamp,并在视频播放时返回image blob

示例用法:

try {
    // get the frame at 1.5 seconds of the video file
    const cover = await getVideoCover(file, 1.5);
    // print out the result image blob
    console.log(cover);
} catch (ex) {
    console.log("ERROR: ", ex);
}

功能:

function getVideoCover(file, seekTo = 0.0) {
    console.log("getting video cover for file: ", file);
    return new Promise((resolve, reject) => {
        // load the file to a video player
        const videoPlayer = document.createElement('video');
        videoPlayer.setAttribute('src', URL.createObjectURL(file));
        videoPlayer.load();
        videoPlayer.addEventListener('error', (ex) => {
            reject("error when loading video file", ex);
        });
        // load metadata of the video to get video duration and dimensions
        videoPlayer.addEventListener('loadedmetadata', () => {
            // seek to user defined timestamp (in seconds) if possible
            if (videoPlayer.duration < seekTo) {
                reject("video is too short.");
                return;
            }
            // delay seeking or else 'seeked' event won't fire on Safari
            setTimeout(() => {
              videoPlayer.currentTime = seekTo;
            }, 200);
            // extract video thumbnail once seeking is complete
            videoPlayer.addEventListener('seeked', () => {
                console.log('video is now paused at %ss.', seekTo);
                // define a canvas to have the same dimension as the video
                const canvas = document.createElement("canvas");
                canvas.width = videoPlayer.videoWidth;
                canvas.height = videoPlayer.videoHeight;
                // draw the video frame to canvas
                const ctx = canvas.getContext("2d");
                ctx.drawImage(videoPlayer, 0, 0, canvas.width, canvas.height);
                // return the canvas image as a blob
                ctx.canvas.toBlob(
                    blob => {
                        resolve(blob);
                    },
                    "image/jpeg",
                    0.75 /* quality */
                );
            });
        });
    });
}

答案 3 :(得分:1)

显示缩略图的最简单方法是使用 <video> 标签本身。

<video src="http://www.w3schools.com/html/mov_bbb.mp4"></video>

如果您想要 x 秒的缩略图,请在 URL 中使用 #t

例如:

<video src="http://www.w3schools.com/html/mov_bbb.mp4#t=5"></video>

确保它不包含任何属性,例如 autoplaycontrols,并且不应将 source 标记作为子元素。

答案 4 :(得分:0)

有了jQuery Lib,您可以在这里使用我的代码。 $ video是一个Video元素。此函数将返回一个字符串

function createPoster($video) {
    //here you can set anytime you want
    $video.currentTime = 5;
    var canvas = document.createElement("canvas");
    canvas.width = 350;
    canvas.height = 200;
    canvas.getContext("2d").drawImage($video, 0, 0, canvas.width, canvas.height);
    return canvas.toDataURL("image/jpeg");;
}

用法示例:

$video.setAttribute("poster", createPoster($video));