Skip to content

Commit

Permalink
updated with convert folder in target directory
Browse files Browse the repository at this point in the history
  • Loading branch information
SH4KUR committed Jan 10, 2024
1 parent 28a327a commit 93ffb1d
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions AudioBitDepthConvert/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ private static void Main(string[] args)
Console.Write("Enter the path of the directory: ");
var directoryPath = Console.ReadLine();

if (Directory.Exists(directoryPath))
if (!string.IsNullOrEmpty(directoryPath) && Directory.Exists(directoryPath))
{
ConvertFiles(directoryPath);
ConvertFiles(RemoveDashesFromEndPath(directoryPath));

Console.WriteLine("Converting completed.");
}
else
{
Console.WriteLine("Directory not found.");
Console.WriteLine($"Directory '{directoryPath}' not found.");
}
}

Expand All @@ -28,11 +28,11 @@ private static void ConvertFiles(string directoryPath)

foreach (var filePath in audioFiles)
{
ConvertBitDepth(filePath);
ConvertBitDepth(filePath, directoryPath);
}
}

private static void ConvertBitDepth(string filePath)
private static void ConvertBitDepth(string filePath, string parentDirectoryPath)
{
using (var reader = new WaveFileReader(filePath))
{
Expand All @@ -45,7 +45,7 @@ private static void ConvertBitDepth(string filePath)

try
{
ExecuteSoxUtility(filePath);
ExecuteSoxUtility(filePath, CreateConvertedFilePath(filePath, parentDirectoryPath));
}
catch (Exception e)
{
Expand All @@ -54,10 +54,8 @@ private static void ConvertBitDepth(string filePath)
}
}

private static void ExecuteSoxUtility(string filePath)
private static void ExecuteSoxUtility(string filePath, string convertedFilePath)
{
var convertedFilePath = CreateConvertedFilePath(filePath);

var startInfo = new ProcessStartInfo()
{
RedirectStandardOutput = true,
Expand All @@ -71,20 +69,27 @@ private static void ExecuteSoxUtility(string filePath)
var process = Process.Start(startInfo) ?? throw new NullReferenceException("Process is null");
process.WaitForExit();

Console.WriteLine($">>> {convertedFilePath}");
Console.WriteLine($"> {convertedFilePath}");
}

private static string CreateConvertedFilePath(string filePath)
private static string CreateConvertedFilePath(string filePath, string parentDirectoryPath)
{
var directoryPath = Path.GetDirectoryName(filePath);
var fileName = Path.GetFileName(filePath);
var outputDirectoryPath = directoryPath + "\\converted";
var fileDirectoryPath = Path.GetDirectoryName(filePath) ?? throw new NullReferenceException($"Directory path for '{filePath}' is null");
var outputDirectoryPath = fileDirectoryPath.Replace(parentDirectoryPath, $"{parentDirectoryPath}\\converted");

if (!Directory.Exists(outputDirectoryPath))
{
Directory.CreateDirectory(outputDirectoryPath);
}

var fileName = Path.GetFileName(filePath);
return $"{outputDirectoryPath}\\{fileName}";
}

private static string RemoveDashesFromEndPath(string directoryPath)
{
return directoryPath.EndsWith("\\")
? directoryPath.Remove(directoryPath.Length - 1)
: directoryPath;
}
}

0 comments on commit 93ffb1d

Please sign in to comment.