Beispiel #1
0
        static void Main(string[] args)
        {
            TextWriter stdout = Console.Out;

            Console.SetOut(Console.Error);
            Console.WriteLine("CUETools.ChaptersToCue v2.1.7 Copyright (C) 2017 Grigory Chudov");
            Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
            Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");

            bool   queryMeta  = false;
            bool   celltimes  = false;
            int    fps_mul    = 0;
            int    fps_div    = 1;
            string inputPath  = "-";
            string outputPath = null;
            string tracksPath = null;
            string imagePath  = null;

            for (int arg = 0; arg < args.Length; arg++)
            {
                bool ok = true;
                if ((args[arg] == "-i" || args[arg] == "--input") && ++arg < args.Length)
                {
                    inputPath = args[arg];
                }
                else if ((args[arg] == "-o" || args[arg] == "--output") && ++arg < args.Length)
                {
                    outputPath = args[arg];
                }
                else if ((args[arg] == "-t" || args[arg] == "--tracks") && ++arg < args.Length)
                {
                    tracksPath = args[arg];
                }
                else if (args[arg] == "--image" && ++arg < args.Length)
                {
                    imagePath = args[arg];
                }
                else if (args[arg] == "-m" || args[arg] == "--meta")
                {
                    queryMeta = true;
                }
                else if (args[arg] == "--celltimes" && ++arg < args.Length)
                {
                    celltimes = true;
                    ok        = int.TryParse(args[arg], out fps_mul);
                    if (ok && fps_mul == 30)
                    {
                        fps_mul = 30000;
                        fps_div = 1001;
                    }
                }
                else
                {
                    ok = false;
                }
                if (!ok)
                {
                    Usage();
                    return;
                }
            }

            string strtoc    = "";
            string extension = null;

            if (tracksPath != null)
            {
                //CUEToolsCodecsConfig config = new CUEConfig();
                //TagLib.UserDefined.AdditionalFileTypes.Config = config;
                TimeSpan pos = new TimeSpan(0);
                using (TextReader sr = tracksPath == "-" ? Console.In : new StreamReader(tracksPath))
                {
                    while (sr.Peek() >= 0)
                    {
                        string line = sr.ReadLine();
                        extension = Path.GetExtension(line);
                        TagLib.File sourceInfo = TagLib.File.Create(new TagLib.File.LocalFileAbstraction(line));
                        strtoc += string.Format(" {0}", (int)(pos.TotalSeconds * 75));
                        pos    += sourceInfo.Properties.Duration;
                    }
                }
                strtoc += string.Format(" {0}", (int)(pos.TotalSeconds * 75));
            }
            else
            {
                using (TextReader sr = inputPath == "-" ? Console.In : new StreamReader(inputPath))
                {
                    if (celltimes)
                    {
                        strtoc += string.Format(" {0}", 0);
                        while (sr.Peek() >= 0)
                        {
                            string line = sr.ReadLine();
                            strtoc += string.Format(" {0}", long.Parse(line) * 75 * fps_div / fps_mul);
                        }
                    }
                    else
                    {
                        while (sr.Peek() >= 0)
                        {
                            string line = sr.ReadLine();
                            Regex  r    = new Regex(@"^CHAPTER(?<number>\d\d)(?<option>[^=]*)=((?<hour>\d+):(?<minute>\d+):(?<second>\d+)\.(?<millisecond>\d+)|(?<text>))");
                            Match  m    = r.Match(line);
                            if (!m.Success)
                            {
                                Console.Error.WriteLine("Invalid input format: {0}", line);
                                return;
                            }
                            var option = m.Result("${option}");
                            if (option != "")
                            {
                                continue;
                            }
                            var chapter     = int.Parse(m.Result("${number}"));
                            var hour        = int.Parse(m.Result("${hour}"));
                            var minute      = int.Parse(m.Result("${minute}"));
                            var second      = int.Parse(m.Result("${second}"));
                            var millisecond = int.Parse(m.Result("${millisecond}"));
                            strtoc += string.Format(" {0}", ((hour * 60 + minute) * 60 + second) * 75 + millisecond * 75 / 1000);
                        }
                        if (imagePath != null)
                        {
                            TagLib.File sourceInfo = TagLib.File.Create(new TagLib.File.LocalFileAbstraction(imagePath));
                            strtoc += string.Format(" {0}", (int)(sourceInfo.Properties.Duration.TotalSeconds * 75));
                        }
                        else
                        {
                            strtoc += string.Format(" {0}", (int)(75 * 60 * 60 * 2));
                            //strtoc += string.Format(" {0}", (int)(75 * 259570688.0 / 96000));
                        }
                    }
                }
            }
            strtoc = strtoc.Substring(1);
            CDImageLayout    toc  = new CDImageLayout(strtoc);
            CTDBResponseMeta meta = null;

            if (queryMeta)
            {
                var ctdb = new CUEToolsDB(toc, null);
                ctdb.ContactDB(null, "CUETools.ChaptersToCue 2.1.7", "", false, true, CTDBMetadataSearch.Extensive);
                foreach (var imeta in ctdb.Metadata)
                {
                    meta = imeta;
                    break;
                }
            }

            if (outputPath == null)
            {
                if (meta != null)
                {
                    outputPath = string.Format("{0} - {1} - {2}.cue", meta.artist ?? "Unknown Artist", meta.year ?? "XXXX", meta.album ?? "Unknown Album");
                }
                else
                {
                    outputPath = "unknown.cue";
                }
            }

            StringWriter cueWriter = new StringWriter();

            cueWriter.WriteLine("REM COMMENT \"{0}\"", "Created by ChaptersToCue");
            if (meta != null && meta.year != null)
            {
                cueWriter.WriteLine("REM DATE {0}", meta.year);
            }
            else
            {
                cueWriter.WriteLine("REM DATE XXXX");
            }
            if (meta != null)
            {
                cueWriter.WriteLine("PERFORMER \"{0}\"", meta.artist);
                cueWriter.WriteLine("TITLE \"{0}\"", meta.album);
            }
            else
            {
                cueWriter.WriteLine("PERFORMER \"\"");
                cueWriter.WriteLine("TITLE \"\"");
            }
            if (meta != null)
            {
                cueWriter.WriteLine("FILE \"{0}\" WAVE", Path.GetFileNameWithoutExtension(outputPath) + (extension ?? ".wav"));
            }
            else
            {
                cueWriter.WriteLine("FILE \"{0}\" WAVE", "");
            }
            for (int track = 1; track <= toc.TrackCount; track++)
            {
                if (toc[track].IsAudio)
                {
                    cueWriter.WriteLine("  TRACK {0:00} AUDIO", toc[track].Number);
                    if (meta != null && meta.track.Length >= toc[track].Number)
                    {
                        cueWriter.WriteLine("    TITLE \"{0}\"", meta.track[(int)toc[track].Number - 1].name);
                        if (meta.track[(int)toc[track].Number - 1].artist != null)
                        {
                            cueWriter.WriteLine("    PERFORMER \"{0}\"", meta.track[(int)toc[track].Number - 1].artist);
                        }
                    }
                    else
                    {
                        cueWriter.WriteLine("    TITLE \"\"");
                    }
                    if (toc[track].ISRC != null)
                    {
                        cueWriter.WriteLine("    ISRC {0}", toc[track].ISRC);
                    }
                    for (int index = toc[track].Pregap > 0 ? 0 : 1; index <= toc[track].LastIndex; index++)
                    {
                        cueWriter.WriteLine("    INDEX {0:00} {1}", index, toc[track][index].MSF);
                    }
                }
            }
            cueWriter.Close();
            if (outputPath == "-")
            {
                stdout.Write(cueWriter.ToString());
            }
            else
            {
                try
                {
                    using (var ofs = new FileStream(outputPath, FileMode.CreateNew, FileAccess.Write))
                        using (var cueFile = new StreamWriter(ofs))
                        {
                            cueFile.Write(cueWriter.ToString());
                            cueFile.Close();
                        }
                }
                catch (System.IO.IOException ex)
                {
                    Console.Error.WriteLine("{0}", ex.Message);
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.SetOut(Console.Error);
            Console.WriteLine("CUERipper v2.1.7 Copyright (C) 2008-10 Grigory Chudov");
            Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
            Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");

            int    correctionQuality = 1;
            string driveLetter       = null;
            int    driveOffset       = 0;
            bool   test = false;
            bool   forceD8 = false, forceBE = false, quiet = false;

            for (int arg = 0; arg < args.Length; arg++)
            {
                bool ok = true;
                if (args[arg] == "-P" || args[arg] == "--paranoid")
                {
                    correctionQuality = 2;
                }
                else if (args[arg] == "-S" || args[arg] == "--secure")
                {
                    correctionQuality = 1;
                }
                else if (args[arg] == "-B" || args[arg] == "--burst")
                {
                    correctionQuality = 0;
                }
                else if (args[arg] == "-T" || args[arg] == "--test")
                {
                    test = true;
                }
                else if (args[arg] == "--d8")
                {
                    forceD8 = true;
                }
                else if (args[arg] == "--be")
                {
                    forceBE = true;
                }
                else if (args[arg] == "-Q" || args[arg] == "--quiet")
                {
                    quiet = true;
                }
                else if ((args[arg] == "-D" || args[arg] == "--drive") && ++arg < args.Length)
                {
                    driveLetter = args[arg];
                }
                else if ((args[arg] == "-O" || args[arg] == "--offset") && ++arg < args.Length)
                {
                    ok = int.TryParse(args[arg], out driveOffset);
                }
                else
                {
                    ok = false;
                }
                if (!ok)
                {
                    Usage();
                    return;
                }
            }

            char[] drives;
            if (driveLetter == null || driveLetter.Length < 1)
            {
                drives = CDDrivesList.DrivesAvailable();
                if (drives.Length < 1)
                {
                    Console.WriteLine("No CD drives found.");
                    return;
                }
            }
            else
            {
                drives    = new char[1];
                drives[0] = driveLetter[0];
            }

#if !DEBUG
            try
#endif
            {
                CDDriveReader audioSource = new CDDriveReader();
                audioSource.Open(drives[0]);

                if (audioSource.TOC.AudioTracks < 1)
                {
                    Console.WriteLine("{0}: CD does not contain any audio tracks.", audioSource.Path);
                    audioSource.Close();
                    return;
                }
                if (driveOffset == 0)
                {
                    if (!AccurateRipVerify.FindDriveReadOffset(audioSource.ARName, out driveOffset))
                    {
                        Console.WriteLine("Unknown read offset for drive {0}!!!", audioSource.Path);
                    }
                }
                //throw new Exception("Failed to find drive read offset for drive" + audioSource.ARName);

                audioSource.DriveOffset       = driveOffset;
                audioSource.CorrectionQuality = correctionQuality;
                audioSource.DebugMessages     = !quiet;
                if (forceD8)
                {
                    audioSource.ForceD8 = true;
                }
                if (forceBE)
                {
                    audioSource.ForceBE = true;
                }
                string readCmd = audioSource.AutoDetectReadCommand;
                if (test)
                {
                    Console.Write(readCmd);
                    return;
                }

                AccurateRipVerify arVerify = new AccurateRipVerify(audioSource.TOC, WebRequest.GetSystemWebProxy());
                AudioBuffer       buff     = new AudioBuffer(audioSource, 0x10000);
                string            CDDBId   = AccurateRipVerify.CalculateCDDBId(audioSource.TOC);
                string            ArId     = AccurateRipVerify.CalculateAccurateRipId(audioSource.TOC);
                var ctdb = new CUEToolsDB(audioSource.TOC, null);
                ctdb.Init(arVerify);
                ctdb.ContactDB(null, "CUETools.ConsoleRipper 2.1.7", audioSource.ARName, true, false, CTDBMetadataSearch.Fast);
                arVerify.ContactAccurateRip(ArId);
                CTDBResponseMeta meta = null;
                foreach (var imeta in ctdb.Metadata)
                {
                    meta = imeta;
                    break;
                }

                //string destFile = (release == null) ? "cdimage.flac" : release.GetArtist() + " - " + release.GetTitle() + ".flac";
                string destFile = (meta == null) ? "cdimage.wav" : meta.artist + " - " + meta.album + ".wav";

                Console.WriteLine("Drive       : {0}", audioSource.Path);
                Console.WriteLine("Read offset : {0}", audioSource.DriveOffset);
                Console.WriteLine("Read cmd    : {0}", audioSource.CurrentReadCommand);
                Console.WriteLine("Secure mode : {0}", audioSource.CorrectionQuality);
                Console.WriteLine("Filename    : {0}", destFile);
                Console.WriteLine("Disk length : {0}", CDImageLayout.TimeToString(audioSource.TOC.AudioLength));
                Console.WriteLine("AccurateRip : {0}", arVerify.ARStatus == null ? "ok" : arVerify.ARStatus);
                Console.WriteLine("MusicBrainz : {0}", meta == null ? "not found" : meta.artist + " - " + meta.album);

                ProgressMeter meter = new ProgressMeter();
                audioSource.ReadProgress += new EventHandler <ReadProgressArgs>(meter.ReadProgress);

                audioSource.DetectGaps();

                StringWriter cueWriter = new StringWriter();
                cueWriter.WriteLine("REM DISCID {0}", CDDBId);
                cueWriter.WriteLine("REM ACCURATERIPID {0}", ArId);
                cueWriter.WriteLine("REM COMMENT \"{0}\"", audioSource.RipperVersion);
                if (meta != null && meta.year != "")
                {
                    cueWriter.WriteLine("REM DATE {0}", meta.year);
                }
                if (audioSource.TOC.Barcode != null)
                {
                    cueWriter.WriteLine("CATALOG {0}", audioSource.TOC.Barcode);
                }
                if (meta != null)
                {
                    cueWriter.WriteLine("PERFORMER \"{0}\"", meta.artist);
                    cueWriter.WriteLine("TITLE \"{0}\"", meta.album);
                }
                cueWriter.WriteLine("FILE \"{0}\" WAVE", destFile);
                for (int track = 1; track <= audioSource.TOC.TrackCount; track++)
                {
                    if (audioSource.TOC[track].IsAudio)
                    {
                        cueWriter.WriteLine("  TRACK {0:00} AUDIO", audioSource.TOC[track].Number);
                        if (meta != null && meta.track.Length >= audioSource.TOC[track].Number)
                        {
                            cueWriter.WriteLine("    TITLE \"{0}\"", meta.track[(int)audioSource.TOC[track].Number - 1].name);
                            cueWriter.WriteLine("    PERFORMER \"{0}\"", meta.track[(int)audioSource.TOC[track].Number - 1].artist);
                        }
                        if (audioSource.TOC[track].ISRC != null)
                        {
                            cueWriter.WriteLine("    ISRC {0}", audioSource.TOC[track].ISRC);
                        }
                        if (audioSource.TOC[track].DCP || audioSource.TOC[track].PreEmphasis)
                        {
                            cueWriter.WriteLine("    FLAGS{0}{1}", audioSource.TOC[track].PreEmphasis ? " PRE" : "", audioSource.TOC[track].DCP ? " DCP" : "");
                        }
                        for (int index = audioSource.TOC[track].Pregap > 0 ? 0 : 1; index <= audioSource.TOC[track].LastIndex; index++)
                        {
                            cueWriter.WriteLine("    INDEX {0:00} {1}", index, audioSource.TOC[track][index].MSF);
                        }
                    }
                }
                cueWriter.Close();
                StreamWriter cueFile = new StreamWriter(Path.ChangeExtension(destFile, ".cue"));
                cueFile.Write(cueWriter.ToString());
                cueFile.Close();

                //IAudioDest audioDest = new FLACWriter(destFile, audioSource.BitsPerSample, audioSource.ChannelCount, audioSource.SampleRate);
                IAudioDest audioDest = new Codecs.WAV.AudioEncoder(new Codecs.WAV.EncoderSettings(audioSource.PCM), destFile);
                audioDest.FinalSampleCount = audioSource.Length;
                while (audioSource.Read(buff, -1) != 0)
                {
                    arVerify.Write(buff);
                    audioDest.Write(buff);
                }

                TimeSpan totalElapsed = DateTime.Now - meter.realStart;
                Console.Write("\r                                                                             \r");
                Console.WriteLine("Results     : {0:0.00}x; {1:d5} errors; {2:d2}:{3:d2}:{4:d2}",
                                  audioSource.Length / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate,
                                  audioSource.FailedSectors.PopulationCount(),
                                  totalElapsed.Hours, totalElapsed.Minutes, totalElapsed.Seconds
                                  );
                audioDest.Close();

                StringWriter logWriter = new StringWriter();
                logWriter.WriteLine("{0}", audioSource.RipperVersion);
                logWriter.WriteLine("Extraction logfile from {0}", DateTime.Now);
                logWriter.WriteLine("Used drive  : {0}", audioSource.Path);
                logWriter.WriteLine("Read offset correction                      : {0}", audioSource.DriveOffset);
                bool wereErrors = false;
                for (int iTrack = 1; iTrack <= audioSource.TOC.AudioTracks; iTrack++)
                {
                    for (uint iSector = audioSource.TOC[iTrack].Start; iSector <= audioSource.TOC[iTrack].End; iSector++)
                    {
                        if (audioSource.FailedSectors[(int)iSector])
                        {
                            if (!wereErrors)
                            {
                                logWriter.WriteLine();
                                logWriter.WriteLine("Errors detected");
                                logWriter.WriteLine();
                            }
                            wereErrors = true;
                            logWriter.WriteLine("Track {0} contains errors", iTrack);
                            break;
                        }
                    }
                }
                logWriter.WriteLine();
                logWriter.WriteLine("TOC of the extracted CD");
                logWriter.WriteLine();
                logWriter.WriteLine("     Track |   Start  |  Length  | Start sector | End sector");
                logWriter.WriteLine("    ---------------------------------------------------------");
                for (int track = 1; track <= audioSource.TOC.TrackCount; track++)
                {
                    logWriter.WriteLine("{0,9}  | {1,8} | {2,8} | {3,9}    | {4,9}",
                                        audioSource.TOC[track].Number,
                                        audioSource.TOC[track].StartMSF,
                                        audioSource.TOC[track].LengthMSF,
                                        audioSource.TOC[track].Start,
                                        audioSource.TOC[track].End);
                }
                logWriter.WriteLine();
                logWriter.WriteLine("AccurateRip summary");
                logWriter.WriteLine();
                arVerify.GenerateFullLog(logWriter, true, ArId);
                logWriter.WriteLine();
                logWriter.WriteLine("End of status report");
                logWriter.Close();
                StreamWriter logFile = new StreamWriter(Path.ChangeExtension(destFile, ".log"));
                logFile.Write(logWriter.ToString());
                logFile.Close();

                audioSource.Close();

                //FLACReader tagger = new FLACReader(destFile, null);
                //tagger.Tags.Add("CUESHEET", cueWriter.ToString());
                //tagger.Tags.Add("LOG", logWriter.ToString());
                //tagger.UpdateTags(false);
            }
#if !DEBUG
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("Error: {0}", ex.Message);
                Console.WriteLine("{0}", ex.StackTrace);
            }
#endif
        }
Beispiel #3
0
        static int Main(string[] args)
        {
            bool             ok = true;
            string           sourceFile = null, destFile = null;
            int              padding          = 8192;
            int              stream           = 0;
            string           encoderMode      = null;
            string           encoderName      = null;
            string           encoderFormat    = null;
            AudioEncoderType audioEncoderType = AudioEncoderType.NoAudio;
            var              decoderOptions   = new Dictionary <string, string>();
            bool             queryMeta        = false;

            for (int arg = 0; arg < args.Length; arg++)
            {
                if (args[arg].Length == 0)
                {
                    ok = false;
                }
                else if (args[arg] == "--encoder" && ++arg < args.Length)
                {
                    encoderName = args[arg];
                }
                else if (args[arg] == "--encoder-format" && ++arg < args.Length)
                {
                    encoderFormat = args[arg];
                }
                else if ((args[arg] == "-p" || args[arg] == "--padding") && ++arg < args.Length)
                {
                    ok = int.TryParse(args[arg], out padding);
                }
                else if ((args[arg] == "-m" || args[arg] == "--mode") && arg + 1 < 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] == "--ctdb")
                {
                    queryMeta = true;
                }
                else if (args[arg] == "--decoder-option" && arg + 2 < args.Length)
                {
                    var optionName  = args[++arg];
                    var optionValue = args[++arg];
                    decoderOptions.Add(optionName, optionValue);
                }
                else if ((args[arg][0] != '-' || args[arg] == "-") && sourceFile == null)
                {
                    sourceFile = args[arg];
                }
                else if ((args[arg][0] != '-' || args[arg] == "-") && sourceFile != null && destFile == null)
                {
                    destFile = args[arg];
                    var x = destFile.Split(':');
                    if (x.Length > 1)
                    {
                        stream = int.Parse(x[0]);
                        if (x[1] != "")
                        {
                            destFile = x[1];
                        }
                        else
                        {
                            arg++;
                            if (arg >= args.Length)
                            {
                                ok = false;
                                break;
                            }
                            destFile = args[arg];
                        }
                    }
                }
                else
                {
                    ok = false;
                }
                if (!ok)
                {
                    break;
                }
            }

            if (!ok || sourceFile == null)
            {
                Usage();
                return(22);
            }

            if (destFile != null && destFile != "-" && destFile != "nul" && File.Exists(destFile))
            {
                Console.Error.WriteLine("Error: file {0} already exists.", destFile);
                return(17);
            }

            DateTime start     = DateTime.Now;
            TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);
            var      config    = new CUEConfigAdvanced();

            config.Init();

#if !DEBUG
            try
#endif
            {
                IAudioSource       audioSource    = null;
                IAudioTitleSet     audioContainer = null;
                IAudioDest         audioDest      = null;
                var                videos         = new List <Codecs.MPEG.MPLS.MPLSStream>();
                List <IAudioTitle> audios         = null;
                List <TimeSpan>    chapters       = null;
                TagLib.UserDefined.AdditionalFileTypes.Config = config;

#if !DEBUG
                try
#endif
                {
                    if (true)
                    {
                        IAudioDecoderSettings decoderSettings = null;
                        if (Path.GetExtension(sourceFile) == ".mpls")
                        {
                            decoderSettings = new Codecs.MPEG.MPLS.DecoderSettings();
                        }
                        else
                        {
                            decoderSettings = new Codecs.MPEG.ATSI.DecoderSettings();
                        }
                        foreach (var decOpt in decoderOptions)
                        {
                            var property = TypeDescriptor.GetProperties(decoderSettings).Find(decOpt.Key, true);
                            if (property == null)
                            {
                                throw new Exception($"{decoderSettings.Name} {decoderSettings.Extension} decoder settings object (of type {decoderSettings.GetType().FullName}) doesn't have a property named {decOpt.Key}.");
                            }
                            property.SetValue(decoderSettings,
                                              TypeDescriptor.GetConverter(property.PropertyType).ConvertFromString(decOpt.Value));
                        }
                        audioSource    = decoderSettings.Open(sourceFile);
                        audioContainer = audioSource as IAudioTitleSet;
                        if (audioContainer == null)
                        {
                            audioContainer = new SingleAudioTitleSet(audioSource);
                        }
                        Console.ForegroundColor = ConsoleColor.White;
                        int  frameRate  = 0;
                        bool interlaced = false;
                        audios = audioContainer.AudioTitles;
                        audios.ForEach(t => chapters = t.Chapters);
                        if (audioSource is Codecs.MPEG.MPLS.AudioDecoder)
                        {
                            var mpls = audioSource as Codecs.MPEG.MPLS.AudioDecoder;
                            mpls.MPLSHeader.play_item.ForEach(i => i.video.ForEach(v => { if (!videos.Exists(v1 => v1.pid == v.pid))
                                                                                          {
                                                                                              videos.Add(v);
                                                                                          }
                                                                                   }));
                        }
                        videos.ForEach(v => { frameRate = v.FrameRate; interlaced = v.Interlaced; });
                        Console.Error.Write($@"M2TS, {
                            videos.Count} video track{(videos.Count != 1 ? "s" : "")}, {
                            audios.Count} audio track{(audios.Count != 1 ? "s" : "")}, {
                            CDImageLayout.TimeToString(audios[0].GetDuration(), "{0:0}:{1:00}:{2:00}")}, {
                            (frameRate * (interlaced ? 2 : 1))}{
                            (interlaced ? "i" : "p")}");
                        Console.Error.WriteLine();
                        //foreach (var item in mpls.MPLSHeader.play_item)
                        //Console.Error.WriteLine("{0}.m2ts", item.clip_id);
                        {
                            Console.ForegroundColor = ConsoleColor.Gray;
                            int id = 1;
                            if (chapters.Count > 1)
                            {
                                Console.ForegroundColor = ConsoleColor.White;
                                Console.Error.Write(id++);
                                Console.Error.Write(": ");
                                Console.ForegroundColor = ConsoleColor.Gray;
                                Console.Error.WriteLine("Chapters, {0} chapters", chapters.Count - 1);
                            }
                            foreach (var video in videos)
                            {
                                Console.ForegroundColor = ConsoleColor.White;
                                Console.Error.Write(id++);
                                Console.Error.Write(": ");
                                Console.ForegroundColor = ConsoleColor.Gray;
                                Console.Error.WriteLine("{0}, {1}{2}", video.CodecString, video.FormatString, video.FrameRate * (video.Interlaced ? 2 : 1));
                            }
                            foreach (var audio in audios)
                            {
                                Console.ForegroundColor = ConsoleColor.White;
                                Console.Error.Write(id++);
                                Console.Error.Write(": ");
                                Console.ForegroundColor = ConsoleColor.Gray;
                                Console.Error.WriteLine("{0}, {1}, {2}, {3}", audio.Codec, audio.Language, audio.GetFormatString(), audio.GetRateString());
                            }
                        }
                    }

                    if (destFile == null)
                    {
                        return(0);
                    }

                    string strtoc = "";
                    for (int i = 0; i < chapters.Count; i++)
                    {
                        strtoc += string.Format(" {0}", (int)Math.Round((chapters[i].TotalSeconds * 75)));
                    }
                    strtoc = strtoc.Substring(1);
                    CDImageLayout    toc  = new CDImageLayout(strtoc);
                    CTDBResponseMeta meta = null;
                    if (queryMeta)
                    {
                        var ctdb = new CUEToolsDB(toc, null);
                        Console.Error.WriteLine("Contacting CTDB...");
                        ctdb.ContactDB(null, "CUETools.eac3to 2.1.8", "", false, true, CTDBMetadataSearch.Extensive);
                        foreach (var imeta in ctdb.Metadata)
                        {
                            meta = imeta;
                            break;
                        }
                    }

                    if (stream > 0)
                    {
                        int chapterStreams = chapters.Count > 1 ? 1 : 0;
                        if (stream <= chapterStreams)
                        {
                            if (destFile == "-" || destFile == "nul")
                            {
                                encoderFormat = "txt";
                            }
                            else
                            {
                                string extension = Path.GetExtension(destFile).ToLower();
                                if (!extension.StartsWith("."))
                                {
                                    encoderFormat = destFile;
                                    if (meta == null || meta.artist == null || meta.album == null)
                                    {
                                        destFile = string.Format("{0}.{1}", Path.GetFileNameWithoutExtension(sourceFile), destFile);
                                    }
                                    else
                                    {
                                        destFile = string.Format("{0} - {1} - {2}.{3}", meta.artist, meta.year, meta.album, destFile);
                                    }
                                }
                                else
                                {
                                    encoderFormat = extension.Substring(1);
                                }
                                if (encoderFormat != "txt" && encoderFormat != "cue")
                                {
                                    throw new Exception(string.Format("Unsupported chapters file format \"{0}\"", encoderFormat));
                                }
                            }

                            Console.Error.WriteLine("Creating file \"{0}\"...", destFile);

                            if (encoderFormat == "txt")
                            {
                                using (TextWriter sw = destFile == "nul" ? (TextWriter) new StringWriter() : destFile == "-" ? Console.Out : (TextWriter) new StreamWriter(destFile))
                                {
                                    for (int i = 0; i < chapters.Count - 1; i++)
                                    {
                                        sw.WriteLine("CHAPTER{0:00}={1}", i + 1,
                                                     CDImageLayout.TimeToString(chapters[i]));
                                        if (meta != null && meta.track.Length >= toc[i + 1].Number)
                                        {
                                            sw.WriteLine("CHAPTER{0:00}NAME={1}", i + 1, meta.track[(int)toc[i + 1].Number - 1].name);
                                        }
                                        else
                                        {
                                            sw.WriteLine("CHAPTER{0:00}NAME=", i + 1);
                                        }
                                    }
                                }
                                Console.BackgroundColor = ConsoleColor.DarkGreen;
                                Console.Error.Write("Done.");
                                Console.BackgroundColor = ConsoleColor.Black;
                                Console.Error.WriteLine();
                                return(0);
                            }

                            if (encoderFormat == "cue")
                            {
                                using (StreamWriter cueWriter = new StreamWriter(destFile, false, Encoding.UTF8))
                                {
                                    cueWriter.WriteLine("REM COMMENT \"{0}\"", "Created by CUETools.eac3to");
                                    if (meta != null && meta.year != null)
                                    {
                                        cueWriter.WriteLine("REM DATE {0}", meta.year);
                                    }
                                    else
                                    {
                                        cueWriter.WriteLine("REM DATE XXXX");
                                    }
                                    if (meta != null)
                                    {
                                        cueWriter.WriteLine("PERFORMER \"{0}\"", meta.artist);
                                        cueWriter.WriteLine("TITLE \"{0}\"", meta.album);
                                    }
                                    else
                                    {
                                        cueWriter.WriteLine("PERFORMER \"\"");
                                        cueWriter.WriteLine("TITLE \"\"");
                                    }
                                    if (meta != null)
                                    {
                                        //cueWriter.WriteLine("FILE \"{0}\" WAVE", Path.GetFileNameWithoutExtension(destFile) + (extension ?? ".wav"));
                                        cueWriter.WriteLine("FILE \"{0}\" WAVE", Path.GetFileNameWithoutExtension(destFile) + (".wav"));
                                    }
                                    else
                                    {
                                        cueWriter.WriteLine("FILE \"{0}\" WAVE", "");
                                    }
                                    for (int track = 1; track <= toc.TrackCount; track++)
                                    {
                                        if (toc[track].IsAudio)
                                        {
                                            cueWriter.WriteLine("  TRACK {0:00} AUDIO", toc[track].Number);
                                            if (meta != null && meta.track.Length >= toc[track].Number)
                                            {
                                                cueWriter.WriteLine("    TITLE \"{0}\"", meta.track[(int)toc[track].Number - 1].name);
                                                if (meta.track[(int)toc[track].Number - 1].artist != null)
                                                {
                                                    cueWriter.WriteLine("    PERFORMER \"{0}\"", meta.track[(int)toc[track].Number - 1].artist);
                                                }
                                            }
                                            else
                                            {
                                                cueWriter.WriteLine("    TITLE \"\"");
                                            }
                                            if (toc[track].ISRC != null)
                                            {
                                                cueWriter.WriteLine("    ISRC {0}", toc[track].ISRC);
                                            }
                                            for (int index = toc[track].Pregap > 0 ? 0 : 1; index <= toc[track].LastIndex; index++)
                                            {
                                                cueWriter.WriteLine("    INDEX {0:00} {1}", index, toc[track][index].MSF);
                                            }
                                        }
                                    }
                                }
                                Console.BackgroundColor = ConsoleColor.DarkGreen;
                                Console.Error.Write("Done.");
                                Console.BackgroundColor = ConsoleColor.Black;
                                Console.Error.WriteLine();
                                return(0);
                            }

                            throw new Exception("Unknown encoder format: " + destFile);
                        }
                        if (stream - chapterStreams <= videos.Count)
                        {
                            throw new Exception("Video extraction not supported.");
                        }
                        if (stream - chapterStreams - videos.Count > audios.Count)
                        {
                            throw new Exception(string.Format("The source file doesn't contain a track with the number {0}.", stream));
                        }
                        int streamId = audios[stream - chapterStreams - videos.Count - 1].StreamId;
                        if (audioSource is Codecs.MPEG.MPLS.AudioDecoder)
                        {
                            (audioSource.Settings as Codecs.MPEG.MPLS.DecoderSettings).StreamId = streamId;
                        }
                    }

                    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,
                                            audioSource.Duration);

                    CUEToolsFormat fmt;
                    if (encoderFormat == null)
                    {
                        if (destFile == "-" || destFile == "nul")
                        {
                            encoderFormat = "wav";
                        }
                        else
                        {
                            string extension = Path.GetExtension(destFile).ToLower();
                            if (!extension.StartsWith("."))
                            {
                                encoderFormat = destFile;
                                if (meta == null || meta.artist == null || meta.album == null)
                                {
                                    destFile = string.Format("{0} - {1}.{2}", Path.GetFileNameWithoutExtension(sourceFile), stream, destFile);
                                }
                                else
                                {
                                    destFile = string.Format("{0} - {1} - {2}.{3}", meta.artist, meta.year, meta.album, destFile);
                                }
                            }
                            else
                            {
                                encoderFormat = extension.Substring(1);
                            }
                            if (File.Exists(destFile))
                            {
                                throw new Exception(string.Format("Error: file {0} already exists.", destFile));
                            }
                        }
                    }
                    if (!config.formats.TryGetValue(encoderFormat, out fmt))
                    {
                        throw new Exception("Unsupported encoder format: " + encoderFormat);
                    }
                    AudioEncoderSettingsViewModel encoder =
                        audioEncoderType == AudioEncoderType.Lossless ? Program.GetEncoder(config, fmt, true, encoderName) :
                        audioEncoderType == AudioEncoderType.Lossy ? Program.GetEncoder(config, fmt, false, encoderName) :
                        Program.GetEncoder(config, fmt, true, encoderName) ?? Program.GetEncoder(config, fmt, false, encoderName);
                    if (encoder == null)
                    {
                        var lst = new List <AudioEncoderSettingsViewModel>(config.encodersViewModel).FindAll(
                            e => e.Extension == fmt.extension && (audioEncoderType == AudioEncoderType.NoAudio || audioEncoderType == (e.Lossless ? AudioEncoderType.Lossless : AudioEncoderType.Lossy))).
                                  ConvertAll(e => e.Name + (e.Lossless ? " (lossless)" : " (lossy)"));
                        throw new Exception("Encoders available for format " + fmt.extension + ": " + (lst.Count == 0 ? "none" : string.Join(", ", lst.ToArray())));
                    }
                    Console.Error.WriteLine("Output {0}  : {1}", stream, destFile);
                    var settings = encoder.Settings.Clone();
                    settings.PCM         = audioSource.PCM;
                    settings.Padding     = padding;
                    settings.EncoderMode = encoderMode ?? settings.EncoderMode;
                    object o = null;
                    try
                    {
                        o = destFile == "-" ? Activator.CreateInstance(settings.EncoderType, settings, "", Console.OpenStandardOutput()) :
                            destFile == "nul" ? Activator.CreateInstance(settings.EncoderType, settings, "", new NullStream()) :
                            Activator.CreateInstance(settings.EncoderType, settings, destFile, null);
                    }
                    catch (System.Reflection.TargetInvocationException ex)
                    {
                        throw ex.InnerException;
                    }
                    if (o == null || !(o is IAudioDest))
                    {
                        throw new Exception("Unsupported audio type: " + destFile + ": " + settings.EncoderType.FullName);
                    }
                    audioDest = o as IAudioDest;
                    audioDest.FinalSampleCount = audioSource.Length;

                    bool keepRunning = true;
                    Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e)
                    {
                        keepRunning = false;
                        if (e.SpecialKey == ConsoleSpecialKey.ControlC)
                        {
                            e.Cancel = true;
                        }
                        else
                        {
                            audioDest.Delete();
                        }
                    };

                    while (audioSource.Read(buff, -1) != 0)
                    {
                        audioDest.Write(buff);
                        TimeSpan elapsed = DateTime.Now - start;
                        if ((elapsed - lastPrint).TotalMilliseconds > 60)
                        {
                            var duration = audioSource.Duration;
                            var position = TimeSpan.FromSeconds((double)audioSource.Position / audioSource.PCM.SampleRate);
                            if (duration < position)
                            {
                                duration = position;
                            }
                            if (duration < TimeSpan.FromSeconds(1))
                            {
                                duration = TimeSpan.FromSeconds(1);
                            }
                            Console.Error.Write("\rProgress  : {0:00}%; {1:0.00}x; {2}/{3}",
                                                100.0 * position.TotalSeconds / duration.TotalSeconds,
                                                position.TotalSeconds / elapsed.TotalSeconds,
                                                elapsed,
                                                TimeSpan.FromSeconds(elapsed.TotalSeconds / position.TotalSeconds * duration.TotalSeconds)
                                                );
                            lastPrint = elapsed;
                        }
                        if (!keepRunning)
                        {
                            throw new Exception("Aborted");
                        }
                    }

                    TimeSpan totalElapsed = DateTime.Now - start;
                    Console.Error.Write("\r                                                                         \r");
                    Console.Error.WriteLine("Results   : {0:0.00}x; {1}",
                                            audioSource.Position / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate,
                                            totalElapsed
                                            );
                }
#if !DEBUG
                catch (Exception ex)
                {
                    if (audioSource != null)
                    {
                        audioSource.Close();
                    }
                    if (audioDest != null)
                    {
                        audioDest.Delete();
                    }
                    throw ex;
                }
#endif
                audioSource.Close();
                audioDest.Close();

                if (sourceFile != "-" && destFile != "-" && destFile != "nul")
                {
                    //TagLib.File destInfo = TagLib.File.Create(new TagLib.File.LocalFileAbstraction(destFile));
                    //NameValueCollection tags;
                    //if (Tagging.UpdateTags(destInfo, tags, config, false))
                    //{
                    //    destInfo.Save();
                    //}
                }
            }
#if !DEBUG
            catch (Exception ex)
            {
                Console.Error.Write("\r                                                                         \r");
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.Error.Write("Error     : {0}", ex.Message);
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Error.WriteLine();
                return(1);
                //Console.WriteLine("{0}", ex.StackTrace);
            }
#endif
            return(0);
        }