From cebaff589cfd38b8a613d2732084407d6a2c1c29 Mon Sep 17 00:00:00 2001 From: Sorashi Date: Sun, 26 Nov 2017 17:40:46 +0100 Subject: [PATCH] Fix #1 --- youtube-play/LinkResolver.cs | 17 +++++++++++++++-- youtube-play/Player.cs | 5 ++++- youtube-play/Program.cs | 8 +++++--- youtube-play/Properties/AssemblyInfo.cs | 4 ++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/youtube-play/LinkResolver.cs b/youtube-play/LinkResolver.cs index 01f1e52..8436f0a 100644 --- a/youtube-play/LinkResolver.cs +++ b/youtube-play/LinkResolver.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json.Linq; +using System; +using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -55,6 +56,7 @@ public Video(string id, string title = null) { public async Task GetBestStreamableAudioFormat() { if (bestStreamableAudioFormat != null) return bestStreamableAudioFormat; var videoFormats = await GetFormats(); + if (videoFormats == null) return null; // at first try to find audio only var audioOnly = videoFormats.Where(x => x.Note == "DASH audio" && (x.Extension == "webm" || x.Extension == "m4a")); @@ -103,11 +105,22 @@ public Task> GetFormats() { Arguments = "-j " + Id, UseShellExecute = false, RedirectStandardOutput = true, + RedirectStandardError = true, CreateNoWindow = true } }; iteratorProcess.Start(); - var jo = JObject.Parse(iteratorProcess.StandardOutput.ReadToEnd()); + var stdout = iteratorProcess.StandardOutput.ReadToEnd(); + var stderr = iteratorProcess.StandardError.ReadToEnd(); + iteratorProcess.WaitForExit(); + var code = iteratorProcess.ExitCode; + if (code != 0) { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("An error occured while resolving the video link:\n" + stderr); + Console.ResetColor(); + return null; + } + var jo = JObject.Parse(stdout); return ResolveFormats(jo["formats"].ToString()); }); Formats = t; diff --git a/youtube-play/Player.cs b/youtube-play/Player.cs index 3a99153..f7f98d6 100644 --- a/youtube-play/Player.cs +++ b/youtube-play/Player.cs @@ -32,7 +32,10 @@ public Task StartPlaying(int volume = 20) { } public async Task AddToQueue(LinkResolver.Video video) { - await video.GetBestStreamableAudioFormat(); + if (await video.GetBestStreamableAudioFormat() == null) { + Console.WriteLine("Skipped " + video.Title); + return playQueue.Count; + } Console.WriteLine("Link resolved: " + video.Title); playQueue.Enqueue(video); return playQueue.Count; diff --git a/youtube-play/Program.cs b/youtube-play/Program.cs index 75e3648..34cf8ec 100644 --- a/youtube-play/Program.cs +++ b/youtube-play/Program.cs @@ -51,7 +51,8 @@ private static async Task MainAsync() { if (!args.Any()) { Console.WriteLine("\nUsage:"); Console.WriteLine("youtube-play