How to set the thumbnail image on HTML5 video?

前端 未结 7 934
失恋的感觉
失恋的感觉 2020-11-30 20:38

Is there a way to set thumbnail image on HTML5 video? I want to see some pictures before play. My code looks like this:

相关标签:
7条回答
  • 2020-11-30 20:54
    <?php
    $thumbs_dir = 'E:/xampp/htdocs/uploads/thumbs/';
    $videos = array();
    if (isset($_POST["name"])) {
     if (!preg_match('/data:([^;]*);base64,(.*)/', $_POST['data'], $matches)) {
      die("error");
     }
     $data = $matches[2];
     $data = str_replace(' ', '+', $data);
     $data = base64_decode($data);
     $file = 'text.jpg';
     $dataname = file_put_contents($thumbs_dir . $file, $data);
    }
    ?>
    //jscode
    <script type="text/javascript">
     var videos = <?= json_encode($videos); ?>;
     var video = document.getElementById('video');
     video.addEventListener('canplay', function () {
         this.currentTime = this.duration / 2;
     }, false);
     var seek = true;
     video.addEventListener('seeked', function () {
        if (seek) {
             getThumb();
        }
     }, false);
    
     function getThumb() {
         seek = false;
         var filename = video.src;
         var w = video.videoWidth;//video.videoWidth * scaleFactor;
         var h = video.videoHeight;//video.videoHeight * scaleFactor;
         var canvas = document.createElement('canvas');
         canvas.width = w;
         canvas.height = h;
         var ctx = canvas.getContext('2d');
         ctx.drawImage(video, 0, 0, w, h);
         var data = canvas.toDataURL("image/jpg");
         var xmlhttp = new XMLHttpRequest;
         xmlhttp.onreadystatechange = function () {
             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
             }
         }
         xmlhttp.open("POST", location.href, true);
         xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         xmlhttp.send('name=' + encodeURIComponent(filename) + '&data=' + data);
     }
      function failed(e) {
         // video playback failed - show a message saying why
         switch (e.target.error.code) {
             case e.target.error.MEDIA_ERR_ABORTED:
                 console.log('You aborted the video playback.');
                 break;
             case e.target.error.MEDIA_ERR_NETWORK:
                 console.log('A network error caused the video download to fail part-way.');
                 break;
             case e.target.error.MEDIA_ERR_DECODE:
                 console.log('The video playback was aborted due to a corruption problem or because the video used features your browser did not support.');
                  break;
              case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
                  console.log('The video could not be loaded, either because the server or network failed or because the format is not supported.');
                  break;
              default:
                  console.log('An unknown error occurred.');
                  break;
          }
    
    
      }
    </script>
    //Html
    <div>
        <video   id="video" src="1499752288.mp4" autoplay="true"  onerror="failed(event)" controls="controls" preload="none"></video>
    </div>
    
    0 讨论(0)
  • 2020-11-30 20:57

    Add poster="placeholder.png" to the video tag.

    <video width="470" height="255" poster="placeholder.png" controls>
        <source src="video.mp4" type="video/mp4">
        <source src="video.ogg" type="video/ogg">
        <source src="video.webm" type="video/webm">
        <object data="video.mp4" width="470" height="255">
        <embed src="video.swf" width="470" height="255">
        </object>
    </video>
    

    Does that work?

    0 讨论(0)
  • 2020-11-30 20:57

    <video width="400" controls="controls" preload="metadata">
      <source src="https://www.youtube.com/watch?v=Ulp1Kimblg0">
    </video>

    0 讨论(0)
  • 2020-11-30 20:59

    If you want a video first frame as a thumbnail, than you can use

    Add #t=0.1 to your video source, like below
    
    <video width="320" height="240" controls>
      <source src="video.mp4#t=0.1" type="video/mp4">
    </video>
    

    NOTE: make sure about your video type(ex: mp4, ogg, webm etc)

    0 讨论(0)
  • 2020-11-30 21:01

    That seems to be an extra image being shown there.

    You can try using this

    <img src="/images/image_of_video.png" alt="image" />
    /* write your code for the video here */
    

    Now using jQuery play the video and hide the image as

    $('img').click(function () {
      $(this).hide();
      // use the parameters to play the video now..
    })
    
    0 讨论(0)
  • 2020-11-30 21:07

    Display Your Video First Frame as Thumbnail:

    Add preload="metadata" to your video tag and the second of the first frame #t=0.5 to your video source:

    <video width="400" controls="controls" preload="metadata">
      <source src="https://www.w3schools.com/html/mov_bbb.mp4#t=0.5" type="video/mp4">
    </video>

    0 讨论(0)
提交回复
热议问题