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

前端 未结 5 1436
我寻月下人不归
我寻月下人不归 2020-12-06 02:32

I use this to have a video player on browser

5条回答
  •  甜味超标
    2020-12-06 02:51

    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. =) )

    
    

    $(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

提交回复
热议问题