小伟

小伟

 

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

 

分类: ASP.NET 154人阅读 评论(2) 收藏 举报


  第一次写博客,心里有点小激动!前一段时间做一个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/XMzE2MzI5MDMy/version/5/source/out?onData=%5Btype%20Function%5D&n=3  此地址是获取视频缩略图的!我们可以模仿http请求该地址获取存有缩略图的源文件!其中{0}是参数,该参数是从.swf视频地址中获取的。比如以上第一种情况中XMzY3NzExNjgw或者是第二中情况中XMzE2MzI5MDMy都是视频的唯一标识值。以下分析图

给出优酷网页地址:

  1. //根据输入的网页地址截取出唯一id,根据id拼出视频地址   
  2.  string hyouKuVideoUrlById = "http://player.youku.com/player.php/sid/{0}/v.swf";  
  3.  //根据视频地址提供的唯一id,请求出缩略图所在地址  string syouKuPicUrlById = "http://v.youku.com/player/getPlayList/VideoIDS/{0}/version/5/source/out?onData=%5Btype%20Function%5D&n=3";   
  4.         string htmlRegxStr = "id=\"link[0-9]+\"[ ]+value=\"http://([\\w]+[.])+[/.\\w_]+";  
  5.   
  6.         public override string[] GetVideoInfo(string url)  
  7.         {  
  8.             Result[0] = url;  
  9.             string videoName = GetVideoIdFromUrl(url);  
  10.  
  11.             #region //http://v.youku.com/v_show/id_XMjkwMzAwMzY4.html   
  12.             if (videoName.ToLower().StartsWith("id_"))  
  13.             {  
  14.   
  15.                 videoName = videoName.Trim().Remove(0, 3);  
  16.                 Result[1] = CombineStr(hyouKuVideoUrlById, videoName);  
  17.                 Result[2] = GetSomething(  
  18.                             GetResultFromUrl(CombineStr(syouKuPicUrlById, videoName)).Replace(@"\/""/"),  
  19.                             imgLinkRegxStr  
  20.                             );//"(http://[^\"]+)[\\w\\W]+\"title\":\"([^\"]+)\""   
  21.             }  
  22.             #endregion  
  23.  
  24.             #region //http://v.youku.com/v_playlist/f17213424o1p0.html   
  25.             else if (videoName.ToLower().StartsWith("f"))  
  26.             {  
  27.                 string info = GetResultFromUrl(url);  
  28.                 MatchCollection cc = GetSomethingCollection(info, htmlRegxStr);  
  29.   
  30.                 for (int i = 0; i < cc.Count; i++)  
  31.                 {  
  32.                     Result[1] = GetSomething(cc[i].Value, hypeLinkRegxStr);  
  33.                     if (Result[1].ToLower().EndsWith(".swf"))  
  34.                     {  
  35.                         break;  
  36.                     }  
  37.                 }  
  38.                 if (Result[1] != "")  
  39.                 {  
  40.                     string getIed = GetVideoId(Result[1], '/', 1);  
  41.                     if (getIed != "")  
  42.                     {  
  43.                         Result[2] = GetSomething(GetResultFromUrl(CombineStr(syouKuPicUrlById, getIed)).Replace(@"\/""/"), imgLinkRegxStr);  
  44.                     }  
  45.                 }  
  46.             }  
  47.             #endregion  
  48.  
  49.             #region else   
  50.             else  
  51.             {   
  52.                 //扩展其他情况:   
  53.             }  
  54.             #endregion   
  55.   
  56.             return Result;  
  57.         }  

给出优酷视频地址:

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


 

 

土豆视频

给出土豆网页地址:

 

  1. //一、输入视频网页(html页面)   
  2.         //1.带.html(规则的网页)   
  3.         static string htuDouThumUrl = "http://www.tudou.com/playlist/service/getPlaylists.html?lids={0}";//---根据lid查询缩略图地址  
  4.         static string htuDouVideoUrlId = "http://v2.tudou.com/v?vn=02&st=1%2C2&it={0}";//-------根据lid查询视频地址需要的id  
  5.         static string htuDouVideoUrlById = "http://www.tudou.com/l/{0}";//-------根据查出来的视频id拼出视频地址  
  6.         //2.不带.html(不规则的网页)   
  7.         static string htuDouContainUrl = "http://www.tudou.com/programs/view/";//-------------不规则的网页地址里面包含此内容  
  8.         static string htuDouVideoUrlByKey = "http://www.tudou.com/v/{0}";//----------根据网页地址截取其中的视频唯一值key,获得视频地址  
  9.       public override string[] GetVideoInfo(string url)  
  10.       {  
  11.           Result[0] = url;//网页地址   
  12.           string lid = "";  
  13.           //1.规则的网页地址:即带html后缀   
  14.           if (url.ToLower().Contains(".html"))  
  15.           {  
  16.               string fileName = string.Empty;  
  17.               if (url.IndexOf('?') > 0)  
  18.               {  
  19.                   fileName = System.IO.Path.GetFileName(url.Remove(url.IndexOf('?')));  
  20.               }  
  21.               else  
  22.               {  
  23.                   fileName = System.IO.Path.GetFileName(url);  
  24.               }  
  25.               fileName = fileName.Replace(".html"string.Empty);  
  26.               int iindex = fileName.ToLower().IndexOf("i");  
  27.               if (iindex < 0) iindex = fileName.Length - 1;  
  28.               int lindex = fileName.ToLower().IndexOf("l");  
  29.               if (lindex >= 0)  
  30.               {  
  31.                   lid = fileName.Substring(lindex + 1, iindex - lindex - 1);  
  32.               }  
  33.               else  
  34.               {  
  35.                   lid = "";  
  36.               }  
  37.               //获取视频地址   
  38.   
  39.               string vedioSl = GetSomething(GetResultFromUrl(CombineStr(htuDouVideoUrlId, lid)), "cd=\"[\\S]+\"");  
  40.               if (vedioSl != "")  
  41.               {  
  42.                   Result[1] = CombineStr(htuDouVideoUrlById, vedioSl.Remove(0, 4).Replace("\""""));  
  43.               }  
  44.               else   
  45.               {  
  46.                   Result[1] = url;  
  47.               }  
  48.               //获取缩略图   
  49.               string newurl = CombineStr(htuDouThumUrl, lid);  
  50.               string picUrl = GetSomething(GetResultFromUrl(newurl).Replace(@"\/""/"), "http://([\\w]+[./]*)+");  
  51.               //if (System.IO.Path.GetFileNameWithoutExtension(picUrl).ToLower() == "p")   
  52.               //{   
  53.               if (picUrl != "")  
  54.               {  
  55.                   Result[2] = picUrl.Replace(System.IO.Path.GetFileName(picUrl), string.Empty) + "w" + System.IO.Path.GetExtension(picUrl);  
  56.               }  
  57.               else   
  58.               {  
  59.                   Result[2] = "服务器没有该视频的缩略图";  
  60.               }  
  61.               //}   
  62.               //else   
  63.               //{   
  64.               //    Result[2] = "";   
  65.               //}   
  66.           }  
  67.           else if (url.ToLower().StartsWith(htuDouContainUrl))  
  68.           {  
  69.               int paIndex = url.IndexOf('?');  
  70.               string[] directs = url.Remove(paIndex > 0 ? paIndex : url.Length - 1).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);  
  71.               string param = string.Empty;//获取地址中的id比如获取http://www.tudou.com/programs/view/uuxZVpkDtUw/?union_id=100493_100001_02_01中uuxZVpkDtUw   
  72.               if (directs.Length > 0)  
  73.                   param = directs[directs.Length - 1];  
  74.               //-----------------------------------得到视频地址---------------------------------   
  75.               Result[1] = CombineStr(htuDouVideoUrlByKey, param);  
  76.               //接下来得到缩略图,调用方法获取视频缩略图   
  77.               TuDouSwf ts = new TuDouSwf();  
  78.                 
  79.               Result[2]=ts.GetVideoInfo(CombineStr(htuDouVideoUrlByKey, param))[2];  
  80.           }  
  81.           else //其他情况   
  82.           {  
  83.             //   
  84.           }  
  85.           return Result;  
  86.       }  

给出土豆视频地址:

  1. //二、输入视频地址(不带.swf)   
  2.        //1.根据视频地址请求的地址中含有lid,不含snap_pic图片地址   
  3.        static string stuDouThumUrl = "http://www.tudou.com/playlist/service/getPlaylists.html?lids={0}";//-------------如果传入路径中包含lid,根据截取出的lid得到缩略图地址  
  4.        static string stuDouHtmlUrl1 = "http://www.tudou.com/playlist/p/l{0}.html";//----根据视频lid值得到视频的网页地址  
  5.        //2.根据视频地址请求的地址中不含lid,含有snap_pic图片地址   
  6.        static string stuDouRemove = "http://www.tudou.com/v/";//----截取视频唯一值时用到的一段  
  7.        static string stuDouHtmlUrl2 = "http://www.tudou.com/programs/view/{0}/";//------根据传入的视频地址截取唯一值拼出网页地址  
  8.        
  9.       public override string[] GetVideoInfo(string url)  
  10.        {  
  11.            Result[1] = url;//视频地址   
  12.            string mc = GetParamFromUrl(url);  
  13.            string[] str = mc.Split('&');  
  14.            string strlid = "";//获取路径中的lid   
  15.            string snap_pic = "";//获取缩略图路径   
  16.            foreach (var item in str)  
  17.            {  
  18.                if (item.Contains("lid="))   
  19.                {  
  20.                    if (item.StartsWith("lid="))   
  21.                    {  
  22.                        strlid = item.ToString();  
  23.                    }  
  24.                }  
  25.                else if (item.Contains("snap_pic="))   
  26.                {  
  27.                    if (item.StartsWith("snap_pic="))   
  28.                    {  
  29.                        snap_pic = item.ToString();  
  30.                    }  
  31.                }  
  32.            }  
  33.   
  34.            //两种获取缩略图的方式:   
  35.            //1.含有lid的   
  36.            if (strlid != "")  
  37.            {  
  38.                string newurl = CombineStr(stuDouThumUrl, strlid.Replace("lid="""));  
  39.                Result[0] = CombineStr(stuDouHtmlUrl1, strlid.Replace("lid="""));  
  40.                string picUrl = GetSomething(GetResultFromUrl(newurl).Replace(@"\/""/"), "http://([\\w]+[./]*)+");  
  41.                Result[2] = picUrl.Replace(System.IO.Path.GetFileName(picUrl), string.Empty) + "w" + System.IO.Path.GetExtension(picUrl);  
  42.                  
  43.            }  
  44.                //2.不含lid,含有缩略图路径   
  45.            else if(snap_pic!="")  
  46.            {  
  47.                string uml = url.Replace(stuDouRemove,"");  
  48.                if (uml.IndexOf("/") > 0)   
  49.                {  
  50.                    uml = uml.Remove(uml.IndexOf("/"));  
  51.                }  
  52.                Result[0] = CombineStr(stuDouHtmlUrl2,uml);  
  53.                 
  54.                Result[2] = System.Web.HttpContext.Current.Server.UrlDecode(snap_pic.Replace("snap_pic=",""));  
  55.   
  56.            }  
  57.   
  58.            return Result;  
  59.        }  

posted on 2012-06-07 14:43  旋风  阅读(690)  评论(0)    收藏  举报

导航