/// https://github.com/BrianAllred/NYoutubeDL/commit/2bd39515eebb8c5fb866687781165804e3b0579f public async Task YouTubeDownloader(string _link, string _extension, string _path) { try { var youtubeDl = new YoutubeDL(); #region File Name string name = Random(100000, 999999).ToString(); while (File.Exists(_path + @"\" + name + _extension)) { name = Random(100000, 999999).ToString(); } #endregion File Name youtubeDl.Options.FilesystemOptions.Output = _path + @"\" + name + _extension; if (_extension == ".mp3") { youtubeDl.Options.PostProcessingOptions.ExtractAudio = true; } youtubeDl.VideoUrl = _link; youtubeDl.YoutubeDlPath = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Milkenm\Bronze Player", "Path", null).ToString() + "youtube-dl.exe"; await youtubeDl.PrepareDownloadAsync(); await youtubeDl.DownloadAsync(); } #region DE3UG catch (Exception exception) { tools.Exception(exception); } #endregion DE3UG }
static async Task Main(string[] args) { var url = args[0]; var dl = new YoutubeDL(); dl.Options.FilesystemOptions.Output = "."; dl.Options.PostProcessingOptions.ExtractAudio = true; dl.VideoUrl = url; dl.StandardErrorEvent += (s, o) => { Console.WriteLine("?? {0}", o); }; dl.StandardOutputEvent += (s, o) => { Console.WriteLine(":: {0}", o); }; // dl.Info.PropertyChanged += (s, o) => { // Console.WriteLine("~ {0}", o); // }; var rs = await dl.PrepareDownloadAsync(); Console.WriteLine("> {0}", rs); }
/// <summary> /// Gets general information about non-YT video /// </summary> /// <param name="url">url to video</param> /// <returns>Title, Thumbnail-Uri, duration</returns> public static async Task <DownloadInfo> GetGeneralInfoAsync(string url) { NYoutubeDL.YoutubeDL dl = new YoutubeDL(); dl.VideoUrl = url; dl.RetrieveAllInfo = true; await dl.PrepareDownloadAsync(); return(dl.Info); }
private async Task DownloadVideoAsync(string youtubeUrl) { try { var youtubeDl = new YoutubeDL(); WriteToStatusBar("Started downloading"); DirectoryInfo dir = new DirectoryInfo("Videos"); if (!dir.Exists) { dir.Create(); } var exportDir = dir.CreateSubdirectory(DateTime.Now.ToString("yyyy-MM-dd")); var exportFilePath = Path.Combine(exportDir.FullName, "video" + DateTime.Now.ToString("hh-mm-ss") + ".mkv"); //set format output and library locations youtubeDl.Options.FilesystemOptions.Output = exportFilePath; youtubeDl.Options.PostProcessingOptions.ExtractAudio = true; youtubeDl.Options.SubtitleOptions.AllSubs = true; youtubeDl.Options.PostProcessingOptions.KeepVideo = true; youtubeDl.Options.PostProcessingOptions.ConvertSubs = NYoutubeDL.Helpers.Enums.SubtitleFormat.srt; youtubeDl.Options.PostProcessingOptions.FfmpegLocation = @"ffmpeg-static\bin\ffmpeg.exe"; youtubeDl.VideoUrl = youtubeUrl; // Optional, required if binary is not in $PATH youtubeDl.YoutubeDlPath = @"youtube-dl\youtube-dl.exe"; youtubeDl.StandardOutputEvent += (sender, output) => WriteToStatusBar(output); youtubeDl.StandardErrorEvent += (sender, errorOutput) => WriteToStatusBar(errorOutput); // youtubeDl.Info.PropertyChanged += delegate { < your code here> }; // Prepare the download (in case you need to validate the command before starting the download) string commandToRun = await youtubeDl.PrepareDownloadAsync(); // Just let it run youtubeDl.Download(); // Wait for it WriteToStatusBar("Done! " + exportFilePath); } catch (Exception e) { WriteToStatusBar(e.Message.ToString()); } }
private async void button1_Click(object sender, EventArgs e) { txtTerminal.AppendText("Starting a video download run..."); mainProgressBar.Visible = true; txtVideoUrl.Enabled = false; button1.Enabled = false; btnChange.Enabled = false; menuStrip1.Enabled = false; var youtubeDl = new YoutubeDL(); // Optional, required if binary is not in $PATH youtubeDl.YoutubeDlPath = "lib\\youtube-dl.exe"; youtubeDl.VideoUrl = txtVideoUrl.Text; youtubeDl.Options.PostProcessingOptions.FfmpegLocation = "lib\\ffmpeg.exe"; youtubeDl.Options.PostProcessingOptions.PreferFfmpeg = true; var tmpDateTime = DateTime.Now.ToString("ddMMyyyy hhmmss"); var tmpFn = CleanFileName(tmpDateTime); if (chkAudioOnly.Checked) { tmpFn = $"{txtSaveFolder.Text}\\{tmpFn}.partial.%(ext)s"; youtubeDl.Options.FilesystemOptions.Output = tmpFn; youtubeDl.Options.PostProcessingOptions.AudioFormat = NYoutubeDL.Helpers.Enums.AudioFormat.mp3; youtubeDl.Options.PostProcessingOptions.KeepVideo = false; youtubeDl.Options.PostProcessingOptions.ExtractAudio = true; } else { tmpFn = $"{txtSaveFolder.Text}\\{tmpFn}.partial.mp4"; youtubeDl.Options.FilesystemOptions.Output = tmpFn; youtubeDl.Options.VideoFormatOptions.Format = NYoutubeDL.Helpers.Enums.VideoFormat.mp4; youtubeDl.Options.PostProcessingOptions.KeepVideo = true; } string commandToRun = await youtubeDl.PrepareDownloadAsync(); youtubeDl.StandardOutputEvent += (sdr, output) => { Console.WriteLine(output); this.txtTerminal.Invoke((Action) delegate { this.txtTerminal.AppendText(Environment.NewLine + output); }); }; youtubeDl.StandardErrorEvent += (sdr, errorOutput) => Console.WriteLine(errorOutput); // Prepare the download (in case you need to validate the command before starting the download) //string commandToRun = await youtubeDl.PrepareDownloadAsync(); youtubeDl.Info.PropertyChanged += delegate { this.mainProgressBar.Invoke((Action) delegate { this.mainProgressBar.Value = youtubeDl.Info.VideoProgress; }); //mainProgressBar.Update(); //var val = youtubeDl.Info.VideoProgress; }; //var vidTitle = youtubeDl.Info.Title; // Just let it run await youtubeDl.DownloadAsync(); txtVideoUrl.Enabled = true; button1.Enabled = true; mainProgressBar.Visible = false; btnChange.Enabled = true; menuStrip1.Enabled = true; if (!chkAudioOnly.Checked) { if (youtubeDl.Info != null && youtubeDl.Info.Title != null) { var newFilename = tmpFn.Replace(".partial", "").Replace(tmpDateTime, CleanFileName(youtubeDl.Info.Title)); File.Move(tmpFn, newFilename); txtTerminal.AppendText(Environment.NewLine + $"Saved as {newFilename}"); } else { var newFilename = tmpFn.Replace(".partial", ""); File.Move(tmpFn, newFilename); txtTerminal.AppendText(Environment.NewLine + $"Saved as {newFilename}"); } } else { //Download YouTube image url if (txtVideoUrl.Text.Contains("youtube")) { var youtubeMatch = new Regex(@"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)") .Match(txtVideoUrl.Text); var ytId = youtubeMatch.Success ? youtubeMatch.Groups[1].Value : string.Empty; if (!string.IsNullOrWhiteSpace(ytId)) { var imageBytes = await GetImageAsByteArray($"/vi/{ytId}/0.jpg", "https://img.youtube.com"); var fnToFind = tmpFn.Replace(@".partial.%(ext)s", ".partial.mp3"); ////Add an image to the audio file. TagLib.Id3v2.Tag.DefaultVersion = 3; TagLib.Id3v2.Tag.ForceDefaultVersion = true; var fileToEdit = TagLib.File.Create(fnToFind); fileToEdit.Tag.Title = "Untitled"; if (youtubeDl.Info != null && youtubeDl.Info.Title != null) { fileToEdit.Tag.Title = youtubeDl.Info.Title; } fileToEdit.Tag.Performers = new[] { "YouTube Downloader" }; fileToEdit.Tag.AlbumArtists = new[] { "YouTube Downloader" }; fileToEdit.Tag.Album = "YouTube"; fileToEdit.Tag.Year = (uint)DateTime.Now.Year; fileToEdit.Tag.Genres = new[] { "Pop" }; fileToEdit.Tag.Comment = " "; fileToEdit.Tag.Track = 1; var pic = new TagLib.Picture { Type = TagLib.PictureType.FrontCover, Description = "Cover", MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg, Data = imageBytes }; fileToEdit.Tag.Pictures = new TagLib.IPicture[] { pic }; var dt = fileToEdit.Tag.Pictures[0].Data.Data; fileToEdit.Save(); } } //Rename the audio file. if (youtubeDl.Info != null && youtubeDl.Info.Title != null) { var fnToFind = tmpFn.Replace(@".partial.%(ext)s", ".partial.mp3"); var newFilename = fnToFind.Replace(".partial", "").Replace(tmpDateTime, CleanFileName(youtubeDl.Info.Title)); File.Move(fnToFind, newFilename); txtTerminal.AppendText(Environment.NewLine + $"Audio saved as {newFilename}"); } else { var fnToFind = tmpFn.Replace(@".partial.%(ext)s", ".partial.mp3"); var newFilename = fnToFind.Replace(".partial", ""); File.Move(fnToFind, newFilename); txtTerminal.AppendText(Environment.NewLine + $"Audio saved as {newFilename}"); } } }