Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

video html5: Is it possible to display thumbnail from video on a specific time?

Tags:

html

video

I use this to have a video player on browser

<video width="320" height="240" controls>
  <source src="video.mp4" type="video/mp4">
</video>

Before clicking play, it display an image from the very beginning of the video, but in most of my video, first several seconds is black screen. Is it possible to make it get image at a specific time of the video, like "0:00:15", without creating thumbnail for the video?

like image 730
Mee Avatar asked Mar 24 '14 11:03

Mee


People also ask

How do I make a video start at a certain time in HTML?

autoplay=1 to the end of the sharing page link. This URL argument will make the video play automatically when the page loads. If you want the video to start playing at a specific time, also add second=15 to the end of the URL.

How do I show video thumbnails in HTML?

Approach: Sometimes the user wants to display a specific image of his choice as the thumbnail of the video. This can be simply done by using the poster attribute. All you have to do is create a poster attribute in the video tag and place the URL of the thumbnail image in it.


2 Answers

I just want to add one more thing in this I guess you forgot to add preload="metadata" attribute in video tag like the below

<video preload="metadata" width="320" height="240" controls>
      <source src="video.mp4#t=15" type="video/mp4">
    </video>

and one more thing I want to add that this will not starts video after 15 seconds, this will only take an screenshot from video and make it as a first view of the video

like image 179
Sahil Lalotra Avatar answered Sep 23 '22 03:09

Sahil Lalotra


Maybe this helps: (I have not tested it. Also you might be able to set the "poster" attribute of the video to the src of the image object. Just try it. =) )

<video width="320" height="240" controls id="video">
    <source src="video.mp4" type="video/mp4">
</video>

$(document).ready(function() {


            var time = 15;
            var scale = 1;

            var video_obj = null;

            document.getElementById('video').addEventListener('loadedmetadata', function() {
                 this.currentTime = time;
                 video_obj = this;

            }, false);

            document.getElementById('video').addEventListener('loadeddata', function() {
                 var video = document.getElementById('video');

                 var canvas = document.createElement("canvas");
                 canvas.width = video.videoWidth * scale;
                 canvas.height = video.videoHeight * scale;
                 canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);

                 var img = document.createElement("img");
                img.src = canvas.toDataURL();
                $('#thumbnail').append(img);

                video_obj.currentTime = 0;

            }, false);

        });

Source 1 Source 2

like image 20
Dafen Avatar answered Sep 23 '22 03:09

Dafen