How to get HTML5 video thumbnail without using pos

2019-01-26 14:41发布

问题:

I have embedded HTML5 video with mp4 format. How to get thumbnail image like poster without using "poster" attribute. This problem coming on Safari and iOS. I have added video like below mentioned code.

<video height=350 id='TestVideo' webkit-playsinline><source src='test.mp4' type=video/mp4></video>

On Chrome, IE, Firefox first frame of video coming as thumbnail image, but not coming on Safari and iOS.

回答1:

If you want to do this without storing server side images it is possible, though a bit clunky... uses a canvas to record the first frame of the video and then overlay that over the video element. It may be possible to use the URL for the Canvas as a source for the Poster (eg video.poster = c.toDataURL();) but that requires correct CORS setup (not sure if the video was on your own server, and if you have control over that, so took the safest option). This will work best if video is correctly encoded for streaming (so MOOV atom is at the front of the video, otherwise it will be slow to draw the overlay - see this answer for more detail)

The HEAD contains styling for the video and the overlay (you will need to adjust sizing and position to suit your application)

<head>
  <style>
    video_box{
      float:left;
    }
    #video_overlays {
      position:absolute;
      float:left;
      width:640px;
      min-height:264px;
      background-color: #000000;
      z-index:300000;
    }
  </style>
</head>

In the BODY you will need the div for the overlay and the video. The overlay div has an onclick handler to hide the overlay and start the video playing

<div id="video_box">
  <div id="video_overlays" onclick="this.style.display='none';document.getElementById('video').play()"></div>

    <video id="video" controls width="640" height="264">
      <source src="BigBuck.mp4" type='video/mp4'>
    </video>

  </div>
</div>

Finally you will need code that will load the video, seek to the first frame and load the visual into a canvas that you then insert into the overlay

<script>

function generateOverlay() {
    video.removeEventListener('seeked',generateOverlay);  / tidy up the event handler so it doesn't redraw the overlay every time the user manually seeks the video
    var c = document.createElement("canvas");
    var ctx = c.getContext("2d");
    c.width = 640;
    c.height = 264;
    ctx.drawImage(video, 0, 0, 640, 264); // take the content of the video frame and place in canvas
    overlay.appendChild(c); // insert canvas into the overlay div
}

    // identify the video and the overlay
    var video = document.getElementById("video");
    var overlay = document.getElementById("video_overlays");

    // add a handler that when the metadata for the video is loaded it then seeks to the first frame
    video.addEventListener('loadeddata', function() {
        this.currentTime = 0;
    }, false);

    // add a handler that when correctly seeked, it generated the overlay
    video.addEventListener('seeked', function() {
    // now video has seeked and current frames will show
    // at the time as we expect
        generateOverlay();
    }, false);

    // load the video, which will trigger the event handlers in turn
    video.load();

</script>


回答2:

Source: How to set the thumbnail image on HTML5 video?

This worked for me:

<img src="thumbnail.png" alt="thumbnail" />
/* code for the video goes here */

Now using jQuery play the video and hide the image as

$("img").on("click", function() {
  $(this).hide();
  // play the video now..
})