欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS?requestVideoFrameCallback()?簡單案例

 更新時(shí)間:2022年07月29日 08:38:11   作者:??天行無忌???  
這篇文章主要介紹了JS?requestVideoFrameCallback()簡單案例,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下

前言:

在文章《瀏覽器視頻幀操作方法 requestVideoFrameCallback()》中介紹了基本的使用情況,本文來體驗(yàn)一下其使用過程,后續(xù)將增加一些酷炫的實(shí)例。

點(diǎn)擊開始播放視頻、暫停視頻以讀取元數(shù)據(jù)。在畫布上繪制視頻幀與實(shí)際視頻幀速率同步的畫面。

主要代碼如下:

<template>
    <div id="app">
        <div class="player-container">
            <div class="title">requestVideoFrameCallback() 簡單實(shí)例</div>
            <div class="item-container left">
                <div class="item-container-in">
                    <video controls playsinline muted autoplay></video>
                    <div class="item-header">Video</div>
                </div>
            </div>
            <div class="item-container right">
                <div class="item-container-in">
                    <canvas></canvas>
                    <div class="item-header">Canvas</div>
                </div>
            </div>
            <div class="fps-info" v-if="fpsInfo">
                <strong>FPS:</strong>
                <i>{{ fpsInfo }}</i>
            </div>
            <div class="metadata-info" v-if="metadata">
                <textarea readonly v-model="metadata"></textarea>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "App",
    data() {
        return {
            fpsInfo: null,
            metadata: null,
        };
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            const video = document.querySelector("video");
            const canvas = document.querySelector("canvas");
            const ctx = canvas.getContext("2d");

            video.addEventListener("play", () => {
                if (
                    !("requestVideoFrameCallback" in HTMLVideoElement.prototype)
                ) {
                    return alert(
                        "你的瀏覽器不支持接口 `Video.requestVideoFrameCallback()`"
                    );
                }
            });

            let width = canvas.width;
            let height = canvas.height;

            let paintCount = 0;
            let startTime = 0.0;

            const updateCanvas = (now, metadata) => {
                if (startTime === 0.0) {
                    startTime = now;
                }

                ctx.drawImage(video, 0, 0, width, height);

                const elapsed = (now - startTime) / 1000.0;
                const fps = (++paintCount / elapsed).toFixed(3);
                this.fpsInfo = !isFinite(fps) ? 0 : fps;
                this.metadata = JSON.stringify(metadata, null, 2);

                video.requestVideoFrameCallback(updateCanvas);
            };

            video.src = "https://vjs.zencdn.net/v/oceans.mp4";
            video.requestVideoFrameCallback(updateCanvas);
        },
    },
};
</script>

<style>
* {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}
#app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    background: #000;
}
.title {
    position: absolute;
    left: 0;
    top: 30;
    height: 60px;
    line-height: 60px;
    font-size: 24px;
    color: #fff;
    text-align: center;
    width: auto;
    right: 0px;
    background-color: #000;
    z-index: 100;
    border-bottom: 1px rgba(255, 255, 255, 0.6) solid;
}
.player-container {
    width: 100%;
    height: 100vh;
    overflow: hidden;
    position: relative;
}
.item-container {
    position: absolute;
    width: 50%;
    height: 100%;
    padding: 50px;
    top: 0px;
    z-index: 1;
}
.item-container.left {
    left: -5px;
    border-right: 1px rgba(255, 255, 255, 0.6) solid;
}
.item-container-in {
    width: 100%;
    height: 100%;
    position: relative;
}
.item-header {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 100px;
    z-index: 9;
    color: rgba(255, 255, 255, 0.8);
    font-weight: 700;
}
.item-container.right {
    right: -5px;
    border-left: 1px rgba(255, 255, 255, 0.6) solid;
}
.item-container canvas,
.item-container video {
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -180px 0px 0px -320px;
    width: 640px;
    height: 360px;
}
.item-container::before {
    position: absolute;
    content: "";
    width: 1px;
    height: 100%;
    overflow: hidden;
    background-color: rgba(#fff, 0.6);
}
.fps-info {
    position: absolute;
    width: 200px;
    height: 50px;
    line-height: 20px;
    font-family: Arial, Helvetica, sans-serif;
    left: 50px;
    bottom: 50px;
    z-index: 10;
    padding: 15px;
    background-color: rgba(255, 255, 255, 0.5);
    text-align: left;
    color: #000;
}
.fps-info i {
    font-style: normal;
}
.metadata-info {
    position: absolute;
    width: 600px;
    height: 180px;
    line-height: 20px;
    font-family: Arial, Helvetica, sans-serif;
    right: 50px;
    bottom: 50px;
    z-index: 10;
    padding: 15px;
    background-color: rgba(255, 255, 255, 0.5);
    text-align: left;
    color: #000;
}
.metadata-info textarea {
    display: block;
    width: 100%;
    height: 100%;
    background-color: transparent;
    border: none;
}
</style>

執(zhí)行代碼點(diǎn)擊播放視頻,右邊的畫面保持和視頻畫面同步。

利用方法 requestVideoFrameCallback() 可以在WEB中實(shí)現(xiàn)使用攝像頭同步動(dòng)作到3D任務(wù)模型中,需要用到AI來識別視頻中的每一幀的骨骼工作,然后同步到虛擬3D模型中。這在虛擬直播中算是比較常見的場景。

到此這篇關(guān)于JS requestVideoFrameCallback() 簡單案例的文章就介紹到這了,更多相關(guān)JS requestVideoFrameCallback內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論