从视频文件提取画面帧

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img{
            width: 50px;
            height: 50px;
        }
    </style>
</head>
<body>
    <input type="file" name="" id="">
    <script>
        function captureFrame(videoFile,time=0){
    return new Promise(resolve=>{
        const vdo=document.createElement('video')
        vdo.currentTime=time;
        vdo.muted=true;
        vdo.autoplay=true;
        vdo.src=URL.createObjectURL(videoFile)
        // console.log(vdo.src);
    vdo.oncanplay=()=>{
    const cvs=document.createElement('canvas')
    cvs.width=vdo.videoWidth;
    cvs.height=vdo.videoHeight;
    const ctx=cvs.getContext('2d')
    ctx.drawImage(vdo,0,0,cvs.width,cvs.height)
    cvs.toBlob((blob)=>{
       const url=URL.createObjectURL(blob)
       resolve({
        blob,url
       })
    })

    }
        // return {
        //     url:,
        //     blob:''
        // }
    })

}
const inp=document.querySelector('input[type=file]')
inp.onchange=async (e)=>{
    const file=e.target.files[0]
   for (let index = 0; index < 10; index++) {
     captureFrame(file,index*5).then(result=>{
        previewImage(result.url);
    })


   }
}

function previewImage(url){
          const img=document.createElement('img')
        img.src=url
        document.body.appendChild(img)
    // const reader=new FileReader()
    // reader.readAsDataURL(file)
    // reader.onloadend=()=>{
    //     const img=document.createElement('img')
    //     img.src=reader.result
    //     document.body.appendChild(img)
    // }

}
    </script>

</body>
</html>
posted @ 2024-05-01 21:34  7c89  阅读(6)  评论(0编辑  收藏  举报