首页 > 其他分享 >JS取视频第一帧

JS取视频第一帧

时间:2024-05-30 10:13:22浏览次数:9  
标签:视频 canvas const img 第一 JS width video height

<!DOCTYPE html> <html lang="en">
<head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>Document</title> </head>
<body>     <input type="file" id="file" /> </body>
</html> <script>     function imageScale(width, originWidth, originHeight) {         const scaleRatio = width / originWidth;         const scaleHeight = scaleRatio * originHeight;         return [width, scaleHeight];     }
    function getVideoPoster(file, scaleWidth, quality = 0.5) {         return new Promise((resolve, reject) => {             const reader = new FileReader();             reader.readAsDataURL(file);             reader.onload = (e) => {                 let video = document.createElement("video");                 video.src = e.target.result;                 video.onloadeddata = () => {                     const canvas = document.createElement("canvas");                     const [width, height] = imageScale(                         scaleWidth,                         video.videoWidth,                         video.videoHeight                     );                     canvas.width = width;                     canvas.height = height;                     const ctx = canvas.getContext("2d");                     ctx.fillRect(0, 0, width, height);                     ctx.drawImage(video, 0, 0, width, height);                     ctx.imageSmoothingEnabled = false;                     canvas.toBlob(                         async (blob) => {                                 video.pause();                                 video = null;                                 resolve(blob);                             },                             "image/jpeg",                             0.5                     );                 };                 video.onerror = () => {                     video.pause();                     video = null;                     reject();                 };                 video.muted = false;                 video.play();             };         });     }     file.onchange = function () {         getVideoPoster(this.files[0], 200).then((blob) => {             let url = window.URL.createObjectURL(blob);             const img = document.createElement("img");             img.src = url;             img.width = 200;             document.body.appendChild(img);         });     } </script>

标签:视频,canvas,const,img,第一,JS,width,video,height
From: https://www.cnblogs.com/zyzlb/p/18221817

相关文章

  • SadTalker API调用生成视频
    fromfastapiimportFastAPIimportuvicornimportnest_asynciofromfastapi.middleware.corsimportCORSMiddlewarefromdatetimeimportdatetimeapp=FastAPI()origins=["*"]app.add_middleware(CORSMiddleware,allow_origins=origins,allow_cr......
  • C#中处理JSON数据的方式
    1.将对象序列化为JSON字符串在C#中,可以使用System.Text.Json和Newtonsoft.Json这两个流行的库来将对象序列化为JSON字符串。以下是使用这两个库进行序列化的示例代码:usingSystem;usingSystem.Text.Json;usingNewtonsoft.Json;publicclassPerson{publicstringN......
  • 我创建了一个文件夹/软件包,其中有两个子软件包(第一个=subpkg1,第二个=subpkg2),在subpkg1
    我正在使用VS代码学习Java。我创建了一个文件夹/包(name=pkg)然后,我在该包中创建了两个包(第一个=subpkg1,第二个=subpkg2)但我在subpkg1中创建了一个文件(test1)并从subpkg2中导入文件(name=food)然后编译并运行。我一直收到pkg.subpkg2doesn'texist的错误信息(见......
  • Java 中主要使用 Json 注释
    我从DB收到了JSON格式的数据,但我必须将所有字段对齐到适当的位置,如下所示:{"姓名":"Maveric"、"年龄":"26"、地址"波士顿街22号";}......
  • 视频监控平台AS-V1000 的场景管理,一键查看多画面视频的场景配置、调用、管理(一键浏览
    目录一、场景管理的定义二、场景管理的功能和特点1、功能(1)场景配置(2)实时监控(3)权限管理2、特点三、AS-V1000的场景配置和调用1、场景配置(1)实时视频预览(2)保存四画面场景(3)保存八画面场景(4)保存多个场景2、调用(1)调出四画面(2)调出八画面四、场景管理的应用范围1、......
  • Python-使用OpenCV(二)_第一个示例程序
    1、创建项目2、创建代码importcv2#加载图片image=cv2.imread("C:\\Users\\Administrator\\Pictures\\Screenshots\\20240311220733.png")#显示图片cv2.imshow("Image",image)#等待任意键被敲击cv2.waitKey(0)#关闭所有窗口cv2.destroyAllWindows()3、结......
  • daemon.json一些配置
    /etc/docker/daemon.json{"registry-mirrors":[  "https://registry.docker-cn.com",  "http://hub-mirror.c.163.com",  "https://docker.mirrors.ustc.edu.cn",  "https://si7y70hh.mirror.aliyuncs.com/"],......
  • 28. 找出字符串中第一个匹配项的下标
    28.找出字符串中第一个匹配项的下标ShowSwiftCodefunccomputePMT(_pattern:String)->[Int]{letm=pattern.lengthvarpmt=[Int](repeating:0,count:m)varj=0foriin1..<m{whilej>0&&pattern[p......
  • Excel中怎样将第一行建立好的规则套用到每一行?
    考虑使用条件格式来完成,有两种方式可以尝试:一、一次性创建条件格式1.选中需要设置条件格式的区域,如果是不连续的区域,可以按住Ctrl键,然后用鼠标依次选中需要的数据区域2.点击开始选项卡,条件格式,在下拉选项中,点一下新建规则,3.在弹出的对话框中选中使用公式确定要设置格......
  • 推荐观看的Spring Boot Cloud教学视频!
    想要学习SpringBootCloud框架,但不知道从哪里开始?不用担心!我为您推荐一些高质量的教学视频,帮助您快速入门和深入理解这个流行的技术栈。......