最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

C#实现土豆优酷等网站视频的缩略图

IT圈 admin 0浏览 0评论

C#实现土豆优酷等网站视频的缩略图


  第一次写博客,心里有点小激动!前一段时间做一个ASP.NET网站时涉及到发布视频的功能。这个功能要求给一个连接地址,然后生成视频缩略图,并提供播放功能!

  我是从百度找到谷歌都没有找到好的解决方案,包括CSDN上面提供一些案例代码,下载下来全是忽悠人,赚取我的积分啊!坑爹呀!你妹呀!好了,现在开始介绍我自己在优酷和土豆地址中找到的一些规律,做出的一点点东西!可能不是很全!

发布视频的时候可以发布视频的网页地址(.html),也可以发布视频(.swf)地址

  优酷视频:

  第一种情况: 

  .html

  .php/sid/XMzY3NzExNjgw/v.swf

  第二种情况:

  .html      

  .php/Type/Folder/Fid/17213424/Ob/1/Pt/0/sid/XMzE2MzI5MDMy/v.swf

重要的一个地址:/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3  此地址是获取视频缩略图的!我们可以模仿http请求该地址获取存有缩略图的源文件!其中{0}是参数,该参数是从.swf视频地址中获取的。比如以上第一种情况中XMzY3NzExNjgw或者是第二中情况中XMzE2MzI5MDMy都是视频的唯一标识值。以下分析图

给出优酷网页地址:

//根据输入的网页地址截取出唯一id,根据id拼出视频地址string hyouKuVideoUrlById = ".php/sid/{0}/v.swf";//根据视频地址提供的唯一id,请求出缩略图所在地址  string syouKuPicUrlById = "/{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 //.htmlif (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 //.htmlelse 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 elseelse{ //扩展其他情况:}#endregionreturn Result;}

给出优酷视频地址:

//根据视频地址提供的唯一id,请求出缩略图所在地址string syouKuPicUrlById = "/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3";public override string[] GetVideoInfo(string url){Result[0] = Result[1] = url;//.php/sid/XMzY3NzExNjgw/v.swf//.php/Type/Folder/Fid/17213424/Ob/1/Pt/0/sid/XMzE2MzI5MDMy/v.swfstring videoId = GetVideoId(url,'/',1);Result[2] = GetSomething(GetResultFromUrl(CombineStr(syouKuPicUrlById, videoId)).Replace(@"\/", "/"), imgLinkRegxStr);return Result;}


 

土豆视频

给出土豆网页地址:

//一、输入视频网页(html页面)//1.带.html(规则的网页)static string htuDouThumUrl = ".html?lids={0}";//---根据lid查询缩略图地址static string htuDouVideoUrlId = "=02&st=1%2C2&it={0}";//-------根据lid查询视频地址需要的idstatic string htuDouVideoUrlById = "/{0}";//-------根据查出来的视频id拼出视频地址//2.不带.html(不规则的网页)static string htuDouContainUrl = "/";//-------------不规则的网页地址里面包含此内容static string htuDouVideoUrlByKey = "/{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比如获取/?union_id=100493_100001_02_01中uuxZVpkDtUwif (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 = ".html?lids={0}";//-------------如果传入路径中包含lid,根据截取出的lid得到缩略图地址static string stuDouHtmlUrl1 = "{0}.html";//----根据视频lid值得到视频的网页地址//2.根据视频地址请求的地址中不含lid,含有snap_pic图片地址static string stuDouRemove = "/";//----截取视频唯一值时用到的一段static string stuDouHtmlUrl2 = "/{0}/";//------根据传入的视频地址截取唯一值拼出网页地址public override string[] GetVideoInfo(string url){Result[1] = url;//视频地址string mc = GetParamFromUrl(url);string[] str = mc.Split('&');string strlid = "";//获取路径中的lidstring 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;}

如有不足的地方请多多指教,如果有友友在开发中遇到这样的问题,希望可以帮助你!

源码地址:
源代码

C#实现土豆优酷等网站视频的缩略图


  第一次写博客,心里有点小激动!前一段时间做一个ASP.NET网站时涉及到发布视频的功能。这个功能要求给一个连接地址,然后生成视频缩略图,并提供播放功能!

  我是从百度找到谷歌都没有找到好的解决方案,包括CSDN上面提供一些案例代码,下载下来全是忽悠人,赚取我的积分啊!坑爹呀!你妹呀!好了,现在开始介绍我自己在优酷和土豆地址中找到的一些规律,做出的一点点东西!可能不是很全!

发布视频的时候可以发布视频的网页地址(.html),也可以发布视频(.swf)地址

  优酷视频:

  第一种情况: 

  .html

  .php/sid/XMzY3NzExNjgw/v.swf

  第二种情况:

  .html      

  .php/Type/Folder/Fid/17213424/Ob/1/Pt/0/sid/XMzE2MzI5MDMy/v.swf

重要的一个地址:/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3  此地址是获取视频缩略图的!我们可以模仿http请求该地址获取存有缩略图的源文件!其中{0}是参数,该参数是从.swf视频地址中获取的。比如以上第一种情况中XMzY3NzExNjgw或者是第二中情况中XMzE2MzI5MDMy都是视频的唯一标识值。以下分析图

给出优酷网页地址:

//根据输入的网页地址截取出唯一id,根据id拼出视频地址string hyouKuVideoUrlById = ".php/sid/{0}/v.swf";//根据视频地址提供的唯一id,请求出缩略图所在地址  string syouKuPicUrlById = "/{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 //.htmlif (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 //.htmlelse 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 elseelse{ //扩展其他情况:}#endregionreturn Result;}

给出优酷视频地址:

//根据视频地址提供的唯一id,请求出缩略图所在地址string syouKuPicUrlById = "/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3";public override string[] GetVideoInfo(string url){Result[0] = Result[1] = url;//.php/sid/XMzY3NzExNjgw/v.swf//.php/Type/Folder/Fid/17213424/Ob/1/Pt/0/sid/XMzE2MzI5MDMy/v.swfstring videoId = GetVideoId(url,'/',1);Result[2] = GetSomething(GetResultFromUrl(CombineStr(syouKuPicUrlById, videoId)).Replace(@"\/", "/"), imgLinkRegxStr);return Result;}


 

土豆视频

给出土豆网页地址:

//一、输入视频网页(html页面)//1.带.html(规则的网页)static string htuDouThumUrl = ".html?lids={0}";//---根据lid查询缩略图地址static string htuDouVideoUrlId = "=02&st=1%2C2&it={0}";//-------根据lid查询视频地址需要的idstatic string htuDouVideoUrlById = "/{0}";//-------根据查出来的视频id拼出视频地址//2.不带.html(不规则的网页)static string htuDouContainUrl = "/";//-------------不规则的网页地址里面包含此内容static string htuDouVideoUrlByKey = "/{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比如获取/?union_id=100493_100001_02_01中uuxZVpkDtUwif (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 = ".html?lids={0}";//-------------如果传入路径中包含lid,根据截取出的lid得到缩略图地址static string stuDouHtmlUrl1 = "{0}.html";//----根据视频lid值得到视频的网页地址//2.根据视频地址请求的地址中不含lid,含有snap_pic图片地址static string stuDouRemove = "/";//----截取视频唯一值时用到的一段static string stuDouHtmlUrl2 = "/{0}/";//------根据传入的视频地址截取唯一值拼出网页地址public override string[] GetVideoInfo(string url){Result[1] = url;//视频地址string mc = GetParamFromUrl(url);string[] str = mc.Split('&');string strlid = "";//获取路径中的lidstring 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;}

如有不足的地方请多多指教,如果有友友在开发中遇到这样的问题,希望可以帮助你!

源码地址:
源代码

发布评论

评论列表 (0)

  1. 暂无评论