【问题标题】:Handle Youtube Embed restriction处理 Youtube 嵌入限制
【发布时间】:2017-07-05 07:10:33
【问题描述】:

我实际上正在开发一个 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();
    }

这将返回我与 WebBrowser.Navigate() 一起使用的字符串

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

欢迎任何想法和解决方案。 但我宁愿有一个合法的解决方案,比如“你必须注册你的域并在 https 中询问页面”,而不是“你可以通过这种方法破解这种安全性”

【问题讨论】:

    标签: c# wpf youtube webbrowser-control embed


    【解决方案1】:

    我发现了一种我不知道为什么会起作用的方法

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

    <!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 播放器,我可以阅读大部分 youtube 视频,例如 VEVO 有限的一个。

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

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2012-02-19
      • 2011-10-10
      • 2014-08-06
      • 2013-12-26
      • 2011-09-06
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多