首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >canvas视频截帧

canvas视频截帧

作者头像
阿超
发布2022-09-28 12:36:57
发布2022-09-28 12:36:57
8290
举报
文章被收录于专栏:快乐阿超快乐阿超

礼貌是儿童和青年都应该特别小心地养成习惯的第一件大事——约翰·洛克

先放代码:

代码语言:javascript
复制
<script>
       /**
        * 
        * @param src string 视频url
        * @param currentTime double 视频截取位置,单位秒
        * @return 截取图片的 base64
        */
       function getImgFromVideoUrl(src, currentTime) {
           if (!src) return
           if (!currentTime || currentTime < 0) {
               // 如果传入截取位置小于0, 给个默认值
               currentTime = 0.001
           }
           return new Promise(resolve => {
               const body = document.querySelector("body")
               // 获取/创建video节点
               const video = document.querySelector("#imgExtractorVideo") || document.createElement("video")
               if (!video.id) {
                   // id,隐藏样式
                   Object.assign(video, {
                       id: 'imgExtractorVideo',
                       style: "display: none"
                   })
                   // 允许跨域
                   video.setAttribute('crossorigin', 'anonymous')
                   // 添加到页面上,下次就不用创建,直接用
                   body.append(video)
               }
               // 此处给video赋值,canplay需要重新赋值,否则load后不会重复调用
               Object.assign(video, { src, currentTime, oncanplay })
               // 获取/创建canvas节点
               const canvas = document.querySelector("#imgExtractorCanvas") || document.createElement("canvas")
               if (!canvas.id) {
                   // id,隐藏样式
                   Object.assign(canvas, {
                       id: 'imgExtractorCanvas',
                       style: "display: none"
                   })
                   // 添加到页面上,下次就不用创建,直接用
                   body.append(canvas)
               }
               // 获取canvas context
               const context = canvas.getContext("2d")
               // canvas渲染视频节点,转换为base64
               async function oncanplay() {
                   // 为canvas设置宽高为视频的宽高
                   Object.assign(canvas, { height: video.videoHeight, width: video.videoWidth })
                   // 渲染视频
                   context.drawImage(video, 0, 0)
                   // 获取视频时长
                   const duration = isNaN(video.duration) ? 0 : video.duration
                   if (currentTime > duration) {
                       // 如果当前传入截取位置大于视频时长,取视频时长 -0.1 然后重新截取
                       currentTime = duration - 0.1;
                       resolve(await getImgFromVideoUrl(src, currentTime))
                   } else {
                       // 这里的 toDataURL 第二个参数为图片质量,可接受范围 0~1 ,超出后为默认值 0.92
                       resolve(canvas.toDataURL("image/png", 1.0))
                   }
               }
           })
       }



       window.onload = async () => {
           // 视频长度 15s 左右

           // 调用
           let imgBase64 = await getImgFromVideoUrl("https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/rabbit.mp4")

           // 使用
           console.log({ imgBase64 })
           let img = document.createElement("img")
           img.src = imgBase64
           document.querySelector("body").append(img)

           // 调用
           imgBase64 = await getImgFromVideoUrl("https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/rabbit.mp4", 3)
           // 重复使用
           console.log({ imgBase64 })
           img = document.createElement("img")
           img.src = imgBase64
           document.querySelector("body").append(img)

           // 调用
           imgBase64 = await getImgFromVideoUrl("https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/rabbit.mp4", 4)
           // 重复使用
           console.log({ imgBase64 })
           img = document.createElement("img")
           img.src = imgBase64
           document.querySelector("body").append(img)

           // 调用
           imgBase64 = await getImgFromVideoUrl("https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/rabbit.mp4", 20)
           // 重复使用
           console.log({ imgBase64 })
           img = document.createElement("img")
           img.src = imgBase64
           document.querySelector("body").append(img)

           // Promise回调地狱
           getImgFromVideoUrl("https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/rabbit.mp4", 3)
               .then(base64 => {
                   // 使用
                   console.log({ base64 })
                   let img = document.createElement("img")
                   img.src = base64
                   document.querySelector("body").append(img)

                   // Promise回调地狱
                   getImgFromVideoUrl("https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/rabbit.mp4", 4)
                       .then(base64 => {
                           // 使用
                           console.log({ base64 })
                           let img = document.createElement("img")
                           img.src = base64
                           document.querySelector("body").append(img)

                           // Promise回调地狱
                           getImgFromVideoUrl("https://waibi.oss-cn-chengdu.aliyuncs.com/picGo/rabbit.mp4", 5)
                               .then(base64 => {
                                   // 使用
                                   console.log({ base64 })
                                   let img = document.createElement("img")
                                   img.src = base64
                                   document.querySelector("body").append(img)
                               })
                       })
               })

       }



   </script>

效果:

接下来是博客和MDN

Promise博客:https://cloud.tencent.com/developer/article/2075561

oss视频截封面博客:https://vampireachao.gitee.io/2022/01/10/oss视频截封面/

MDN

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档