【问题标题】:Stream YouTube video from extracted url从提取的 URL 流式传输 YouTube 视频
【发布时间】:2016-05-09 13:55:15
【问题描述】:

我正在尝试找到从 http 流播放 YouTube 视频的解决方案。不是嵌入式播放器或通过网站。原因是我需要在应用程序中本地播放一些阻止嵌入的剪辑。 所以获取http流的第一个任务很容易解决,例如使用YouTubeExtractor。例如来自这个 youtube 网址 https://www.youtube.com/watch?v=31crA53Dgu0

YouTubeExtractor 提取此类 url 以下载视频。如您所见,没有具体的 .mp4 或 .mov 文件的路径。还绑定了IP,所以url对你不起作用。

https://r3---sn-puu8-3c2e.googlevideo.com:443/videoplayback?sparams=dur,gcr,id,initcwndbps,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source,upn,expire&source=youtube&initcwndbps=3147500&pl=19&upn=oYYkSNBwoc8&fexp=9412913,9416126,9416891,9422596,9428398,9429016,9431012,9431364,9431901,9432206,9432825,9433096,9433424,9433623,9433946,9434085,9435504,9435703,9435937&id=o-AGanAaajMgOmp4VrXIwo9jewJnqlsvZecDxCcRpSN3lS&expire=1462821352&gcr=ua&ip=12.34.56.149&lmt=1458705532562546&ms=au&mt=1462799507&mv=m&dur=217.849&sver=3&itag=22&key=yt6&mn=sn-puu8-3c2e&mm=31&ipbits=0&mime=video/mp4&ratebypass=yes&requiressl=yes&signature=A142619EBA90D00CC46FF05B9CF1E3469B0EF196.072B532670A4D833582F94CF9C46F1F7D298F230&fallback_host=tc.v1.cache5.googlevideo.com

private string ExtractVideoUrl(string youtubeUrl)
    {
        IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(youtubeUrl, false);
        var video = videoInfos.First();
        if (video.RequiresDecryption)
        {
            DownloadUrlResolver.DecryptDownloadUrl(video);
        }

        return video.DownloadUrl;       // videoInfos.First().DownloadUrl;
    }

然后下载视频这个库使用下面的代码。有趣的时刻是读取流和写入 youtube 视频的内​​容。

var request = (HttpWebRequest)WebRequest.Create(this.Video.DownloadUrl);
        if (this.BytesToDownload.HasValue)
        {
            request.AddRange(0, this.BytesToDownload.Value - 1);
        }

        // the following code is alternative, you may implement the function after your needs
        using (WebResponse response = request.GetResponse())
        {
            using (Stream source = response.GetResponseStream())
            {
            **// HOW to Play source stream???**
                using (FileStream target = File.Open(this.SavePath, FileMode.Create, FileAccess.Write))
                {
                    var buffer = new byte[1024];
                    bool cancel = false;
                    int bytes;
                    int copiedBytes = 0;

                    while (!cancel && (bytes = source.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        target.Write(buffer, 0, bytes);
                        .... 

所以一般的问题是如何从打开的流中播放视频? 例如 Chrome、Firefox 和 KMPlayer 很容易做到这一点。浏览器生成一个带有 video 标签的简单页面,因此通过 JS 管理播放器将是微不足道的。但是...内部WebBrowser控件不能,它建议下载文件。我尝试了CEFSharp(Chrome 嵌入式框架),但没有运气。 也许有人知道可以流式传输视频的优秀视频播放器 WPF 库吗? 我也尝试了 VLC.wpf 和 Shockwave Flash,但仍然对这个提取的 url 不满意。大量搜索,但目前结果不足。

【问题讨论】:

    标签: wpf video youtube streaming video-streaming


    【解决方案1】:

    经过 2 天的研究,我找到了如何直接从 WPF 应用程序播放 youtube 视频 url 的解决方案。 之前的一些笔记:

    • WPF 和 WinForms 的 WebBrowser 组件不支持 HTML5。所以没有办法在那里使用
    • Chrome 嵌入式框架 (CEF) 及其 CEFSharp 包装器默认不支持
    • WPF MediaElement 组件does not support 从流中播放视频。但是也有解决方法,它们不适合播放 youtube url。
    • 找不到可以播放 HttpWebRequest 返回的 Stream 对象中的视频的 .NET 媒体播放器

    所以解决方案是使用GeckoFx 网络浏览器,它也可以通过nuget 获得。由于它是基于 WinForms 的组件,因此需要使用 WinFormsHosting 在 WPF 中使用它。最终解决方案是:

    <xmlns:gecko="clr-namespace:Gecko;assembly=Geckofx-Winforms">
    ....
    <WindowsFormsHost Width="400" Height="300" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" >
                    <gecko:GeckoWebBrowser x:Name="_geckoBrowser"/>
                </WindowsFormsHost>
    

    以及使用简单的代码:

    public void PlayVideo(string videoId)
        {                        
            var url = string.Format("https://www.youtube.com/watch?v={0}", videoId);
            var videoUrl = ExtractVideoUrl(url);
    
            _geckoBrowser.Navigate(videoUrl);
        }
    

    GeckoFx 依赖于 XulRunner 运行时 sdk。虽然它是用 nuget 包安装的。此解决方案的一个缺点是所有应用依赖项的大小增加了超过 70mb。

    【讨论】:

      猜你喜欢
      • 2010-11-03
      • 2019-10-16
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2013-04-15
      • 2014-02-16
      相关资源
      最近更新 更多