Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an image from the video

I want to grab an image from a video based on the current time of the video upon button click. I'm currently using the video tag to play the video but have not seen any resource for grabbing an image from that.

<video
    id="video-player"
    class="video-player"
    width="640"
    controls="controls"
    muted=true>
    <source type="video/mp4" src="http://media.w3.org/2010/05/sintel/trailer.mp4" id="mp4"></source>
    <source type="video/webm" src="http://media.w3.org/2010/05/sintel/trailer.webm" id="webm"></source>
    <source type="video/ogg" src="http://media.w3.org/2010/05/sintel/trailer.ogv" id="ogv"></source>
    <p>Your user agent does not support the HTML5 Video element.</p>
</video>
like image 603
Danson Avatar asked May 19 '14 19:05

Danson


People also ask

Can you turn a video into a picture?

You can make a video into a Live Photo using free apps on both iPhone and Android. Once you've made your video into a Live Photo, you can set it as your phone's background. To make a video into a Live Photo, use the app intoLive on an iPhone or TurnLive on an Android.


1 Answers

This can be done via canvas. Let's say you have a video

<video id="video" controls="controls">
  ....
</video>

You can do the following:

const video = document.getElementById("video");

const canvas = document.createElement("canvas");
// scale the canvas accordingly
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// draw the video at that frame
canvas.getContext('2d')
  .drawImage(video, 0, 0, canvas.width, canvas.height);
// convert it to a usable data URL
const dataURL = canvas.toDataURL();

You can then do whatever you want with dataURL, such as displaying it as an image:

var img = document.createElement("img");
img.src = dataURL;

Inspiration drawn from odetocode

like image 156
nolawi Avatar answered Sep 23 '22 23:09

nolawi