Skip to content

Commit

Permalink
Add verbose switch
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-zk committed Oct 10, 2021
1 parent 78b91ae commit 7d15b70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Creates a YouTube ASH file for Audiosurf 2 from the command line.
-p, --proc=VALUE Path to youtube-dl or a compatible fork.
Default: ./youtube-dl
-6 Forces the downloader to use IPv6.
-v, --verbose Displays downloader console output.

## Installation
### Linux
Expand Down
11 changes: 8 additions & 3 deletions yashgen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static void Main(string[] args)
string destination = ".";
string ydlPath = "./youtube-dl";
bool forceIpv6 = false;
bool verbose = false;

var p = new OptionSet()
{
Expand All @@ -41,6 +42,9 @@ static void Main(string[] args)
{ "6",
"Forces the downloader to use IPv6.",
x => { forceIpv6 = true; } },
{ "v|verbose",
"Displays downloader console output.",
x => { verbose = true; } },
};

if (args.Length == 0)
Expand All @@ -61,7 +65,7 @@ static void Main(string[] args)
{
if (IsYoutubeId(id))
{
CreateAndSaveYash(id, destination, ydlPath, forceIpv6);
CreateAndSaveYash(id, destination, ydlPath, forceIpv6, verbose);
Console.WriteLine("Done\n");
}
else
Expand All @@ -87,14 +91,15 @@ static void Main(string[] args)
static bool IsYoutubeId(string input)
=> Regex.IsMatch(input, "^[a-zA-Z0-9_-]{11}$");

static void CreateAndSaveYash(string videoId, string destination, string ydlPath, bool forceIpv6 = false)
static void CreateAndSaveYash(string videoId, string destination, string ydlPath,
bool forceIpv6, bool verbose)
{
Console.WriteLine("Processing {0}", videoId);
Console.WriteLine("Downloading audio");
string ytAudioFile;
try
{
ytAudioFile = YoutubeDl.CallYoutubeDl(videoId, ydlPath, forceIpv6);
ytAudioFile = YoutubeDl.CallYoutubeDl(videoId, ydlPath, forceIpv6, verbose);
}
catch (YoutubeDlException)
{
Expand Down
22 changes: 10 additions & 12 deletions yashgen/YoutubeDl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class YoutubeDl
/// </summary>
/// <param name="videoId">The ID of the video.</param>
/// <returns>The path to the downloaded file.</returns>
public static string CallYoutubeDl(string videoId, string ydlPath, bool forceIpv6 = false)
public static string CallYoutubeDl(string videoId, string ydlPath,
bool forceIpv6, bool verbose)
{
errors = "";
var tempFile = Path.Combine(OUTPUT_FOLDER, videoId + ".wav");
Expand All @@ -41,21 +42,25 @@ public static string CallYoutubeDl(string videoId, string ydlPath, bool forceIpv
if (forceIpv6)
info.Arguments += " -6";

#if DEBUG
if (verbose)
{
Console.WriteLine("videoUrl: " + videoUrl);
Console.WriteLine("args: " + info.Arguments);
#endif
}

info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
var process = new Process();
process.StartInfo = info;
process.OutputDataReceived += Process_OutputDataReceived;
if (verbose)
{
process.OutputDataReceived += (_, e) => Console.WriteLine(e.Data);
}
process.ErrorDataReceived += ErrorDataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.Dispose();
Expand All @@ -66,13 +71,6 @@ public static string CallYoutubeDl(string videoId, string ydlPath, bool forceIpv
return tempFile;
}

private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
#if DEBUG
Console.WriteLine(e.Data);
#endif
}

public static void PrintVersion(string ydlPath)
{
var info = new ProcessStartInfo(ydlPath, "--version");
Expand Down

0 comments on commit 7d15b70

Please sign in to comment.