[C#]获得youku视频截图和地址

baacloud免费翻墙vpn注册使用

实现方式如下

youku的站外引用提供了如下地址

image

http://player.youku.com/player.php/sid/XNTg0NDExMzI=/v.swf

其中XNTg0NDExMzI=这个部分可以看做是视频的ID

而每次视频播放时v.swf通过以下网址获取视频信息

http://v.youku.com/player/getPlayList/VideoIDS/视频的ID/version/5/source/out?onData=%5Btype%20Function%5D&n=3

这个个网址返回的是一段json,类似

{"data":[{"tt":"0","ct":"a","cs":"2143","logo":"http:\/\/vimg12.youku.com\/1100641F46493E8D0B3EFF0048E76B57CA9FBD-CEC7-7777-3402-EC9732C9950B","seed":5484,"tags":["\u7f8e\u56fd","\u98df\u54c1"],"categories":"91","streamsizes":{"flv":"3111068"},"streamfileids":{"flv":"19*67*19*19*2*42*19*16*19*19*42*61*47*44*32*46*19*5*42*42*46*61*19*19*42*32*44*50*2*5*42*37*61*30*16*16*37*23*26*37*61*30*37*26*23*44*16*16*26*5*16*47*46*26*30*50*19*16*44*44*12*37*46*47*12*46*"},"videoid":"14610283","segs":{"flv":[{"no":0,"size":"3111068","seconds":"90.00"}]},"fileid":"19*67*19*19*2*42*19*16*19*19*42*61*47*44*32*46*19*5*42*42*46*61*19*19*42*32*44*50*2*5*42*37*61*30*16*16*37*23*26*37*61*30*37*26*23*44*16*16*26*5*16*47*46*26*30*50*19*16*44*44*12*37*46*47*12*46*","username":"dongdong19850912","userid":"4777835","title":"\u591a\u79cd\u7f8e\u56fd\u8f93\u534e\u98df\u54c1\u88ab\u68c0\u51fa\u8d28\u91cf\u95ee\u9898","key1":"bd7ed04d","key2":"3fd76e2fd48efb01","seconds":"90.00","streamtypes":["flv"]}],"user":{"id":0}}

其中的Title自然就是标题,logo就是视频的截图

 

这里可以用json.net来反序列化获取,但我这里是使用的正则来提取的

		static public string[] GetMediaPic(string url) {
if (url.ToLower().StartsWith("http://player.youku.com/player.php/sid/")) {//判断是否为youku视频
return getPicYouKu(url);
} else {
return new string[0];
}
}
static string[] getPicYouKu(string url) {
string id = url.Replace(@"http://player.youku.com/player.php/sid/","").Replace(@"/v.swf","");
string newurl = string.Format(
@"http://v.youku.com/player/getPlayList/VideoIDS/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3"
, id);
var hp = new HttpProc(newurl);//这个类见下方网址
var html = hp.Proc().Replace(@"\/", "/");//通过get方式获取信息
var re = new Regex("(http://[^\"]+)[\\w\\W]+\"title\":\"([^\"]+)\"");//与此同时提取截图和标题
var m = re.Match(html);
if (m.Success) {
var ret = new string[2];
ret[0] = m.Groups[1].Value;//0是url
ret[1] = UnicodeToChinese(m.Groups[2].Value);//1是title,要从unicode转为汉字
return ret;
} else
return new string[0];//如果没有就返回空数组     
}
                /// <summary>
/// unicode转汉字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public string UnicodeToChinese(string str) {
return ConvertTo(str, "unicode");
}
static string ConvertTo(string str, string encode) {
StringBuilder tmpStr = new StringBuilder();
for (int i = 0; i < str.Length; i++) {
if (str[i] == '\\' && str[i + 1] == 'u') {
string s1 = str.Substring(i + 2, 2);
string s2 = str.Substring(i + 4, 2);
int t1 = Convert.ToInt32(s1, 16);
int t2 = Convert.ToInt32(s2, 16);
byte[] array = new byte[2];
array[0] = (byte)t2;
array[1] = (byte)t1;
string s = System.Text.Encoding.GetEncoding(encode).GetString(array);
tmpStr.Append(s);
i = i + 5;
} else { tmpStr.Append(str[i]); }
}
return tmpStr.ToString();
}

HttpProc可参见:http://www.51aspx.com/CV/CHSNS122,UserWebService,ChAlumna,Reqres.cs.html

 

如无外最后返回数组即为[0]存标题,[1]存截图地址

 

var x=GetMediaPic("http://player.youku.com/player.php/sid/XNTg0NDExMzI=/v.swf");
if(x.Length==0){
        //不是youku或无法获取
}else{
Title=x[0];
Url=x[1];
}
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏