2017-09-30 69 views
1

環境

  • 的Windows 8.1
  • 的Visual Studio 2017年社區
  • C#
  • WPF應用程序

問題

  • YoutubeExtractor在下載視頻時拋出System.Net.WebException。

我得到了Nuget的YoutubeExtractor,它不起作用。 VideoInfo對象不爲null。我在Youtube上嘗試了幾個視頻,並且彈出了相同的例外情況。我搜索了這個問題,但沒有給我太多幫助。System.Net.WebException上YoutubeExtractor下載視頻

這是代碼。

var videos = DownloadUrlResolver.GetDownloadUrls(@"https://www.youtube.com/watch?v=M1wLtAXDgqg"); 
VideoInfo video = videos 
    .First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360); 

if (video.RequiresDecryption) 
    DownloadUrlResolver.DecryptDownloadUrl(video); 

var videoDownloader = new VideoDownloader(video, System.IO.Path.Combine("D:", video.Title + video.VideoExtension)); 

videoDownloader.DownloadProgressChanged += (sender_, args) => Console.WriteLine(args.ProgressPercentage); 

videoDownloader.Execute(); // I got the exception here. 

我該如何解決這個問題?謝謝。

編輯:2017年10月26日13:42 GMT

阿列克謝 'Tyrrrz' 戈盧布的回答幫助這麼多!我糾正了他的原始代碼,現在是這樣。

using YoutubeExplode; 
using YoutubeExplode.Models.MediaStreams; 

var client = new YoutubeClient(); 
var video = await client.GetVideoAsync("bHzHlSLhtmM"); 
// double equal signs after s.VideoQuality instead of one 
var streamInfo = video.MuxedStreamInfos.First(s => s.Container == Container.Mp4 && s.VideoQuality == VideoQuality.Medium360); 
// "D:\\" instead of "D:" 
var pathWithoutExtension = System.IO.Path.Combine("D:\\", video.Title); 
// streamInfo.Container.GetFileExtension() instead of video.VideoExtension (video doesn't have the property) 
var path = System.IO.Path.ChangeExtension(pathWithoutExtension, streamInfo.Container.GetFileExtension()); 
// new Progress<double>() instead of new Progress() 
await client.DownloadMediaStreamAsync(streamInfo, path, new Progress<double>(d => Console.WriteLine(d.ToString("p2")))); 

回答

1

如果沒有其他的工作,你可以嘗試YoutubeExplode這是一個更新的和維護的庫。然後

您的代碼將是:

var client = new YoutubeClient(); 
var video = await client.GetVideoAsync("M1wLtAXDgqg"); 
var streamInfo = video.MuxedStreamInfos.First(s => s.Container == Container.Mp4 && s.VideoQuality == VideoQuality.Medium360); 
var path = System.IO.Path.Combine("D:\\", video.Title + "." + streamInfo.Container.GetFileExtension()); 
await client.DownloadMediaStreamAsync(streamInfo, path, new Progress<double>(d => Console.WriteLine(d.ToString("p2"))); 
+1

謝謝!所以這是你的圖書館。我在代碼中發現了一些錯誤,並使用修正後的代碼更新了我原來的帖子。請檢查一下。 – dixhom

+0

@dixhom對此感到抱歉,我在寫這封信的時候很睏倦,忘記了檢查。 x_x –

+0

@dixhom很高興你已經明白了! –