【发布时间】:2016-05-09 13:55:15
【问题描述】:
我正在尝试找到从 http 流播放 YouTube 视频的解决方案。不是嵌入式播放器或通过网站。原因是我需要在应用程序中本地播放一些阻止嵌入的剪辑。 所以获取http流的第一个任务很容易解决,例如使用YouTubeExtractor。例如来自这个 youtube 网址 https://www.youtube.com/watch?v=31crA53Dgu0
YouTubeExtractor 提取此类 url 以下载视频。如您所见,没有具体的 .mp4 或 .mov 文件的路径。还绑定了IP,所以url对你不起作用。
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