/* Converters.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ namespace HandBrake.ApplicationServices.Functions { using System; using System.Text.RegularExpressions; using HandBrake.ApplicationServices.Model.Encoding; /// /// A class to convert various things to native C# objects /// public class Converters { /** * TODO: * - Many of these converters can be ditched at a later point. Should be able to model all this within the enums themsevles. * **/ /// /// Convert HandBrakes time remaining into a TimeSpan /// /// /// The time remaining for the encode. /// /// /// A TimepSpan object /// public static TimeSpan EncodeToTimespan(string time) { TimeSpan converted = new TimeSpan(0, 0, 0, 0); Match m = Regex.Match(time.Trim(), @"^([0-9]{2}:[0-9]{2}:[0-9]{2})"); if (m.Success) { TimeSpan.TryParse(m.Groups[0].Value, out converted); } return converted; } #region Audio /// /// Get the GUI equiv to a CLI mixdown /// /// The Audio Mixdown /// The GUI representation of the mixdown public static string GetMixDown(string mixdown) { switch (mixdown.Trim()) { case "mono": return "Mono"; case "stereo": return "Stereo"; case "dpl1": return "Dolby Surround"; case "dpl2": return "Dolby Pro Logic II"; case "6ch": return "6 Channel Discrete"; default: return "Automatic"; } } /// /// Get the GUI equiv to a CLI mixdown /// /// The Audio Mixdown /// The GUI representation of the mixdown public static Mixdown GetAudioMixDown(string mixdown) { switch (mixdown.Trim()) { case "Mono": return Mixdown.Mono; case "Stereo": return Mixdown.Stereo; case "Dolby Surround": return Mixdown.DolbySurround; case "Dolby Pro Logic II": return Mixdown.DolbyProLogicII; case "6 Channel Discrete": return Mixdown.SixChannelDiscrete; default: return Mixdown.Auto; } } /// /// Return the CLI Mixdown name /// /// GUI mixdown name /// CLI mixdown name public static string GetCliMixDown(Mixdown selectedAudio) { switch (selectedAudio) { case Mixdown.Auto: return "auto"; case Mixdown.Mono: return "mono"; case Mixdown.Stereo: return "stereo"; case Mixdown.DolbySurround: return "dpl1"; case Mixdown.DolbyProLogicII: return "dpl2"; case Mixdown.SixChannelDiscrete: return "6ch"; default: return "auto"; } } /// /// Get the GUI equiv to a CLI audio encoder /// /// The Audio Encoder /// The GUI representation of that audio encoder public static string GetGUIAudioEncoder(string audioEnc) { switch (audioEnc) { case "faac": return "AAC (faac)"; case "lame": return "MP3 (lame)"; case "vorbis": return "Vorbis (vorbis)"; case "ac3": return "AC3 (ffmpeg)"; case "copy:ac3": return "AC3 Passthru"; case "copy:dts": return "DTS Passthru"; default: return "AAC (faac)"; } } /// /// Get the GUI equiv to a CLI audio encoder /// /// The Audio Encoder /// The GUI representation of that audio encoder public static AudioEncoder GetAudioEncoderFromCliString(string audioEnc) { switch (audioEnc) { case "faac": return AudioEncoder.Faac; case "lame": return AudioEncoder.Lame; case "vorbis": return AudioEncoder.Vorbis; case "ac3": return AudioEncoder.Ac3; case "copy:ac3": return AudioEncoder.Ac3Passthrough; case "copy:dts": return AudioEncoder.DtsPassthrough; default: return AudioEncoder.Faac; } } /// /// Get the GUI equiv to a GUI audio encoder string /// /// The Audio Encoder /// The GUI representation of that audio encoder public static AudioEncoder GetAudioEncoder(string audioEnc) { switch (audioEnc) { case "AAC (faac)": return AudioEncoder.Faac; case "AAC (CoreAudio)": return AudioEncoder.Faac; case "MP3 (lame)": return AudioEncoder.Lame; case "Vorbis (vorbis)": return AudioEncoder.Vorbis; case "AC3 (ffmpeg)": return AudioEncoder.Ac3; case "AC3 Passthru": return AudioEncoder.Ac3Passthrough; case "DTS Passthru": return AudioEncoder.DtsPassthrough; default: return AudioEncoder.Faac; } } /// /// Get the CLI Audio Encoder name /// /// /// String The GUI Encode name /// /// /// String CLI encoder name /// public static string GetCliAudioEncoder(AudioEncoder selectedEncoder) { switch (selectedEncoder) { case AudioEncoder.Faac: return "faac"; case AudioEncoder.Lame: return "lame"; case AudioEncoder.Vorbis: return "vorbis"; case AudioEncoder.Ac3Passthrough: return "copy:ac3"; case AudioEncoder.DtsPassthrough: return "copy:dts"; case AudioEncoder.Ac3: return "ac3"; default: return "faac"; } } #endregion #region Video /// /// Get the Video Encoder for a given string /// /// /// The encoder name /// /// /// VideoEncoder enum object /// public static VideoEncoder GetVideoEncoder(string encoder) { switch (encoder) { case "ffmpeg": return VideoEncoder.FFMpeg; case "x264": return VideoEncoder.X264; case "theora": return VideoEncoder.Theora; default: return VideoEncoder.X264; } } /// /// Get the Video Encoder for a given string /// /// /// The encoder name /// /// /// VideoEncoder enum object /// public static string GetVideoEncoder(VideoEncoder encoder) { switch (encoder) { case VideoEncoder.FFMpeg: return "ffmpeg"; case VideoEncoder.X264: return "x264"; case VideoEncoder.Theora: return "theora"; default: return "x264"; } } #endregion #region File Format /// /// Get the OutputFormat Enum for a given string /// /// /// OutputFormat as a string /// /// /// An OutputFormat Enum /// public static OutputFormat GetFileFormat(string format) { switch (format.ToLower()) { default: return OutputFormat.Mp4; case "m4v": return OutputFormat.M4V; case "mkv": return OutputFormat.Mkv; } } /// /// Get the OutputFormat Enum for a given string /// /// /// OutputFormat as a string /// /// /// An OutputFormat Enum /// public static string GetFileFormat(OutputFormat format) { switch (format) { default: return "mp4"; case OutputFormat.M4V: return "m4v"; case OutputFormat.Mkv: return "mkv"; } } #endregion } }