/// <summary> /// Download the whole Playlist from YouTube url and extract it /// </summary> /// <param name="url">URL to the YouTube Playlist</param> /// <returns>The File Path to the downloaded mp3</returns> private static async Task <string> DownloadPlaylistFromYouTube(string url) { TaskCompletionSource <string> tcs = new TaskCompletionSource <string>(); new Thread(() => { string file; int count = 0; do { file = Path.Combine(DownloadPath, "tempvideo" + ++count + ".mp3"); } while (File.Exists(file)); MusicBot.Print("Downloading the song under " + file, ConsoleColor.Gray); //youtube-dl.exe Process youtubedl; string args = $"--extract-audio --audio-format mp3 -o \"{file.Replace(".mp3", ".%(ext)s")}\" {url}"; MusicBot.Print("In cmd: " + "youutube-dl " + args, ConsoleColor.Magenta); //Download Video ProcessStartInfo youtubedlDownload = new ProcessStartInfo() { FileName = "youtube-dl", Arguments = args, CreateNoWindow = true, RedirectStandardOutput = true, /*UseShellExecute = false*/ //Linux? }; youtubedl = Process.Start(youtubedlDownload); //Wait until download is finished youtubedl.WaitForExit(); if (File.Exists(file)) { //Return MP3 Path & Video Title tcs.SetResult(file); } else { //Error downloading tcs.SetResult(null); MusicBot.Print($"Could not download Song, youtube-dl responded with:\n\r{youtubedl.StandardOutput.ReadToEnd()}", ConsoleColor.Red); } }).Start(); string result = await tcs.Task; if (result == null) { throw new Exception("youtube-dl.exe failed to download!"); } //Remove \n at end of Line result = result.Replace("\n", "").Replace(Environment.NewLine, ""); return(result); }
/// <summary> /// Get Video Title from YouTube URL /// </summary> /// <param name="url">URL to the YouTube Video</param> /// <returns>The YouTube Video Title</returns> private static async Task <Tuple <string, string> > GetInfoFromYouTube(string url) { TaskCompletionSource <Tuple <string, string> > tcs = new TaskCompletionSource <Tuple <string, string> >(); MusicBot.Print("Creating new thread with youtube-dl", ConsoleColor.Red); new Thread(() => { string title; string duration; //youtube-dl.exe Process youtubedl; string args = $"-s -e --get-duration {url}"; MusicBot.Print("In cmd: " + "youtube-dl " + args, ConsoleColor.Magenta); //Get Video Title ProcessStartInfo youtubedlGetTitle = new ProcessStartInfo() { FileName = "youtube-dl", Arguments = args, CreateNoWindow = false, RedirectStandardOutput = true, /*UseShellExecute = false*/ //Linux? }; youtubedl = Process.Start(youtubedlGetTitle); MusicBot.Print("youtube-dl started", ConsoleColor.Green); youtubedl.WaitForExit(); MusicBot.Print("youtube-dl ended", ConsoleColor.Green); //Read Title string[] lines = youtubedl.StandardOutput.ReadToEnd().Split('\n'); if (lines.Length >= 2) { title = lines[0]; duration = lines[1]; } else { title = "No Title found"; duration = "0"; } tcs.SetResult(new Tuple <string, string>(title, duration)); }).Start(); Tuple <string, string> result = await tcs.Task; if (result == null) { throw new Exception("youtube-dl.exe failed to receive title!"); } return(result); }
/// <summary> /// Gets Title of Video or Song /// </summary> /// <param name="url">URL to Video or Song</param> /// <returns>The Title of the Video or Song</returns> public static async Task <Tuple <string, string> > GetInfo(string url) { if (url.ToLower().Contains("youtube.com")) { MusicBot.Print("File to play is from youtube.", ConsoleColor.Green); return(await GetInfoFromYouTube(url)); } else { throw new Exception("Video URL not supported!"); } }
private static async Task Do() { try { _cts = new CancellationTokenSource(); Bot = new MusicBot(); //Async Thread Block await Task.Delay(-1, _cts.Token); } catch (TaskCanceledException) { // Task Canceled } }
private static void Main(string[] args) { ConsoleHelper.Set(); Console.Title = "Music Bot (Loading...)"; Console.WriteLine("(Press Ctrl + C or close this Window to exit Bot)"); try { #region JSON.NET string json = File.ReadAllText("config.json"); Config cfg = JsonConvert.DeserializeObject <Config>(json); if (cfg == new Config()) { throw new Exception("Please insert values into Config.json!"); } #endregion #region TXT Reading //string[] config = File.ReadAllLines("config.txt"); //Config cfg = new Config() { // BotName = config[0].Split(':')[1], // ChannelName = config[1].Split(':')[1], // ClientId = config[2].Split(':')[1], // ClientSecret = config[3].Split(':')[1], // ServerName = config[4].Split(':')[1], // Token = config[5].Split(':')[1], //}; #endregion } catch (Exception e) { MusicBot.Print("Your config.json has incorrect formatting, or is not readable!", ConsoleColor.Red); MusicBot.Print(e.Message, ConsoleColor.Red); try { //Open up for editing Process.Start("config.json"); } catch { // file not found, process not started, etc. } Console.ReadKey(); return; } Do().GetAwaiter().GetResult(); //Thread Block //Thread.Sleep(-1); }