处理 Youtube 嵌入限制

Handle Youtube Embed restriction

其实我在开发JARVIS助手 它将有很多模块但我从应该是简单的模块开始。

Youtube One。所以我的全屏或隐藏的主窗口有一个 WebBrowser

    private static string GetYouTubeVideoPlayerHTML(string videoCode)
    {
        var sb = new StringBuilder();

        const string YOUTUBE_URL = @"http://www.youtube.com/embed/";

        sb.Append("<html>");
        sb.Append("    <head>");
        sb.Append("        <meta name=\"viewport\" content=\"width=device-width; height=device-height;\">");
        sb.Append("    </head>");
        sb.Append("    <body marginheight=\"0\" marginwidth=\"0\" leftmargin=\"0\" topmargin=\"0\" style=\"overflow-y: hidden\">");
        sb.Append($"        <iframe width=\"100%\" height=\"100%\" src =\"{YOUTUBE_URL}{videoCode}?autoplay=1&showinfo=0&controls=0\" frameborder = \"0\" allowfullscreen>");
        sb.Append("    </body>");
        sb.Append("</html>");

        return sb.ToString();
    }

这个 return 我使用 WebBrowser.Navigate()

的字符串

效果很好,但如果视频嵌入了像 VEVO 这样的限制,我就看不到了 https://www.youtube.com/embed/wfN4PVaOU5Q 在这里举例 一件事告诉我这是可能的,因为在这个网站 http://codepen.io/Jebik/pen/ZLZQwX 嵌入的作品就像一个魅力...... 所以这可能是我对这个限制不了解的地方。

欢迎任何想法和解决方案。 但我宁愿有一个像 "man you must register your domain and ask the page in https" 这样的合法解决方案,而不是 "you can hack this security by this method"

我找到了一种方法,但我不知道它为什么有效

我使用服务器我必须上传网页

<!DOCTYPE html>
<html>
  <body>
    <div id="player"></div>
    <script>
      var player;
      var body = document.body;
      var html = document.documentElement;
      var height = Math.max( body.scrollHeight, body.offsetHeight, 
                       html.clientHeight, html.scrollHeight, html.offsetHeight );
      var width = Math.max( body.scrollWidth, body.offsetWidth, 
                       html.clientWidth, html.scrollWidth, html.offsetWidth );
      window.onresize = resize;
      var tag = document.createElement('script');
      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
      function onYouTubeIframeAPIReady() 
      {
        player = new YT.Player('player', {
          height: height,
          width: width,
          playerVars: { 'autoplay': 1, 'controls': 0, 'showinfo': 0, 'enablejsapi':1, 'iv_load_policy':3, 'modestbranding':1, 'showinfo':0},
          videoId: '<?php echo $_GET["v"]; ?>',
          events: {
            'onReady': onPlayerReady
          }
        });
      }
      function resize()
      {
        height = Math.max( body.scrollHeight, body.offsetHeight, 
                       html.clientHeight, html.scrollHeight, html.offsetHeight );
        width = Math.max( body.scrollWidth, body.offsetWidth, 
                       html.clientWidth, html.scrollWidth, html.offsetWidth );
        if(player != null)
        {
          player.setSize(width, height);
        }
      }
      function onPlayerReady(event) {
        event.target.playVideo();
      }
      function playVideo() 
      {
        player.playVideo();
      }
      function pauseVideo() 
      {
        player.pauseVideo();
      }
    </script>
  </body>
</html>

网页必须从https访问 我不知道为什么 也许 youtube 使用证书来了解网站的真实位置(检查它的注册位置而不是 ip)

然后我把我的 WebBrowser.NavigateToString 改成了 WebBrowser.Navigate($"https://domain.fr/youtube.php?v={VideoCode}");

通过这种方式,我的 WPF 中有一个全屏的 youtube 播放器 window,我可以阅读大部分的 youtube 视频,比如 VEVO limited one。

这已解决,但如果有人知道为什么我很想知道