public virtual async Task <string?> DownloadYouTubeAsync(string url, MediaType.MediaCodec codec) { string unified = YouTuberHelpers.UnifyYouTubeUrl(url); if (string.IsNullOrWhiteSpace(unified)) { return(Config.InvalidYouTube); } if (IsDuplicate(unified)) { return(Config.DuplicateYouTube); } IEnumerable <YouTubeVideo> videos = await _client.GetAllAvailableFormatAsync(unified); var video = FilterOnlyVideoFormats(videos).MaxBy(e => e.Resolution); string validationMessage = ValidateVideo(video); if (validationMessage != "OK") { return(validationMessage); } YouTuberHelpers.CreateFolder(Config.BaseFolder); string path = Path.Combine(Config.BaseFolder, video !.FullName); await File.WriteAllBytesAsync(path, await video.GetBytesAsync()); GetAudio(path, codec); return($"{video.Title} is ready under {Config.BaseFolder}"); }
private void GetAudio(string path, MediaType.MediaCodec codec) { if (codec == MediaType.MediaCodec.none) { return; } lock (_set) { ExtractAudio(path, codec).Wait(); File.Delete(path); } }
public virtual async Task DownloadYouTubeAsync(IEnumerable <string> urls, MediaType.MediaCodec codec = MediaType.MediaCodec.none) { ParallelOptions options = new ParallelOptions(); int maxProc = Environment.ProcessorCount; options.MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling(maxProc * 1.75)); int count = 1; await Parallel.ForEachAsync(urls, options, async (url, token) => { var result = await DownloadYouTubeAsync(url, codec); if (!string.IsNullOrWhiteSpace(result)) { Console.WriteLine($"{count++}- {result}"); } }); }
private static async Task ExtractAudio(string path, MediaType.MediaCodec codec) { var currentFolder = Directory.GetCurrentDirectory(); var ffmpegPath = $"{currentFolder}/FFmpeg"; FFmpeg.SetExecutablesPath(ffmpegPath, "FFmpeg"); await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Official, ffmpegPath); FileInfo fi = new FileInfo(path); string inputPath = fi.FullName; string outputPath = Path.ChangeExtension(inputPath, codec.ToString()); if (File.Exists(outputPath)) { File.Delete(outputPath); } var conversion = await FFmpeg.Conversions.FromSnippet.ExtractAudio(inputPath, outputPath); await conversion.Start(); await Task.Delay(500); }
public void MapAudioTypeTest(int order, string input, MediaType.MediaCodec expected) { var codec = YouTuberHelpers.MapAudioType(input); codec.ShouldBe(expected); }