2011-11-30 116 views
0

我想從YouTube下載視頻。我想如何從YouTube獲取視頻下載鏈接?

http://www.youtube.com/watch?v=Fwa_GvIBH38&feature=feedrec_grec_index 

http://o-o.preferred.btcl-dac1.v6.lscache4.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Csource%2Calgorithm%2Cburst%2Cfactor%2Ccp&fexp=914016%2C904443&algorithm=throttle-factor&itag=34&ip=58.0.0.0&burst=40&sver=3&signature=82D07EBDBED8BC79D08AEE3F90B1473E44D4065E.88AB21BBB9E2D252B65FE6C419CD7867CDE8815C&source=youtube&expire=1322694000&key=yt1&ipbits=8&factor=1.25&cp=U0hRR1ZUUl9FSkNOMV9OTlZBOmRXLUt2VHYwWVY2&id=1706bf1af2011f7f&ptchn=collegehumor&ptk=collegehumor 

我上面從IDM鏈接。

我從一個網站獲得兩種方法來獲得上述鏈接。

//this methods get's the download link for youtube videos in mp4 format. 
    public string url(string url) 
    {  
     string html = getYoutubeHtml(url);  
     HtmlAgilityPack.HtmlDocument hDoc = new HtmlDocument();  
     hDoc.LoadHtml(html);  
     HtmlNode node = hDoc.GetElementbyId("movie_player");  
     string flashvars = node.Attributes[5].Value; 
     string _url = Uri.UnescapeDataString(flashvars);  
     string[] w = _url.Split('&');  
     string link = ""; 
     bool foundUrlMap = false; 
     for (int i = 0; i < w.Length; i++) 
     {  
      if (w[i].Contains("fmt_url_map="))   
      {   
       foundUrlMap = true;  
       link += w[i].Split('|')[1];  
      }   
      if (foundUrlMap)  
      {   
       //add the parameters to the url  
       link += "&" + w[i];   
       if (w[i].Contains("id="))  
       {    
        link = link.Split(',')[0]; 
        //change the array index for different formats   
        break;    
       }  
      }   
     }   
     link = link.Split('|')[1] + "&title=out"; 
     System.Windows.MessageBox.Show(link); 
     return link; 
    } 
    //this method downloads the html code from the youtube page. 
    private string getYoutubeHtml(string url)  
    {   
     string html = ""; 
     WebRequest request = WebRequest.Create(url); 
     WebResponse response = request.GetResponse(); 
     TextReader reader = new StreamReader(response.GetResponseStream()); 
     string line = ""; 
     while ((line = reader.ReadLine()) != null)   
     {   
      html += line;  
     }  
     return html; 
    } 

這不是work.ing

它沒有找到fmt_url_map=瓦特

所以現在我能做什麼裏面?

+0

嘗試轉換[這個問題的回答(http://stackoverflow.com/questions/6015962/how-直接加載流這個解決方案不起作用。C# – Blender

+0

該解決方案不起作用。 – Vero009

+0

您是否已將其轉換? – Blender

回答

2

字符串fmt_url_map在youtube上不存在/不再存在,與此相關的問題是,youtube是一個持續發展的野獸。這就是說目前這個工作。

您目前需要搜索的字符串是「url_encoded_fmt_stream_map」,但再次通過「url =」拆分更容易。

我花了幾個小時測試這個今天,最奇怪的一點是讓flashvars字符串正確解碼。

 string _url = string.Empty; 
     //Crazy string! 
     _url = Uri.UnescapeDataString(flashvars); 
     _url = HttpUtility.HtmlDecode(_url); 
     _url = HttpUtility.UrlDecode(_url); 
     _url = Uri.UnescapeDataString(_url); 

其中需要兩次Uri.UnescapeDataString()!

public string url(string url, string videoFormat) 
    { 
     HtmlAgilityPack.HtmlDocument hDoc = new HtmlDocument(); 
     hDoc.Load(new WebClient().OpenRead(url)); 
     HtmlNode node = hDoc.GetElementbyId("movie_player"); 
     string flashvars = node.Attributes[5].Value; 

     string _url = string.Empty; 
     //Crazy string! 
     _url = Uri.UnescapeDataString(flashvars); 
     _url = HttpUtility.HtmlDecode(_url); 
     _url = HttpUtility.UrlDecode(_url); 
     _url = Uri.UnescapeDataString(_url); 

     string[] w = _url.Split(new[] {"url="}, StringSplitOptions.None); 
     string link = ""; 
     if(!string.IsNullOrEmpty(videoFormat)) 
     { 
      foreach (string t in w) 
      { 
       if(t.Contains("type=") && t.Contains(videoFormat)) 
       { 
        link = t; 
        break; 
       } 
      } 
     } 
     else 
     { 
      link = w[1]; 
     } 

     link += "&title=out"; 
     System.Windows.MessageBox.Show(link); 
     Process.Start(link); 
     return link; 
    } 

所以對於一些加分的方法現在接受你傳遞您所需要的視頻格式,「MP4」,「X-flv」和「WEBM」的字符串。

而且它不再需要你的getYoutubeHtml()方法,它從Web客戶端

HtmlAgilityPack.HtmlDocument hDoc = new HtmlDocument(); 
    hDoc.Load(new WebClient().OpenRead(url)); 
+1

我知道這已經過了一年多了,正如你所說YouTube一直在變化。話雖如此,我認爲這種方法不再適用。原因:flashvars變量是通過javascript生成的,「佔位符」flashvars被編碼的javascript字符串替換。檢查一個YT視頻的來源,並找到這個:'swf = swf.replace('__ flashvars__',encoded.join('&'));' – Anders