diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 36e4188..b83db69 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -19,6 +19,7 @@ namespace FileConverter using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading; + using System.Windows; public partial class Application : System.Windows.Application { @@ -94,8 +95,8 @@ namespace FileConverter { this.debugMode = true; System.Array.Resize(ref args, 9); - args[1] = "--output-type"; - args[2] = "Flac"; + args[1] = "--conversion-preset"; + args[2] = "To Mp3"; args[3] = @"D:\Projects\FileConverter\TestFiles\Herbie Hancock - Speak Like A Child [RVG Edition].flac"; args[3] = @"D:\Projects\FileConverter\TestFiles\01 - Le Bruit Du Bang.wma"; args[4] = @"D:\Projects\FileConverter\TestFiles\test\Toccata.wav"; @@ -105,8 +106,8 @@ namespace FileConverter args[7] = @"D:\Projects\FileConverter\TestFiles\test\Toccata - Copie (2).wav"; args[8] = @"D:\Projects\FileConverter\TestFiles\test\Toccata - Copie (5).wav"; - System.Array.Resize(ref args, 2); - args[1] = "--settings"; + //System.Array.Resize(ref args, 2); + //args[1] = "--settings"; } #endif @@ -118,8 +119,10 @@ namespace FileConverter Diagnostics.Log(string.Empty); - OutputType outputType = OutputType.None; + ConversionPreset conversionPreset = null; List filePaths = new List(); + + // Parse arguments. for (int index = 1; index < args.Length; index++) { string argument = args[index]; @@ -139,42 +142,25 @@ namespace FileConverter Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); return; - case "output-type": + case "conversion-preset": + if (index >= args.Length - 1) { - if (index >= args.Length - 1) - { - Diagnostics.Log("ERROR ! Invalid format."); - return; - } - - string outputTypeName = args[index + 1].ToLowerInvariant(); - switch (outputTypeName) - { - case "mp3": - outputType = OutputType.Mp3; - break; - - case "ogg": - outputType = OutputType.Ogg; - break; - - case "wav": - outputType = OutputType.Wav; - break; - - case "flac": - outputType = OutputType.Flac; - break; - - default: - Diagnostics.Log("ERROR ! Unknown output type {0}.", outputType); - return; - } - - index++; - continue; + MessageBox.Show(string.Format("ERROR ! Invalid format."), "Error", MessageBoxButton.OK, MessageBoxImage.Error); + Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + return; } + conversionPreset = Settings.GetPresetFromName(args[index + 1]); + if (conversionPreset == null) + { + MessageBox.Show(string.Format("Invalid conversion preset '{0}'.", args[index + 1]), "Error", MessageBoxButton.OK, MessageBoxImage.Error); + Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + return; + } + + index++; + continue; + case "verbose": { this.Verbose = true; @@ -193,24 +179,17 @@ namespace FileConverter } } - if (outputType == OutputType.None) + if (conversionPreset == null) { - Diagnostics.Log("ERROR ! Can't retrieve the output type from arguments."); + Diagnostics.Log("ERROR ! Can't retrieve the conversion preset from arguments."); return; } // Create convertion jobs. for (int index = 0; index < filePaths.Count; index++) { - ConversionJob conversionJob = new ConversionJob(); - string inputFilePath = filePaths[index]; - string extension = System.IO.Path.GetExtension(inputFilePath); - string outputFilePath = inputFilePath.Substring(0, inputFilePath.Length - extension.Length) + "." - + outputType.ToString().ToLowerInvariant(); - - conversionJob.Initialize(outputType, inputFilePath, outputFilePath); - + ConversionJob conversionJob = new ConversionJob(conversionPreset, inputFilePath); this.conversionJobs.Add(conversionJob); } diff --git a/Application/FileConverter/ConversionJob.cs b/Application/FileConverter/ConversionJob.cs index dab3b9e..0e575f7 100644 --- a/Application/FileConverter/ConversionJob.cs +++ b/Application/FileConverter/ConversionJob.cs @@ -27,7 +27,7 @@ namespace FileConverter public ConversionJob() { - this.OutputType = OutputType.None; + this.ConversionPreset = null; this.progress = 0f; this.InputFilePath = string.Empty; this.OutputFilePath = string.Empty; @@ -35,6 +35,17 @@ namespace FileConverter this.ExitingMessage = string.Empty; } + public ConversionJob(ConversionPreset conversionPreset, string inputFilePath) + { + this.ConversionPreset = conversionPreset; + this.InputFilePath = inputFilePath; + + string extension = System.IO.Path.GetExtension(inputFilePath); + this.OutputFilePath = inputFilePath.Substring(0, inputFilePath.Length - extension.Length) + "." + this.ConversionPreset.OutputType.ToString().ToLowerInvariant(); + + this.Initialize(); + } + public event PropertyChangedEventHandler PropertyChanged; public enum ConversionState @@ -47,6 +58,12 @@ namespace FileConverter Failed, } + public ConversionPreset ConversionPreset + { + get; + private set; + } + public string InputFilePath { get; @@ -58,13 +75,7 @@ namespace FileConverter get; private set; } - - public OutputType OutputType - { - get; - private set; - } - + public ConversionState State { get @@ -107,67 +118,33 @@ namespace FileConverter } } - public void Initialize(OutputType outputType, string inputFileName, string outputFileName) - { - if (outputType == OutputType.None) - { - throw new ArgumentException("The output type must be valid.", "outputType"); - } - - this.OutputType = outputType; - this.ffmpegProcessStartInfo = null; - - string applicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); - string ffmpegPath = string.Format("{0}\\ffmpeg.exe", applicationDirectory); - if (!System.IO.File.Exists(ffmpegPath)) - { - Diagnostics.Log("Can't find ffmpeg executable ({0}). Try to reinstall the application.", ffmpegPath); - return; - } - - this.ffmpegProcessStartInfo = new ProcessStartInfo(ffmpegPath); - - this.ffmpegProcessStartInfo.CreateNoWindow = true; - this.ffmpegProcessStartInfo.UseShellExecute = false; - this.ffmpegProcessStartInfo.RedirectStandardOutput = true; - this.ffmpegProcessStartInfo.RedirectStandardError = true; - - this.InputFilePath = inputFileName; - this.OutputFilePath = outputFileName; - - this.State = ConversionState.Ready; - } - public void StartConvertion() { - if (this.OutputType == OutputType.None) + if (this.ConversionPreset == null) { - throw new Exception("The converter is not initialized."); + throw new Exception("The conversion preset must be valid."); } Diagnostics.Log("Convert file {0} to {1}.", this.InputFilePath, this.OutputFilePath); string arguments = string.Empty; - switch (this.OutputType) + switch (this.ConversionPreset.OutputType) { case OutputType.Mp3: - arguments = string.Format("-n -stats -i \"{0}\" -qscale:a 5 \"{1}\"", this.InputFilePath, this.OutputFilePath); + arguments = string.Format("-n -stats -i \"{0}\" -qscale:a 2 \"{1}\"", this.InputFilePath, this.OutputFilePath); break; case OutputType.Ogg: - arguments = string.Format("-n -stats -i \"{0}\" -acodec libvorbis -qscale:a 5 \"{1}\"", this.InputFilePath, this.OutputFilePath); + arguments = string.Format("-n -stats -i \"{0}\" -acodec libvorbis -qscale:a 2 \"{1}\"", this.InputFilePath, this.OutputFilePath); break; case OutputType.Flac: - arguments = string.Format("-n -stats -i \"{0}\" \"{1}\"", this.InputFilePath, this.OutputFilePath); - break; - case OutputType.Wav: arguments = string.Format("-n -stats -i \"{0}\" \"{1}\"", this.InputFilePath, this.OutputFilePath); break; default: - throw new NotImplementedException("Converter not implemented for output file type " + this.OutputType); + throw new NotImplementedException("Converter not implemented for output file type " + this.ConversionPreset.OutputType); } if (string.IsNullOrEmpty(arguments) || this.ffmpegProcessStartInfo == null) @@ -217,6 +194,33 @@ namespace FileConverter Diagnostics.Log("\nDone!"); } + private void Initialize() + { + if (this.ConversionPreset == null) + { + throw new Exception("The conversion preset must be valid."); + } + + this.ffmpegProcessStartInfo = null; + + string applicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + string ffmpegPath = string.Format("{0}\\ffmpeg.exe", applicationDirectory); + if (!System.IO.File.Exists(ffmpegPath)) + { + Diagnostics.Log("Can't find ffmpeg executable ({0}). Try to reinstall the application.", ffmpegPath); + return; + } + + this.ffmpegProcessStartInfo = new ProcessStartInfo(ffmpegPath); + + this.ffmpegProcessStartInfo.CreateNoWindow = true; + this.ffmpegProcessStartInfo.UseShellExecute = false; + this.ffmpegProcessStartInfo.RedirectStandardOutput = true; + this.ffmpegProcessStartInfo.RedirectStandardError = true; + + this.State = ConversionState.Ready; + } + private void ParseFFMPEGOutput(string input) { Match match = this.durationRegex.Match(input); diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index fca6d8d..4d2de17 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -11,6 +11,7 @@ namespace FileConverter using System.Security.Principal; using System.Windows; using System.ComponentModel; + using System.Linq; using System.Runtime.CompilerServices; using Microsoft.Win32; @@ -46,6 +47,11 @@ namespace FileConverter handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } + public ConversionPreset GetPresetFromName(string presetName) + { + return this.conversionPresets.FirstOrDefault(match => match.Name == presetName); + } + public void Load() { ICollection presets = this.ConversionPresets;