private async Task <YoutubeVideoCache> GetCacheAsync() { var result = YoutubeVideoCache.Load(Code); if (result != null) { return(result); } try { Console.WriteLine($"{Code}:Begin"); var youTube = new YoutubeClient(); var streamManifest = await youTube.Videos.Streams.GetManifestAsync(Code); var streamInfo = streamManifest.GetMuxed(); Console.WriteLine($"{Code}:Got videos list"); result = new YoutubeVideoCache { Hash = Code }; var videosToDownload = streamInfo; videoDownloaders = videosToDownload .Select(x => DownloadVideoAsync(youTube, x, result)); await Task.WhenAll(videoDownloaders); result.Mips = new Dictionary <int, string>(videosToDownload .Select(x => new KeyValuePair <int, string>(x.Resolution.Height, FileNameFormatter(result, x))) .OrderBy(x => x.Key)); result.Save(); } catch (Exception ex) { Log.Error.Here(ex.Message); Environment.Exit(1); } return(result); }
private async Task DownloadVideoAsync(YoutubeClient youTube, MuxedStreamInfo streamInfo, YoutubeVideoCache cache) { Utils.CreateDirectory(cache.Directory); var path = Path.Combine(cache.Directory, FileNameFormatter(cache, streamInfo)); if (File.Exists(path)) { return; } Console.WriteLine($"{Code}: start download {path}"); await youTube.Videos.Streams.DownloadAsync(streamInfo, path); Console.WriteLine($"{Code}: download {path} done."); }
private static string FileNameFormatter(YoutubeVideoCache cache, MuxedStreamInfo streamInfo) => $"{cache.Hash}_{streamInfo.Resolution.Height}.{streamInfo.Container}";