对于其他仍然好奇的人来说,要克服的问题是获得指向视频的直接链接,这确实需要一些小技巧,但它非常可靠且易于操作。首先,您需要视频 id,这样您就可以获得可以使用 youtube api 的 youtube url。然后做这样的事情。我几乎将用户脚本转换为 Silverlight。
WebClient client = new WebClient();
string url = "http://www.youtube.com/watch?v=HLQqOpILDcI";
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ClientDownloadStringCompleted);
client.DownloadStringAsync(new Uri(url, UriKind.Absolute));
下一点看起来很糟糕。
private void ClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
rx = new Regex("(?<=url_encoded_fmt_stream_map=)([^(\\\\)]*)(?=\\\\)", RegexOptions.IgnoreCase);
match = rx.Matches(flashvars);
string video_format = match[0].ToString();
string sep1="%2C";
string sep2="%26";
string sep3="%3D";
string link = "";
string[] videoFormatsGroup = Regex.Split(video_format, sep1);
for (var i=0;i<videoFormatsGroup.Length;i++){
string[] videoFormatsElem = Regex.Split(videoFormatsGroup[i], sep2);
if (videoFormatsElem.Length<5) continue;
string[] partialResult1 = Regex.Split(videoFormatsElem[0], sep3);
if (partialResult1.Length<2) continue;
string url = partialResult1[1];
url = HttpUtility.UrlDecode(HttpUtility.UrlDecode(url));
string[] partialResult2 = Regex.Split(videoFormatsElem[4], sep3);
if (partialResult2.Length<2) continue;
int itag = Convert.ToInt32(partialResult2[1]);
if (itag == 18){
link = url;
}
}
}
最后一位itag==18根据这个选择质量
{'5':'FLV 240p','18':'MP4 360p','22':'MP4 720p (HD)','34':'FLV 360p','35':'FLV 480p','37':'MP4 1080p (HD)','38':'MP4 4K (HD)','43':'WebM 360p','44':'WebM 480p','45':'WebM 720p (HD)','46':'WebM 1080p (HD)'};
现在你可以对链接做任何你想做的事情,比如用 mediaplayerlauncher 或 mediaelement 打开它。就个人而言,我很想将它下载到独立存储并同时播放,但目前这似乎说起来容易做起来难。感谢您抽出宝贵的时间,很抱歉发了这么长的帖子。