How to capture image of Youtube Video and display on web site

前端 未结 1 1064
轮回少年
轮回少年 2020-12-11 13:53

I am interested in creating a website that displays a few \"screen captures\" on the screen. These \"screen captures\" can be any image file type (ex: jpeg, png, etc.) I wou

相关标签:
1条回答
  • 2020-12-11 14:40

    You can use YouTube IFrame API (see https://developers.google.com/youtube/iframe_api_reference).

    Here is an example for playing the video provided in your question (gbcmYbMB9mo):

    <body onload="LoadYouTubeIframeAPI()">
        <script type="text/javascript">
            var player = null;
            var playerParams =
            {
                playerVars:
                {
                    "enablejsapi":1,
                    "origin":document.domain,
                    "rel":0
                },
                events:
                {
                    "onReady":onPlayerReady,
                    "onError":onPlayerError,
                    "onStateChange":onPlayerStateChange
                }
            };
            function LoadYouTubeIframeAPI()
            {
                var scriptElement = document.createElement("script");
                scriptElement.src = "http://www.youtube.com/iframe_api";
                var firstScriptElement = document.getElementsByTagName("script")[0];
                firstScriptElement.parentNode.insertBefore(scriptElement,firstScriptElement);
            }
            function onYouTubeIframeAPIReady()
            {
                player = new YT.Player("player",playerParams);
            }
            function onPlayerReady(event)
            {
                player.loadVideoById("gbcmYbMB9mo");
            }
            function onPlayerError(event)
            {
                ...
            }
            function onPlayerStateChange(event)
            {
                if (event.data == YT.PlayerState.ENDED)
                {
                    ...
                }
            }
        </script>
    </body>
    

    You might need to "play a little" with this code in order to prevent the video from starting automatically...

    0 讨论(0)
提交回复
热议问题