gpt4 book ai didi

c# - 下载视频时,YoutubeExtractor上的System.Net.WebException

转载 作者:行者123 更新时间:2023-12-03 05:31:15 40 4
gpt4 key购买 nike

环境

  • Windows 8.1
  • Visual Studio 2017社区
  • C#
  • WPF应用程序

  • 问题
  • YoutubeExtractor在下载视频时引发System.Net.WebException。

  • 我有Nuget的YoutubeExtractor,它不起作用。 VideoInfo对象不为null。我在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"))));

    最佳答案

    如果没有其他效果,则可以尝试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")));

    关于c# - 下载视频时,YoutubeExtractor上的System.Net.WebException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46502734/

    40 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com