2017-09-30 1 views
1

Environnement

  • de Windows 8.1
  • Visual Studio 2017 Communauté
  • C#
  • application WPF

Problème

  • YoutubeExtractor lance System.Net.WebException lors du téléchargement d'une vidéo.

J'ai obtenu YoutubeExtractor par Nuget et ça ne marche pas. L'objet VideoInfo n'est pas nul. J'ai essayé plusieurs vidéos sur Youtube et les mêmes exceptions sont apparues. J'ai googlé le problème et cela ne m'a pas beaucoup aidé.System.Net.WebException sur YoutubeExtractor lors du téléchargement d'une vidéo

Voici le code.

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. 

Comment puis-je résoudre ce problème? Merci.

EDIT: 26/10/2017 13:42 GMT

Alexey réponse de 'Tyrrrz' Golub a aidé tellement! J'ai corrigé son code original et le voici.

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")))); 

Répondre

1

Si rien ne fonctionne, vous pouvez essayer YoutubeExplode qui est plus à jour et de la bibliothèque maintenue pour cela.

Votre code serait alors:

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

Merci! Donc c'est ta bibliothèque. J'ai trouvé quelques erreurs dans votre code et mis à jour mon message original avec le code corrigé qui a bien fonctionné. S'il te plaît vérifie le. – dixhom

+0

@ Désolé pour cela, j'avais sommeil quand j'ai écrit cela et j'ai oublié de vérifier. x_x –

+0

@dixhom très heureux que vous l'ayez compris! –