如何拍摄基于HTML5-JavaScript的视频播放器的快照? [英] How to take a snapshot of HTML5-JavaScript-based video player?

查看:106
本文介绍了如何拍摄基于HTML5-JavaScript的视频播放器的快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我有一个带JavaScript功能的HTML5页面,可以播放wmv视频文件。
当视频播放时(暂停或不播放)我需要拍摄快照,并以任何图像格式保存为JPG或BMP格式。
任何帮助将升值。
谢谢。

 <!DOCTYPE HTML> 

< html>
< head>
< title> HTML5视频< / title>
< script type =text / javascript>
function checkPlay(){
var myVideo = document.getElementById(myVid);
var ytStr ='< iframe width =500height =500src =C:\XA0002.wmvframeborder =0allowfullscreen>< / iframe>';
try {
var canPlay = document.getElementsByTagName('video')[0] .canPlayType(video / wmv);
if((canPlay ==no)||(canPlay ==)){
myVideo.innerHTML = ytStr;
}
MediaElement(v,{success:function(media){media.play();}});
} catch(err){
myVideo.innerHTML = ytStr;
}
}
< / script>
< / head>
< body onload =checkPlay()>
< div id =myVid/>

< / body>
< / html>


解决方案

要将视频帧复制到图像文件加载视频,将图像复制到画布并将其导出到文件。这是完全可能的,但有一些地方你可能会遇到麻烦,所以让我们一步一步。



1)加载视频



要捕捉像素,您需要将视频加载到< video> 标记中,而不是 iframe object 嵌入。这个文件需要来自一个网页服务器,它与网页本身一样(除非你使用跨域标题,这很复杂,可能无法在您的浏览器中使用。出于安全原因,这受到浏览器的限制。您的代码从本地文件系统加载视频,而不是



您还需要加载正确的文件格式,IE9 +可以播放WMV,但其他浏览器不太可能,如果可以,加载多个来源,使用webm,mp4和理想的ogg / theora。

  var container = document.getElementById(myVid),
video = document.createElement('video'),
canCapture = true;
if(!video.canPlayType('video / wmv')){
/ *如果你没有多个源,你可以加载一个Flash fa llback这里
(比如jPlayer),但是你将无法捕获帧* /
canCapture = false;
return;
}
video.src ='myvideo.wmv';
container.appendChild(video);
video.play(); //或者把它放在按钮点击处理程序中,如果你想要自己的控件

2)接下来,创建画布和绘画上下文。实际上,您甚至不需要将其附加到DOM,但您需要将其设置为要保存最终图像的大小。对于这个例子,我们假设你有一个固定的维度,但如果你愿意,你可以将它设置为视频大小的倍数。只要确保在视频中的'loadedmetadata'事件中运行该视频,因为视频尺寸不会立即显示。

  var canvas = document.createElement('canvas'); 
canvas.width = 640;
canvas.height = 480;
var ctx = canvas.getContext('2d');
//如果要预览捕获的图像,
//将画布附加到DOM的某处,您可以看到它。



<3>将图像捕捉到画布并将其保存到文件中。将此代码放在按钮上或计时器内的 onclick 事件中 - 但是您想要决定何时捕获图像。使用drawImage方法。 ([本文]提供了一个很好的解释,包括安全问题。视频与图像相同。)

  //将图像绘制到画布上。缩放到目标尺寸
ctx.drawImage(video,0,0,canvas.width,canvas.height);

//转换为所需的文件格式
var dataURI = canvas.toDataURL('image / jpeg'); //也可以使用'image / png'

4)导出图像文件



jpg文件现在保存为数据URI ,这是一个很长的JavaScript字符串,表示完整的二进制文件的编码版本。这取决于你如何处理它。您可以通过将它设置为src: myImage.src = dataUri; 来将它直接放置到 img 元素中。



如果要将其保存到文件中,则几乎需要将其上传到服务器。这是很好的教程如何做到这一点。



像往常一样,以上所有内容仅限于支持它的浏览器。如果你要坚持wmv视频,你几乎只能使用Internet Explorer 9+。 6-8不支持视频标签或画布。如果您可以添加更多视频格式,则可以使用Firefox(3.5+)和Chrome。


Actually, i have a HTML5 page with JavaScript function that allow me to play a wmv video file. I need to take a snapshot when the video is playing (with pause or without) and saved in any image format JPG or BMP whatever. Any help will appreciate. Thank you.

<!DOCTYPE HTML>

    <html>
        <head>
            <title>HTML5 video</title>        
    <script type="text/javascript">
        function checkPlay() {
            var myVideo = document.getElementById("myVid");
            var ytStr = '<iframe width="500" height="500" src="C:\XA0002.wmv" frameborder="0" allowfullscreen></iframe>';
            try {
                var canPlay = document.getElementsByTagName('video')[0].canPlayType("video/wmv");
                if ((canPlay == "no") || (canPlay == "")) {
                    myVideo.innerHTML = ytStr;
                }
                new MediaElement(v, { success: function (media) { media.play(); } });
            } catch (err) {
                myVideo.innerHTML = ytStr;
            }
        }
    </script>
        </head>
        <body onload="checkPlay()">
          <div id="myVid"/>

      </body>
    </html>

解决方案

To copy an video frame to an image file involves properly loading the video, copying the image to a canvas and exporting it to a file. This is totally possible, but there are a few places where you can run into trouble, so let's take it one step at a time.

1) Loading the video

To capture the pixels, you need to load the video into a <video> tag, not an iframe or object or embed. And the file needs to come from a web server, which is the same one the web page itself is on (unless you use cross-origin headers, which is complicated and may not work in your browser. This is restricted by the browser for security reasons. Your code loads the video from the local file system, which won't work.

You also need to load the right file format. IE9+ may play WMV, but it's unlikely that any other browser will. If you can, load up multiple sources, using webm, mp4 and ideally ogg/theora.

var container = document.getElementById("myVid"),
    video = document.createElement('video'),
    canCapture = true;
if (!video.canPlayType('video/wmv')) {
    /* If you don't have multiple sources, you can load up a Flash fallback here
       (like jPlayer), but you won't be able to capture frames */
    canCapture = false;
    return;
}
video.src = 'myvideo.wmv';
container.appendChild(video);
video.play(); //or put this in a button click handler if you want your own controls

2) Next, create the canvas and a drawing context. You actually don't need to even attach it to the DOM, but you do need to set it to the size at which you want to save the resulting image. For this example, we'll assume you have a fixed dimension in mind, but if you want, you can set it to a multiple of the video size. Just make sure you run that inside a 'loadedmetadata' event on the video, because the video dimensions won't be available right away.

var canvas = document.createElement('canvas');
canvas.width = 640;
canvas.height = 480;
var ctx = canvas.getContext('2d');
// if you want to preview the captured image,
// attach the canvas to the DOM somewhere you can see it.

3) Capture the image to the canvas and save it to a file. Put this code in an onclick event on a button or inside a timer - however you want to decided when the image gets captured. Use the drawImage method. ([This article] provides a good explanation, including the security concerns. It's the same for video as for an image.)

//draw image to canvas. scale to target dimensions
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

//convert to desired file format
var dataURI = canvas.toDataURL('image/jpeg'); // can also use 'image/png'

4) Exporting the image file

The jpg file is now saved as a Data URI, which is a long javascript string representing an encoded version of the full binary file. It's up to you what to do with it. You can place it directly into an img element by just setting it to the src: myImage.src = dataUri;.

If you want to save it to a file, you pretty much need to upload it to a server. Here is a good tutorial on how to do that.

As usual, all of the above are restricted to browsers that support it. If you're going to stick to wmv video, you're pretty much limited to Internet Explorer 9+. 6-8 don't support either the video tag or the canvas. If you can add more video formats, you can use Firefox (3.5+) and Chrome.

这篇关于如何拍摄基于HTML5-JavaScript的视频播放器的快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆