下载视频时YoutubeExtractor上的System.Net.WebException

时间:2017-09-30 12:32:09

标签: c# wpf visual-studio download youtube

环境

  • Windows 8.1
  • Visual Studio 2017社区
  • C#
  • WPF申请

问题

  • 下载视频时,YoutubeExtractor会抛出System.Net.WebException。

我得到了Nuget的YoutubeExtractor,它没有用。 VideoInfo对象不为空。我在Youtube上尝试了几个视频,弹出了同样的异常。我搜索了这个问题,并没有给我太多帮助。

这是代码。

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

Alexey' Tyrrrz' Golub的回答非常有帮助!我纠正了他的原始代码,现在是。

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 个答案:

答案 0 :(得分: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")));