第一次写博客,心里有点小激动!前一段时间做一个ASP.NET网站时涉及到发布视频的功能。这个功能要求给一个连接地址,然后生成视频缩略图,并提供播放功能!
我是从百度找到谷歌都没有找到好的解决方案,包括CSDN上面提供一些案例代码,下载下来全是忽悠人,赚取我的积分啊!坑爹呀!你妹呀!好了,现在开始介绍我自己在优酷和土豆地址中找到的一些规律,做出的一点点东西!可能不是很全!
发布视频的时候可以发布视频的网页地址(.html),也可以发布视频(.swf)地址
优酷视频:
第一种情况:
http://v.youku.com/v_show/id_XMzY3NzExNjgw.html
http://player.youku.com/player.php/sid/XMzY3NzExNjgw/v.swf
第二种情况:
http://v.youku.com/v_playlist/f17213424o1p0.html
http://player.youku.com/player.php/Type/Folder/Fid/17213424/Ob/1/Pt/0/sid/XMzE2MzI5MDMy/v.swf
重要的一个地址:http://v.youku.com/player/getPlayList/VideoIDS/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3 此地址是获取视频缩略图的!我们可以模仿http请求该地址获取存有缩略图的源文件!其中{0}是参数,该参数是从.swf视频地址中获取的。比如以上第一种情况中XMzY3NzExNjgw或者是第二中情况中XMzE2MzI5MDMy都是视频的唯一标识值。以下分析图
给出优酷网页地址:
//根据输入的网页地址截取出唯一id,根据id拼出视频地址
string hyouKuVideoUrlById = "http://player.youku.com/player.php/sid/{0}/v.swf";
//根据视频地址提供的唯一id,请求出缩略图所在地址 string syouKuPicUrlById = "http://v.youku.com/player/getPlayList/VideoIDS/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3";
string htmlRegxStr = "id=\"link[0-9]+\"[ ]+value=\"http://([\\w]+[.])+[/.\\w_]+";
public override string[] GetVideoInfo(string url)
{
Result[0] = url;
string videoName = GetVideoIdFromUrl(url);
#region //http://v.youku.com/v_show/id_XMjkwMzAwMzY4.html
if (videoName.ToLower().StartsWith("id_"))
{
videoName = videoName.Trim().Remove(0, 3);
Result[1] = CombineStr(hyouKuVideoUrlById, videoName);
Result[2] = GetSomething(
GetResultFromUrl(CombineStr(syouKuPicUrlById, videoName)).Replace(@"\/", "/"),
imgLinkRegxStr
);//"(http://[^\"]+)[\\w\\W]+\"title\":\"([^\"]+)\""
}
#endregion
#region //http://v.youku.com/v_playlist/f17213424o1p0.html
else if (videoName.ToLower().StartsWith("f"))
{
string info = GetResultFromUrl(url);
MatchCollection cc = GetSomethingCollection(info, htmlRegxStr);
for (int i = 0; i < cc.Count; i++)
{
Result[1] = GetSomething(cc[i].Value, hypeLinkRegxStr);
if (Result[1].ToLower().EndsWith(".swf"))
{
break;
}
}
if (Result[1] != "")
{
string getIed = GetVideoId(Result[1], '/', 1);
if (getIed != "")
{
Result[2] = GetSomething(GetResultFromUrl(CombineStr(syouKuPicUrlById, getIed)).Replace(@"\/", "/"), imgLinkRegxStr);
}
}
}
#endregion
#region else
else
{
//扩展其他情况:
}
#endregion
return Result;
}
给出优酷视频地址:
//根据视频地址提供的唯一id,请求出缩略图所在地址
string syouKuPicUrlById = "http://v.youku.com/player/getPlayList/VideoIDS/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3";
public override string[] GetVideoInfo(string url)
{
Result[0] = Result[1] = url;
//http://player.youku.com/player.php/sid/XMzY3NzExNjgw/v.swf
//http://player.youku.com/player.php/Type/Folder/Fid/17213424/Ob/1/Pt/0/sid/XMzE2MzI5MDMy/v.swf
string videoId = GetVideoId(url,'/',1);
Result[2] = GetSomething(GetResultFromUrl(CombineStr(syouKuPicUrlById, videoId)).Replace(@"\/", "/"), imgLinkRegxStr);
return Result;
}
土豆视频
给出土豆网页地址:
//一、输入视频网页(html页面) //1.带.html(规则的网页) static string htuDouThumUrl = "http://www.tudou.com/playlist/service/getPlaylists.html?lids={0}";//---根据lid查询缩略图地址 static string htuDouVideoUrlId = "http://v2.tudou.com/v?vn=02&st=1%2C2&it={0}";//-------根据lid查询视频地址需要的id static string htuDouVideoUrlById = "http://www.tudou.com/l/{0}";//-------根据查出来的视频id拼出视频地址 //2.不带.html(不规则的网页) static string htuDouContainUrl = "http://www.tudou.com/programs/view/";//-------------不规则的网页地址里面包含此内容 static string htuDouVideoUrlByKey = "http://www.tudou.com/v/{0}";//----------根据网页地址截取其中的视频唯一值key,获得视频地址 public override string[] GetVideoInfo(string url) { Result[0] = url;//网页地址 string lid = ""; //1.规则的网页地址:即带html后缀 if (url.ToLower().Contains(".html")) { string fileName = string.Empty; if (url.IndexOf('?') > 0) { fileName = System.IO.Path.GetFileName(url.Remove(url.IndexOf('?'))); } else { fileName = System.IO.Path.GetFileName(url); } fileName = fileName.Replace(".html", string.Empty); int iindex = fileName.ToLower().IndexOf("i"); if (iindex < 0) iindex = fileName.Length - 1; int lindex = fileName.ToLower().IndexOf("l"); if (lindex >= 0) { lid = fileName.Substring(lindex + 1, iindex - lindex - 1); } else { lid = ""; } //获取视频地址 string vedioSl = GetSomething(GetResultFromUrl(CombineStr(htuDouVideoUrlId, lid)), "cd=\"[\\S]+\""); if (vedioSl != "") { Result[1] = CombineStr(htuDouVideoUrlById, vedioSl.Remove(0, 4).Replace("\"", "")); } else { Result[1] = url; } //获取缩略图 string newurl = CombineStr(htuDouThumUrl, lid); string picUrl = GetSomething(GetResultFromUrl(newurl).Replace(@"\/", "/"), "http://([\\w]+[./]*)+"); //if (System.IO.Path.GetFileNameWithoutExtension(picUrl).ToLower() == "p") //{ if (picUrl != "") { Result[2] = picUrl.Replace(System.IO.Path.GetFileName(picUrl), string.Empty) + "w" + System.IO.Path.GetExtension(picUrl); } else { Result[2] = "服务器没有该视频的缩略图"; } //} //else //{ // Result[2] = ""; //} } else if (url.ToLower().StartsWith(htuDouContainUrl)) { int paIndex = url.IndexOf('?'); string[] directs = url.Remove(paIndex > 0 ? paIndex : url.Length - 1).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string param = string.Empty;//获取地址中的id比如获取http://www.tudou.com/programs/view/uuxZVpkDtUw/?union_id=100493_100001_02_01中uuxZVpkDtUw if (directs.Length > 0) param = directs[directs.Length - 1]; //-----------------------------------得到视频地址--------------------------------- Result[1] = CombineStr(htuDouVideoUrlByKey, param); //接下来得到缩略图,调用方法获取视频缩略图 TuDouSwf ts = new TuDouSwf(); Result[2]=ts.GetVideoInfo(CombineStr(htuDouVideoUrlByKey, param))[2]; } else //其他情况 { // } return Result; }
给出土豆视频地址:
//二、输入视频地址(不带.swf) //1.根据视频地址请求的地址中含有lid,不含snap_pic图片地址 static string stuDouThumUrl = "http://www.tudou.com/playlist/service/getPlaylists.html?lids={0}";//-------------如果传入路径中包含lid,根据截取出的lid得到缩略图地址 static string stuDouHtmlUrl1 = "http://www.tudou.com/playlist/p/l{0}.html";//----根据视频lid值得到视频的网页地址 //2.根据视频地址请求的地址中不含lid,含有snap_pic图片地址 static string stuDouRemove = "http://www.tudou.com/v/";//----截取视频唯一值时用到的一段 static string stuDouHtmlUrl2 = "http://www.tudou.com/programs/view/{0}/";//------根据传入的视频地址截取唯一值拼出网页地址 public override string[] GetVideoInfo(string url) { Result[1] = url;//视频地址 string mc = GetParamFromUrl(url); string[] str = mc.Split('&'); string strlid = "";//获取路径中的lid string snap_pic = "";//获取缩略图路径 foreach (var item in str) { if (item.Contains("lid=")) { if (item.StartsWith("lid=")) { strlid = item.ToString(); } } else if (item.Contains("snap_pic=")) { if (item.StartsWith("snap_pic=")) { snap_pic = item.ToString(); } } } //两种获取缩略图的方式: //1.含有lid的 if (strlid != "") { string newurl = CombineStr(stuDouThumUrl, strlid.Replace("lid=", "")); Result[0] = CombineStr(stuDouHtmlUrl1, strlid.Replace("lid=", "")); string picUrl = GetSomething(GetResultFromUrl(newurl).Replace(@"\/", "/"), "http://([\\w]+[./]*)+"); Result[2] = picUrl.Replace(System.IO.Path.GetFileName(picUrl), string.Empty) + "w" + System.IO.Path.GetExtension(picUrl); } //2.不含lid,含有缩略图路径 else if(snap_pic!="") { string uml = url.Replace(stuDouRemove,""); if (uml.IndexOf("/") > 0) { uml = uml.Remove(uml.IndexOf("/")); } Result[0] = CombineStr(stuDouHtmlUrl2,uml); Result[2] = System.Web.HttpContext.Current.Server.UrlDecode(snap_pic.Replace("snap_pic=","")); } return Result; }
如有不足的地方请多多指教,如果有友友在开发中遇到这样的问题,希望可以帮助你!
源码地址:
源代码