2016-12-05 125 views
-1

我試圖使用YoutubeExtractor從YouTube下載音頻,但如果單擊按鈕我有信息「序列沒有元素」。我該如何解決它?從YouTube下載音頻c#

private void button2_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     IEnumerable<VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(textBox1.Text); 

     VideoInfo video = videos.Where(info => info.CanExtractAudio).OrderByDescending(info => info.AudioBitrate).First(); 

     if (video.RequiresDecryption) 
     { 
      DownloadUrlResolver.DecryptDownloadUrl(video); 
     } 
     AudioDownloader download = new AudioDownloader(video, Path.Combine(Application.StartupPath + "\\", video.Title + video.AudioExtension)); 

     download.Execute(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
+0

你知道YoutubeExtractor音頻提取目前僅適用於Flash視頻嗎? – Jim

回答

0

問題是致電First()。如果您的IEnumerable不包含任何項目,這將引發異常。

要糾正這一點,你需要的兩件事情是肯定的:

  1. DownloadUrlResolver.GetDownloadUrls()函數返回至少1項。
  2. 即在videos變量的至少一個項具有CanExtractAudio集到true
+0

我如何使用DownloadUrlResolver.GetDownloadUrls()返回一個項目; ? – Ops