HTML/CSS

HTML5截取自动视频封面

在日常项目中,html5 的 video 标签还是比较常用到的,开发过程中,我们都会使用到,通过监听 video 标签的播放、暂停、停止等等来使用;但我们是否也会遇到过,有些浏览器在显示这标签,兼容不太友好,video 标签的封面是一层黑色的,而不是视频的封面图。那么,此时,我们就得截取每个视频的封面图来,因为 video 标签有个属性 poster 是用来储存封面图地址的,但需注意先用本地视频,非本地视频会有跨域截取问题,这个得跟后端开发协作处理。

代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en" >
  3. <head>
  4.     <meta charset="UTF-8" >
  5.     <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >
  6.     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
  7.     <meta name="format-detection" content="telephone=no" >
  8.     <meta name="screen-orientation" content="portrait" >
  9.     <meta name="x5-orientation" content="portrait" >
  10.     <meta name="full-screen" content="yes" >
  11.     <meta name="x5-fullscreen" content="true" >
  12.     <meta name="browsermode" content="application" >
  13.     <meta name="x5-page-mode" content="app" >
  14.     <title>视频video应用</title>
  15.     <style>
  16.         .video-con{ width: 300px; height: 150px; position: relative; margin-bottom: 20px;}
  17.         .video-con .video{ width: 100%; height: 100%;}
  18.         .video-con .btn{ width: 64px; height: 64px; background: transparent url("play-btn.png") no-repeat;
  19.             position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); border: none;
  20.         }
  21.         .video-con .btn.pause{ background-image: url("pause-btn.png");}
  22.     </style>
  23. </head>
  24.  
  25. <body>
  26.  
  27. <h1>视频video应用:截取封面图;监听播放状态</h1>
  28.  
  29. <div class="video-con">
  30.     <video class="video"  poster="">
  31.         <source src="1.mp4" >
  32.     </video>
  33.     <button type="button" class="btn js-play-btn"></button>
  34. </div>
  35.  
  36. <div class="video-con">
  37.     <video class="video" poster="">
  38.         <source src="2.mp4" >
  39.     </video>
  40.     <button type="button" class="btn js-play-btn"></button>
  41. </div>
  42.  
  43.  
  44. <!--html5的video-->
  45. <script type="text/javascript">
  46.     // 视频------视频截图 ~~ 视频播放状态 ~~
  47.     const setMedia = function(video, scale = 0.8) {
  48.         // 设置poster属性:(非本地视频资源会有跨域截图问题)
  49.         video.addEventListener('loadeddata', function(e) {
  50.             // 拿到图片
  51.             let canvas = document.createElement('canvas');
  52.             canvas.width = video.videoWidth * scale;
  53.             canvas.height = video.videoHeight * scale;
  54.             canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
  55.             let src = canvas.toDataURL('image/png');
  56.             // 显示在dom,测试用
  57.             (function(flag = true) {
  58.                 if (!flag) {return;}
  59.                 let img = document.createElement('img');
  60.                 img.src = src;
  61.                 document.body.appendChild(img);
  62.             })(0);
  63.             // 设置属性
  64.             video.setAttribute('poster', src);
  65.         });
  66. // -------------------------------------------------------------------------------------
  67.         //检测视频播放状态:
  68.         //播放按钮
  69.         let playBtn =  video.parentNode.childNodes[2].nextSibling;
  70.         //设置状态
  71.         function vidplaySate(e) {
  72.             if (video.paused) {
  73.                 video.play();
  74.                 playBtn.classList.add('pause');
  75.             } else {
  76.                 video.pause();
  77.                 playBtn.classList.remove('pause');
  78.             }
  79.         }
  80.         //点击监听
  81.         video.addEventListener('click', vidplaySate, false);
  82.         playBtn.addEventListener('click', vidplaySate, false);
  83.         //结束监听
  84.         video.addEventListener('ended',function () {
  85.             playBtn.classList.remove('pause');
  86.         });
  87.     };
  88.     //视频:
  89.     let videos = document.querySelectorAll('video');
  90.     videos.forEach((video) => {
  91.         setMedia(video);
  92.     });
  93. </script>
也想出现在这里?联系我们
创客主机
收藏
(2)
PetitQ

PetitQ

文章:24509 画廊:208 视频:17 商品:108

本站承接 WordPress/DedeCMS/PbootCMS 等系统仿站、开发、定制等服务!

最新视频

热评视频

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!