Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic video thumbnail on hover

I'm creating a video player with HTML5 and Javascript and I'm wondering if anyone has solved the challenge of creating dynamic thumbnails.

Mimicking youtube video player where you hover over progress bar and a popup shows with a thumbnail. I know that youtube saves out an image and repositions the sprite in the thumbnail viewer frame based on position hovered over.

Is this viable to do with JS and canvas?

What I'm doing now is... hovering over my progress bar creates a copy of the video element. I then set the currentTime on the copied element. I then grab a snapshot using canvas. I'm not sure why, but the images seem to be blank.

Maybe someone has run into this problem?

like image 807
ZiggidyCreative Avatar asked Nov 18 '15 02:11

ZiggidyCreative


Video Answer


2 Answers

YouTube have pre rendered thumbs that are stored in a single image in a grid with ten columns and how ever many rows are needed. The thum images I have seen are low quality jpg 800 pixels across giving thumbs 80 pixels by ?? (depending on aspect) When the user hovers the thumb closest to the that time is displayed.

Creating the thumbs at the client side will be problematic because video are not entirely random access. Seeking to a random location involves the video moving to the nearest previous keyframe and then decoding all frames till it gets to the frame you have requested. Depending on the format, encoding options, the spacing of key frames (if any rolling eyes), and if the seek location has been loaded, seeking to some location can be very slow. Getting a set of thumbs for a video can take a long time. The additional problem with the HTML5 video API is that it has only one playback channel, so while you are seeking for thumbs you can not watch the video.

You problem with blanks may be due to not waiting for the seek event.

Try

video.addEventListener("seeked",function(e){
    // snap shot code here
});

But this does not always work I have found. So it pays to listen to all the appropriate events, and only seek when the video is ready. Here is a list of media events that will help.

As videos don't change your best bet is to create the thumbs on your server after the video has been uploaded. Low quality jpgs seem to be the go and will be loaded by the client much sooner than even a fraction of the video has been.

like image 114
Blindman67 Avatar answered Oct 29 '22 11:10

Blindman67


However, this can be achieved by using a combination of HTML5 canvas. You will have to do the following.

  1. add an timeupdate event listener to the HTML5 video
  2. at each 1 sec, grab the current time and create an element (span is this case) and bind the value of the current time in a special attribute of the newly created span
  3. Append each created span element to your HTML5 progress element (I suppose you're using 'div' and not 'progress' element.
  4. Add an mouseenter event listener to the created span.
  5. Whenever the user hovers the progress bar, the mouse would eventually touches one of the span. Then, dynamically create a video element and set the src attribute with the main HTML5 video source. Then, set the currentTime to the value of the hovered span and snapshot it to an already canvas element.
  6. There, you show the user the current frameRate.

Scripting is all about manipulations and gradually processes of putting pieces of codes to work.

like image 23
Nwaedobadoo Avatar answered Oct 29 '22 12:10

Nwaedobadoo