Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>1) Loading the video</p> <p>To capture the pixels, you need to load the video into a <code>&lt;video&gt;</code> tag, not an <code>iframe</code> or <code>object</code> or <code>embed</code>. And the file needs to come from a web server, which is the same one the web page itself is on (unless you use <a href="https://developer.mozilla.org/en-US/docs/CORS_Enabled_Image" rel="noreferrer">cross-origin headers</a>, 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.</p> <p>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 <a href="https://developer.mozilla.org/en-US/docs/Using_HTML5_audio_and_video" rel="noreferrer">multiple sources</a>, using webm, mp4 and ideally ogg/theora.</p> <pre><code>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 </code></pre> <p>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.</p> <pre><code>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. </code></pre> <p>3) Capture the image to the canvas and save it to a file. Put this code in an <code>onclick</code> 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.)</p> <pre><code>//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' </code></pre> <p>4) Exporting the image file</p> <p>The jpg file is now saved as a <a href="http://en.wikipedia.org/wiki/Data_Uri" rel="noreferrer">Data URI</a>, 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 <code>img</code> element by just setting it to the src: <code>myImage.src = dataUri;</code>.</p> <p>If you want to save it to a file, you pretty much need to upload it to a server. Here is a <a href="http://www.re-cycledair.com/html-5-canvas-saving-to-a-file-with-php" rel="noreferrer">good tutorial</a> on how to do that.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload