js獲取video元素第一幀畫面

唯一遺憾的地方是不能在視頻剛加載的時(shí)候就截出畫面,我個(gè)人的做法是設(shè)置了500毫秒的回調(diào)。
google找了好多教程,他們也沒辦法解決這個(gè)問題。

<!DOCTYPE html>
<html>

<body>
    <video id="video" oncanplay="canplay(event)"></video>

    <input id="input" type="file" accept="video/*" onchange="change(event)" />

    <img id="img" alt="第一幀截圖">
</body>

<script>
    var video = document.getElementById('video')
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');


    function change(e) {
        video.src = window.URL.createObjectURL(e.target.files[0]);
        video.load()
    }


    function canplay(e) {

        setTimeout(() => {

            canvas.width = e.target.videoWidth
            canvas.height = e.target.videoHeight

            ctx.drawImage(e.target, 0, 0);
            img.src = canvas.toDataURL();

        }, 500); //讓它等500毫秒再執(zhí)行以上操作

    }

</script>


</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容