mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
CUETools.Converter: support lossy formats, encoder modes, ctrl-c
This commit is contained in:
@@ -6,91 +6,176 @@ using CUETools.Processor.Settings;
|
|||||||
|
|
||||||
namespace CUETools.Converter
|
namespace CUETools.Converter
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Usage()
|
static void Usage()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Usage : CUETools.Converter.exe <infile> <outfile>");
|
Console.Error.WriteLine("Usage : CUETools.Converter.exe [options] <infile> <outfile>");
|
||||||
Console.WriteLine();
|
Console.Error.WriteLine();
|
||||||
//Console.WriteLine("-N --stdinname <t> pseudo filename to use when input from STDIN.");
|
Console.Error.WriteLine("Options:");
|
||||||
//Console.WriteLine(" --stdout write processed WAV output to STDOUT.");
|
Console.Error.WriteLine();
|
||||||
}
|
Console.Error.WriteLine(" --lossy Use lossy encoder.");
|
||||||
|
Console.Error.WriteLine(" --lossless Use lossless encoder (default).");
|
||||||
|
Console.Error.WriteLine(" -p # Padding bytes.");
|
||||||
|
Console.Error.WriteLine(" -m <mode> Encoder mode (0..8 for flac, V0..V9 for mp3, etc)");
|
||||||
|
Console.Error.WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
static int Main(string[] args)
|
||||||
{
|
{
|
||||||
TextWriter stdout = Console.Out;
|
bool ok = true;
|
||||||
Console.SetOut(Console.Error);
|
string sourceFile = null, destFile = null;
|
||||||
Console.WriteLine("CUETools.Converter, Copyright (C) 2009 Grigory Chudov.");
|
int padding = 8192;
|
||||||
Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
|
string encoderMode = null;
|
||||||
Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");
|
AudioEncoderType audioEncoderType = AudioEncoderType.NoAudio;
|
||||||
if (args.Length < 2)
|
for (int arg = 0; arg < args.Length; arg++)
|
||||||
{
|
{
|
||||||
Usage();
|
if (args[arg].Length == 0)
|
||||||
return;
|
ok = false;
|
||||||
}
|
else if ((args[arg] == "-p" || args[arg] == "--padding") && ++arg < args.Length)
|
||||||
string sourceFile = args[0];
|
ok = int.TryParse(args[arg], out padding);
|
||||||
string destFile = args[1];
|
else if ((args[arg] == "-m" || args[arg] == "--mode") && ++arg < args.Length)
|
||||||
|
encoderMode = args[arg];
|
||||||
|
else if (args[arg] == "--lossy")
|
||||||
|
audioEncoderType = AudioEncoderType.Lossy;
|
||||||
|
else if (args[arg] == "--lossless")
|
||||||
|
audioEncoderType = AudioEncoderType.Lossless;
|
||||||
|
else if (args[arg][0] != '-' && sourceFile == null)
|
||||||
|
sourceFile = args[arg];
|
||||||
|
else if (args[arg][0] != '-' && sourceFile != null && destFile == null)
|
||||||
|
destFile = args[arg];
|
||||||
|
else
|
||||||
|
ok = false;
|
||||||
|
if (!ok)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
DateTime start = DateTime.Now;
|
Console.Error.WriteLine("CUETools.Converter, Copyright (C) 2009-13 Grigory Chudov.");
|
||||||
TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);
|
Console.Error.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
|
||||||
CUEConfig config = new CUEConfig();
|
Console.Error.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");
|
||||||
|
if (!ok || sourceFile == null || destFile == null)
|
||||||
|
{
|
||||||
|
Usage();
|
||||||
|
return 22;
|
||||||
|
}
|
||||||
|
|
||||||
SettingsReader sr = new SettingsReader("CUE Tools", "settings.txt", null);
|
if (File.Exists(destFile))
|
||||||
config.Load(sr);
|
{
|
||||||
|
Console.Error.WriteLine("Error: file already exists.");
|
||||||
|
return 17;
|
||||||
|
}
|
||||||
|
|
||||||
|
string extension = Path.GetExtension(destFile).ToLower();
|
||||||
|
DateTime start = DateTime.Now;
|
||||||
|
TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);
|
||||||
|
CUEConfig config = new CUEConfig();
|
||||||
|
|
||||||
|
SettingsReader sr = new SettingsReader("CUE Tools", "settings.txt", null);
|
||||||
|
config.Load(sr);
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
try
|
try
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
IAudioSource audioSource = AudioReadWrite.GetAudioSource(sourceFile, null, config);
|
IAudioSource audioSource = null;
|
||||||
IAudioDest audioDest = AudioReadWrite.GetAudioDest(AudioEncoderType.Lossless, destFile, (long)audioSource.Length, 8192, audioSource.PCM, config);
|
IAudioDest audioDest = null;
|
||||||
AudioBuffer buff = new AudioBuffer(audioSource, 0x10000);
|
try
|
||||||
|
{
|
||||||
|
audioSource = AudioReadWrite.GetAudioSource(sourceFile, null, config);
|
||||||
|
AudioBuffer buff = new AudioBuffer(audioSource, 0x10000);
|
||||||
|
Console.Error.WriteLine("Filename : {0}", sourceFile);
|
||||||
|
Console.Error.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.PCM.SampleRate, audioSource.PCM.ChannelCount, audioSource.PCM.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.PCM.SampleRate));
|
||||||
|
|
||||||
Console.WriteLine("Filename : {0}", sourceFile);
|
CUEToolsFormat fmt;
|
||||||
Console.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.PCM.SampleRate, audioSource.PCM.ChannelCount, audioSource.PCM.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.PCM.SampleRate));
|
if (!extension.StartsWith(".") || !config.formats.TryGetValue(extension.Substring(1), out fmt))
|
||||||
|
throw new Exception("Unsupported audio type: " + destFile);
|
||||||
|
CUEToolsUDC encoder = audioEncoderType == AudioEncoderType.Lossless ? fmt.encoderLossless :
|
||||||
|
audioEncoderType == AudioEncoderType.Lossy ? fmt.encoderLossy :
|
||||||
|
fmt.encoderLossless != null ? fmt.encoderLossless : fmt.encoderLossy;
|
||||||
|
if (encoder == null)
|
||||||
|
throw new Exception("Encoder available for format " + extension + ": " + (fmt.encoderLossless != null ? fmt.encoderLossless.Name + " (lossless)" : fmt.encoderLossy != null ? fmt.encoderLossy.Name + " (lossy)" : "none"));
|
||||||
|
var settings = encoder.settings.Clone();
|
||||||
|
settings.PCM = audioSource.PCM;
|
||||||
|
settings.Padding = padding;
|
||||||
|
settings.EncoderMode = encoderMode ?? settings.EncoderMode;
|
||||||
|
settings.Validate();
|
||||||
|
object o = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
o = Activator.CreateInstance(encoder.type, destFile, settings);
|
||||||
|
}
|
||||||
|
catch (System.Reflection.TargetInvocationException ex)
|
||||||
|
{
|
||||||
|
throw ex.InnerException;
|
||||||
|
}
|
||||||
|
if (o == null || !(o is IAudioDest))
|
||||||
|
throw new Exception("Unsupported audio type: " + destFile + ": " + encoder.type.FullName);
|
||||||
|
audioDest = o as IAudioDest;
|
||||||
|
audioDest.FinalSampleCount = audioSource.Length;
|
||||||
|
|
||||||
while (audioSource.Read(buff, -1) != 0)
|
bool keepRunning = true;
|
||||||
{
|
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
|
||||||
audioDest.Write(buff);
|
{
|
||||||
TimeSpan elapsed = DateTime.Now - start;
|
keepRunning = false;
|
||||||
if ((elapsed - lastPrint).TotalMilliseconds > 60)
|
if (e.SpecialKey == ConsoleSpecialKey.ControlC)
|
||||||
{
|
e.Cancel = true;
|
||||||
Console.Error.Write("\rProgress : {0:00}%; {1:0.00}x; {2}/{3}",
|
else
|
||||||
100.0 * audioSource.Position / audioSource.Length,
|
audioDest.Delete();
|
||||||
audioSource.Position / elapsed.TotalSeconds / audioSource.PCM.SampleRate,
|
};
|
||||||
elapsed,
|
|
||||||
TimeSpan.FromMilliseconds(elapsed.TotalMilliseconds / audioSource.Position * audioSource.Length)
|
|
||||||
);
|
|
||||||
lastPrint = elapsed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TimeSpan totalElapsed = DateTime.Now - start;
|
while (audioSource.Read(buff, -1) != 0)
|
||||||
Console.Error.Write("\r \r");
|
{
|
||||||
Console.WriteLine("Results : {0:0.00}x; {1}",
|
audioDest.Write(buff);
|
||||||
audioSource.Position / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate,
|
TimeSpan elapsed = DateTime.Now - start;
|
||||||
totalElapsed
|
if ((elapsed - lastPrint).TotalMilliseconds > 60)
|
||||||
);
|
{
|
||||||
audioSource.Close();
|
Console.Error.Write("\rProgress : {0:00}%; {1:0.00}x; {2}/{3}",
|
||||||
audioDest.Close();
|
100.0 * audioSource.Position / audioSource.Length,
|
||||||
|
audioSource.Position / elapsed.TotalSeconds / audioSource.PCM.SampleRate,
|
||||||
|
elapsed,
|
||||||
|
TimeSpan.FromMilliseconds(elapsed.TotalMilliseconds / audioSource.Position * audioSource.Length)
|
||||||
|
);
|
||||||
|
lastPrint = elapsed;
|
||||||
|
}
|
||||||
|
if (!keepRunning)
|
||||||
|
throw new Exception("Aborted");
|
||||||
|
}
|
||||||
|
|
||||||
TagLib.UserDefined.AdditionalFileTypes.Config = config;
|
TimeSpan totalElapsed = DateTime.Now - start;
|
||||||
TagLib.File sourceInfo = TagLib.File.Create(new TagLib.File.LocalFileAbstraction(sourceFile));
|
Console.Error.Write("\r \r");
|
||||||
TagLib.File destInfo = TagLib.File.Create(new TagLib.File.LocalFileAbstraction(destFile));
|
Console.Error.WriteLine("Results : {0:0.00}x; {1}",
|
||||||
if (Tagging.UpdateTags(destInfo, Tagging.Analyze(sourceInfo), config))
|
audioSource.Position / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate,
|
||||||
{
|
totalElapsed
|
||||||
sourceInfo.Tag.CopyTo(destInfo.Tag, true);
|
);
|
||||||
destInfo.Tag.Pictures = sourceInfo.Tag.Pictures;
|
}
|
||||||
destInfo.Save();
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
}
|
if (audioSource != null) audioSource.Close();
|
||||||
|
if (audioDest != null) audioDest.Delete();
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
audioSource.Close();
|
||||||
|
audioDest.Close();
|
||||||
|
|
||||||
|
TagLib.UserDefined.AdditionalFileTypes.Config = config;
|
||||||
|
TagLib.File sourceInfo = TagLib.File.Create(new TagLib.File.LocalFileAbstraction(sourceFile));
|
||||||
|
TagLib.File destInfo = TagLib.File.Create(new TagLib.File.LocalFileAbstraction(destFile));
|
||||||
|
if (Tagging.UpdateTags(destInfo, Tagging.Analyze(sourceInfo), config))
|
||||||
|
{
|
||||||
|
sourceInfo.Tag.CopyTo(destInfo.Tag, true);
|
||||||
|
destInfo.Tag.Pictures = sourceInfo.Tag.Pictures;
|
||||||
|
destInfo.Save();
|
||||||
|
}
|
||||||
|
}
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine();
|
Console.Error.Write("\r \r");
|
||||||
Console.WriteLine("Error: {0}", ex.Message);
|
Console.Error.WriteLine("Error : {0}", ex.Message);
|
||||||
//Console.WriteLine("{0}", ex.StackTrace);
|
return 1;
|
||||||
}
|
//Console.WriteLine("{0}", ex.StackTrace);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user