/// <summary>
        /// Takes in a query which can be in any order and parses it. 
        /// All varibles are then set so they can be used elsewhere.
        /// </summary>
        /// <param name="input">A ClI Query</param>
        /// <returns>A Parsed Query</returns>
        public static QueryParser Parse(String input)
        {
            QueryParser thisQuery = new QueryParser();

            #region Regular Expressions
            // Useful Destination Finder
            //Regex r1 = new Regex(@"(-i)(?:\s\"")([a-zA-Z0-9?';!^%&*()_\-:\\\s\.]+)(?:\"")");
            //Match source = r1.Match(input.Replace('"', '\"'));

            //Source
            Match title = Regex.Match(input, @"-t ([0-9]*)");
            Match chapters = Regex.Match(input, @"-c ([0-9-]*)");
            Match format = Regex.Match(input, @"-f ([a-z0-9a-z0-9a-z0-9]*)");

            //Destination
            Match videoEncoder = Regex.Match(input, @"-e ([a-zA-Z0-9]*)");

            //Picture Settings Tab
            Match width = Regex.Match(input, @"-w ([0-9]*)");
            Match height = Regex.Match(input, @"-l ([0-9]*)");
            Match maxWidth = Regex.Match(input, @"-X ([0-9]*)");
            Match maxHeight = Regex.Match(input, @"-Y ([0-9]*)");
            Match deinterlace = Regex.Match(input, @"--deinterlace=\""([a-zA-Z]*)\""");
            Match denoise = Regex.Match(input, @"--denoise=\""([a-zA-Z]*)\""");
            Match deblock = Regex.Match(input, @"--deblock=([0-9]*)");
            Match detelecine = Regex.Match(input, @"--detelecine");
            Match anamorphic = Regex.Match(input, @" -p ");
            Match chapterMarkers = Regex.Match(input, @" -m");
            Match chapterMarkersFileMode = Regex.Match(input, @"--markers");
            Match crop = Regex.Match(input, @"--crop ([0-9]*):([0-9]*):([0-9]*):([0-9]*)");
            Match lanamorphic = Regex.Match(input, @" -P");
            Match decomb = Regex.Match(input, @" --decomb");

            //Video Settings Tab
            Match videoFramerate = Regex.Match(input, @"-r ([0-9]*)");
            Match videoBitrate = Regex.Match(input, @"-b ([0-9]*)");
            Match videoQuality = Regex.Match(input, @"-q ([0-9.]*)");
            Match videoFilesize = Regex.Match(input, @"-S ([0-9.]*)");
            Match twoPass = Regex.Match(input, @" -2");
            Match turboFirstPass = Regex.Match(input, @" -T");
            Match grayscale = Regex.Match(input, @" -g");
            Match largerMp4 = Regex.Match(input, @" -4");
            Match ipodAtom = Regex.Match(input, @" -I");
            Match optimizeMP4 = Regex.Match(input, @" -O");

            //Audio Settings Tab
            Match audioTrack1 = Regex.Match(input, @"-a ([0-9]*)");
            Match audioTrack2 = Regex.Match(input, @"-a ([0-9]*),([0-9]*)");
            Match audioTrack3 = Regex.Match(input, @"-a ([0-9]*),([0-9]*),([0-9]*)");
            Match audioTrack4 = Regex.Match(input, @"-a ([0-9]*),([0-9]*),([0-9]*),([0-9]*)");

            Match audioTrack1Mix = Regex.Match(input, @"-6 ([0-9a-z0-9]*)");  
            Match audioTrack2Mix = Regex.Match(input, @"-6 ([0-9a-z0-9]*),([0-9a-z0-9]*)");
            Match audioTrack3Mix = Regex.Match(input, @"-6 ([0-9a-z0-9]*),([0-9a-z0-9]*),([0-9a-z0-9]*)");
            Match audioTrack4Mix = Regex.Match(input, @"-6 ([0-9a-z0-9]*),([0-9a-z0-9]*),([0-9a-z0-9]*),([0-9a-z0-9]*)"); 

            Match audioEncoder1 = Regex.Match(input, @"-E ([a-zA-Z0-9+]*)");
            Match audioEncoder2 = Regex.Match(input, @"-E ([a-zA-Z0-9+]*),([a-zA-Z0-9+]*)");
            Match audioEncoder3 = Regex.Match(input, @"-E ([a-zA-Z0-9+]*),([a-zA-Z0-9+]*),([a-zA-Z0-9+]*)");
            Match audioEncoder4 = Regex.Match(input, @"-E ([a-zA-Z0-9+]*),([a-zA-Z0-9+]*),([a-zA-Z0-9+]*),([a-zA-Z0-9+]*)");

            Match audioBitrate1 = Regex.Match(input, @"-B ([0-9auto]*)");
            Match audioBitrate2 = Regex.Match(input, @"-B ([0-9auto]*),([0-9auto]*)");
            Match audioBitrate3 = Regex.Match(input, @"-B ([0-9auto]*),([0-9auto]*),([0-9auto]*)");
            Match audioBitrate4 = Regex.Match(input, @"-B ([0-9auto]*),([0-9auto]*),([0-9auto]*),([0-9auto]*)");

            Match audioSampleRate1 = Regex.Match(input, @"-R ([0-9Auto.]*)");
            Match audioSampleRate2 = Regex.Match(input, @"-R ([0-9Auto.]*),([0-9Auto.]*)");
            Match audioSampleRate3 = Regex.Match(input, @"-R ([0-9Auto.]*),([0-9Auto.]*),([0-9Auto.]*)");
            Match audioSampleRate4 = Regex.Match(input, @"-R ([0-9Auto.]*),([0-9Auto.]*),([0-9Auto.]*),([0-9Auto.]*)");

            Match drc1 = Regex.Match(input, @"-D ([0-9.]*)");
            Match drc2 = Regex.Match(input, @"-D ([0-9.]*),([0-9.]*)");
            Match drc3 = Regex.Match(input, @"-D ([0-9.]*),([0-9.]*),([0-9.]*)");
            Match drc4 = Regex.Match(input, @"-D ([0-9.]*),([0-9.]*),([0-9.]*),([0-9.]*)");

            Match subtitles = Regex.Match(input, @"-s ([0-9a-zA-Z]*)");
            Match subScan = Regex.Match(input, @" -U");
            Match forcedSubtitles = Regex.Match(input, @" -F");

            //H264 Tab
            Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");

            //Program Options
            Match verbose = Regex.Match(input, @" -v");
            #endregion

            #region Set Varibles
            try
            {
                #region Source Tab
                if (title.Success != false)
 	                    thisQuery.q_dvdTitle = int.Parse(title.ToString().Replace("-t ", ""));

                if (chapters.Success != false)
                {
                    string[] actTitles = new string[2];
                    actTitles = chapters.ToString().Replace("-c ", "").Split('-');
                    thisQuery.q_chaptersStart = int.Parse(actTitles[0]);
                    if (actTitles.Length > 1)
                    {
                        thisQuery.q_chaptersFinish = int.Parse(actTitles[1]);
                    }

                    if ((thisQuery.q_chaptersStart == 1) && (thisQuery.q_chaptersFinish == 0))
                        thisQuery.q_chaptersFinish = thisQuery.q_chaptersStart;
                }

                if (format.Success != false)
                    thisQuery.q_format = format.ToString().Replace("-f ", "");

                #endregion

                #region Destination

                string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", "");
                switch (videoEncoderConvertion)
                {
                    case "ffmpeg":
                        videoEncoderConvertion = "MPEG-4 (FFmpeg)";
                        break;
                    case "xvid":
                        videoEncoderConvertion = "MPEG-4 (XviD)";
                        break;
                    case "x264":
                        videoEncoderConvertion = "H.264 (x264)";
                        break;
                    case "theora":
                        videoEncoderConvertion = "VP3 (Theora)";
                        break;
                    default:
                        videoEncoderConvertion = "MPEG-4 (FFmpeg)";
                        break;
                }
                thisQuery.q_videoEncoder = videoEncoderConvertion;

                #endregion

                #region Picture Tab

                if (width.Success != false)
                    thisQuery.q_videoWidth = int.Parse(width.ToString().Replace("-w ", ""));

                if (height.Success != false)
                    thisQuery.q_videoHeight = int.Parse(height.ToString().Replace("-l ", ""));

                if (maxWidth.Success != false)
                    thisQuery.q_maxWidth = int.Parse(maxWidth.ToString().Replace("-X ", ""));

                if (maxHeight.Success != false)
                    thisQuery.q_maxHeight = int.Parse(maxHeight.ToString().Replace("-Y ", ""));

                if (crop.Success != false)
                {
                    thisQuery.q_cropValues = crop.ToString().Replace("--crop ", "");
                    string[] actCropValues = new string[3];
                    actCropValues = thisQuery.q_cropValues.Split(':');
                    thisQuery.q_croptop = actCropValues[0];
                    thisQuery.q_cropbottom = actCropValues[1];
                    thisQuery.q_cropLeft = actCropValues[2];
                    thisQuery.q_cropRight = actCropValues[3];
                }

                // Deblock Slider
                string deblockValue = "";
                thisQuery.q_deBlock = 0;
                if (deblock.Success != false)
                    deblockValue = deblock.ToString().Replace("--deblock=", "");

                if (deblockValue != "")
                    int.TryParse(deblockValue, out thisQuery.q_deBlock);

                thisQuery.q_detelecine = detelecine.Success;
                thisQuery.q_decomb = decomb.Success;

                thisQuery.q_deinterlace = "None";
                if (deinterlace.Success != false)
                {
                    switch (deinterlace.ToString().Replace("--deinterlace=", "").Replace("\"",""))
                    {
                        case "fast":
                            thisQuery.q_deinterlace = "Fast";
                            break;
                        case "slow":
                            thisQuery.q_deinterlace = "Slow";
                            break;
                        case "slower":
                            thisQuery.q_deinterlace = "Slower";
                            break;
                        case "slowest":
                            thisQuery.q_deinterlace = "Slowest";
                            break;
                        default:
                            thisQuery.q_deinterlace = "None";
                            break;
                    }
                }

                thisQuery.q_denoise = "None";
                if (denoise.Success != false)
                {
                    switch (denoise.ToString().Replace("--denoise=", "").Replace("\"", ""))
                    {
                        case "weak":
                            thisQuery.q_denoise = "Weak";
                            break;
                        case "medium":
                            thisQuery.q_denoise = "Medium";
                            break;
                        case "strong":
                            thisQuery.q_denoise = "Strong";
                            break;
                        default:
                            thisQuery.q_denoise = "None";
                            break;
                    }

                }
                thisQuery.q_anamorphic = anamorphic.Success;
                if (chapterMarkersFileMode.Success == true || chapterMarkers.Success == true)
                    thisQuery.q_chapterMarkers = true;
                
                thisQuery.q_looseAnamorphic = lanamorphic.Success;

                #endregion
     
                #region Video Settings Tab
                thisQuery.q_grayscale = grayscale.Success;
                thisQuery.q_twoPass = twoPass.Success;
                thisQuery.q_turboFirst = turboFirstPass.Success;
                thisQuery.q_largeMp4 = largerMp4.Success;
                if (videoFramerate.Success != false)
                    thisQuery.q_videoFramerate = videoFramerate.ToString().Replace("-r ", "");
                else
                    thisQuery.q_videoFramerate = "Same as source";

                if (videoBitrate.Success != false)
                    thisQuery.q_avgBitrate = videoBitrate.ToString().Replace("-b ", "");
                if (videoFilesize.Success != false)
                    thisQuery.q_videoTargetSize = videoFilesize.ToString().Replace("-S ", "");

                double qConvert = 0;
                if (videoQuality.Success != false)
                {
                    qConvert = double.Parse(videoQuality.ToString().Replace("-q ", ""), Culture) * 100;
                    qConvert = System.Math.Ceiling(qConvert);
                    thisQuery.q_videoQuality = int.Parse(qConvert.ToString());
                }
                thisQuery.q_ipodAtom = ipodAtom.Success;
                thisQuery.q_optimizeMp4 = optimizeMP4.Success;

                #endregion

                #region Audio Tab

                // Tracks
                thisQuery.q_audioTrack1 = "Automatic";

                if (audioTrack2.Success != false)
                {
                    string[] audioChan = audioTrack2.ToString().Split(',');
                    thisQuery.q_audioTrack2 = audioChan[1];
                }
                else
                    thisQuery.q_audioTrack2 = "None";

                if (audioTrack3.Success != false)
                {
                    string[] audioChan = audioTrack3.ToString().Split(',');
                    thisQuery.q_audioTrack3 = audioChan[2];
                }
                else
                    thisQuery.q_audioTrack3 = "None";

                if (audioTrack4.Success != false)
                {
                    string[] audioChan = audioTrack4.ToString().Split(',');
                    thisQuery.q_audioTrack4 = audioChan[3];
                }
                else
                    thisQuery.q_audioTrack4 = "None";

    
                // Mixdowns
                thisQuery.q_audioTrackMix1 = "Automatic";
                if (audioTrack1Mix.Success != false)
                    thisQuery.q_audioTrackMix1 = getMixDown(audioTrack1Mix.ToString().Replace("-6 ", "").Replace(" ", ""));

                thisQuery.q_audioTrackMix2 = "Automatic";
                if (audioTrack2Mix.Success != false)
                {
                    string[] audio2mix = audioTrack2Mix.ToString().Split(',');
                    thisQuery.q_audioTrackMix2 = getMixDown(audio2mix[1].Trim());
                }

                thisQuery.q_audioTrackMix3 = "Automatic";
                if (audioTrack3Mix.Success != false)
                {
                    string[] audio3mix = audioTrack3Mix.ToString().Split(',');
                    thisQuery.q_audioTrackMix3 = getMixDown(audio3mix[2].Trim());
                }

                thisQuery.q_audioTrackMix4 = "Automatic";
                if (audioTrack4Mix.Success != false)
                {
                    string[] audio4mix = audioTrack4Mix.ToString().Split(',');
                    thisQuery.q_audioTrackMix4 = getMixDown(audio4mix[3].Trim());
                }
                

                // Audio Encoders
                if (audioEncoder1.Success != false)
                    thisQuery.q_audioEncoder1 = getAudioEncoder(audioEncoder1.ToString().Replace("-E ", ""));

                if (audioEncoder2.Success != false)
                {
                    string[] audio2enc = audioEncoder2.ToString().Split(',');
                    thisQuery.q_audioEncoder2 = getAudioEncoder(audio2enc[1].Trim());
                }

                if (audioEncoder3.Success != false)
                {
                    string[] audio3enc = audioEncoder3.ToString().Split(',');
                    thisQuery.q_audioEncoder3 = getAudioEncoder(audio3enc[2].Trim());
                }

                if (audioEncoder4.Success != false)
                {
                    string[] audio4enc = audioEncoder4.ToString().Split(',');
                    thisQuery.q_audioEncoder4 = getAudioEncoder(audio4enc[3].Trim());
                }


                // Audio Bitrate
                thisQuery.q_audioBitrate1 = "";
                if (audioBitrate1.Success != false)
                {
                    thisQuery.q_audioBitrate1 = audioBitrate1.ToString().Replace("-B ", "").Trim();
                    if (audioBitrate1.ToString().Replace("-B ", "").Trim() == "0") thisQuery.q_audioBitrate1 = "Auto";
                }

                thisQuery.q_audioBitrate2 = "";
                if (audioBitrate2.Success != false && audioTrack2.Success == true)
                {
                    string[] audioBitrateSelect = audioBitrate2.ToString().Split(',');
                    if (audioBitrateSelect[1].Trim() == "0") audioBitrateSelect[1] = "Auto";
                    thisQuery.q_audioBitrate2 = audioBitrateSelect[1].Trim();                      
                }

                thisQuery.q_audioBitrate3 = "";
                if (audioBitrate3.Success != false && audioTrack3.Success == true)
                {
                    string[] audioBitrateSelect = audioBitrate3.ToString().Split(',');
                    if (audioBitrateSelect[2].Trim() == "0") audioBitrateSelect[2] = "Auto";
                    thisQuery.q_audioBitrate3 = audioBitrateSelect[2].Trim();
                }

                thisQuery.q_audioBitrate4 = "";
                if (audioBitrate4.Success != false)
                {
                    string[] audioBitrateSelect = audioBitrate4.ToString().Split(',');
                    if (audioBitrateSelect[3].Trim() == "0") audioBitrateSelect[3] = "Auto";
                    thisQuery.q_audioBitrate4 = audioBitrateSelect[3].Trim();
                }


                // Audio Sample Rate
                // Make sure to change 0 to Auto
                thisQuery.q_audioSamplerate1 = "Auto";
                if (audioSampleRate1.Success != false)
                {
                    thisQuery.q_audioSamplerate1 = audioSampleRate1.ToString().Replace("-R ", "").Trim();
                    if (thisQuery.q_audioSamplerate1 == "0") thisQuery.q_audioSamplerate1 = "Auto";
                }
                    

                if (audioSampleRate2.Success != false)
                {
                    string[] audioSRSelect = audioSampleRate2.ToString().Split(',');
                    if (audioSRSelect[1] == "0") audioSRSelect[1] = "Auto";
                    thisQuery.q_audioSamplerate2 = audioSRSelect[1].Trim();
                }

                if (audioSampleRate3.Success != false)
                {
                    string[] audioSRSelect = audioSampleRate3.ToString().Split(',');
                    if (audioSRSelect[2] == "0") audioSRSelect[2] = "Auto";
                    thisQuery.q_audioSamplerate3 = audioSRSelect[2].Trim();
                }

                if (audioSampleRate4.Success != false)
                {
                    string[] audioSRSelect = audioSampleRate4.ToString().Split(',');
                    if (audioSRSelect[3] == "0") audioSRSelect[3] = "Auto";
                    thisQuery.q_audioSamplerate4 = audioSRSelect[3].Trim();
                }

                // DRC
                float drcValue;

                thisQuery.q_drc1 = 1;
                if (drc1.Success != false)
                {
                    string value = drc1.ToString().Replace("-D ", "");
                    float.TryParse(value, out drcValue);
                    thisQuery.q_drc1 = drcValue;
                }
               
                thisQuery.q_drc2 = 1;   
                if (drc2.Success != false)
                {
                    string[] drcPoint = drc2.ToString().Split(',');
                    float.TryParse(drcPoint[1], out drcValue);
                    thisQuery.q_drc2 = drcValue;
                }

                thisQuery.q_drc3 = 1;
                if (drc3.Success != false)
                {
                    string[] drcPoint = drc3.ToString().Split(',');
                    float.TryParse(drcPoint[2], out drcValue);
                    thisQuery.q_drc3 = drcValue;
                }

                thisQuery.q_drc4 = 1;
                if (drc4.Success != false)
                {
                    string[] drcPoint = drc4.ToString().Split(',');
                    float.TryParse(drcPoint[3], out drcValue);
                    thisQuery.q_drc4 = drcValue;
                }

                // Subtitle Stuff
                if (subtitles.Success != false)
                    thisQuery.q_subtitles = subtitles.ToString().Replace("-s ", "");
                else
                {
                    if (subScan.Success)
                        thisQuery.q_subtitles = "Autoselect";
                    else
                        thisQuery.q_subtitles = "None";
                }

                thisQuery.q_forcedSubs = forcedSubtitles.Success;

                #endregion

                #region H.264 and other
                //
                //H264 Tab
                //
                if (x264.Success != false)
                    thisQuery.q_h264 = x264.ToString().Replace("-x ", "");

                //
                //Progam Options
                //
                thisQuery.q_verbose = verbose.Success;
                #endregion
            }
            catch (Exception exc)
            {
                MessageBox.Show("An error has occured in the Query Parser. Please report this error on the forum in the 'Windows' support section. \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            #endregion

            return thisQuery;
        }
Esempio n. 2
0
        /// <summary>
        /// This function takes in a Query which has been parsed by QueryParser and
        /// set's all the GUI widgets correctly.
        /// </summary>
        /// <param name="mainWindow">
        /// FrmMain window
        /// </param>
        /// <param name="presetQuery">
        /// The Parsed CLI Query
        /// </param>
        /// <param name="name">
        /// Name of the preset
        /// </param>
        public static void LoadPreset(frmMain mainWindow, QueryParser presetQuery, string name)
        {
            #region Source

            // Reset some vaules to stock first to prevent errors.
            mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;

            // Now load all the new settings onto the main window
            if (presetQuery.Format != null)
            {
                string destination = mainWindow.text_destination.Text;
                destination = destination.Replace(".mp4", "." + presetQuery.Format);
                destination = destination.Replace(".m4v", "." + presetQuery.Format);
                destination = destination.Replace(".mkv", "." + presetQuery.Format);
                mainWindow.text_destination.Text = destination;
            }

            #endregion

            #region Destination and Output Settings

            if (presetQuery.Format != null)
            {
                if (presetQuery.Format == "mp4" || presetQuery.Format == "m4v")
                {
                    if (mainWindow.drop_format.SelectedIndex == 0)
                        mainWindow.SetExtension(".mp4");
                    else
                        mainWindow.drop_format.SelectedIndex = 0;
                }
                else if (presetQuery.Format == "mkv")
                {
                    if (mainWindow.drop_format.SelectedIndex == 1)
                        mainWindow.SetExtension(".mkv");
                    else
                        mainWindow.drop_format.SelectedIndex = 1;
                }
            }

            mainWindow.check_iPodAtom.CheckState = presetQuery.IpodAtom ? CheckState.Checked : CheckState.Unchecked;

            mainWindow.check_optimiseMP4.CheckState = presetQuery.OptimizeMP4
                                                          ? CheckState.Checked
                                                          : CheckState.Unchecked;

            mainWindow.check_largeFile.CheckState = presetQuery.LargeMP4 ? CheckState.Checked : CheckState.Unchecked;

            mainWindow.setContainerOpts(); // select the container options according to the selected format

            #endregion

            #region Picture

            mainWindow.PictureSettings.check_autoCrop.Checked = true;
            if (presetQuery.CropValues != null)
            {
                int top, bottom, left, right;
                int.TryParse(presetQuery.CropTop, out top);
                int.TryParse(presetQuery.CropBottom, out bottom);
                int.TryParse(presetQuery.CropLeft, out left);
                int.TryParse(presetQuery.CropRight, out right);

                mainWindow.PictureSettings.check_customCrop.Checked = true;
                mainWindow.PictureSettings.crop_top.Value = top;
                mainWindow.PictureSettings.crop_bottom.Value = bottom;
                mainWindow.PictureSettings.crop_left.Value = left;
                mainWindow.PictureSettings.crop_right.Value = right;
            }

            // Set the anamorphic mode 0,1,2,3
            mainWindow.PictureSettings.drp_anamorphic.SelectedIndex = presetQuery.AnamorphicMode;

            // Keep Aspect Ration Anamorphic Setting.
            mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.KeepDisplayAsect
                                                                     ? CheckState.Checked
                                                                     : CheckState.Unchecked;

            // Set the Width and height as Required.
            if (presetQuery.Width != 0)
                mainWindow.PictureSettings.text_width.Value = presetQuery.Width;

            if (presetQuery.Height != 0)
                mainWindow.PictureSettings.text_height.Value = presetQuery.Height;

            // Max Width/Height override Width/Height
            if (presetQuery.MaxWidth != 0)
                mainWindow.PictureSettings.text_width.Value = presetQuery.MaxWidth;

            if (presetQuery.MaxHeight != 0)
                mainWindow.PictureSettings.text_height.Value = presetQuery.MaxHeight;

            mainWindow.PictureSettings.PresetMaximumResolution = new Size(presetQuery.MaxWidth, presetQuery.MaxHeight);

            // Case where both height and max height are 0 - For built-in presets
            if (presetQuery.MaxHeight == 0 && presetQuery.Height == 0)
                mainWindow.PictureSettings.text_height.Value = 0;

            if (presetQuery.MaxWidth == 0 && presetQuery.Width == 0)
                if (mainWindow.selectedTitle != null && mainWindow.selectedTitle.Resolution.Width != 0)
                    mainWindow.PictureSettings.text_width.Value = mainWindow.selectedTitle.Resolution.Width;

            // Aspect Ratio for non anamorphic sources
            if (presetQuery.AnamorphicMode == 0)
                mainWindow.PictureSettings.check_KeepAR.CheckState = presetQuery.Height == 0
                                                                         ? CheckState.Checked
                                                                         : CheckState.Unchecked;

            // Custom Anamorphic Controls
            mainWindow.PictureSettings.updownDisplayWidth.Text = presetQuery.DisplayWidthValue.ToString();
            mainWindow.PictureSettings.updownParHeight.Text = presetQuery.PixelAspectHeight.ToString();
            mainWindow.PictureSettings.updownParWidth.Text = presetQuery.PixelAspectWidth.ToString();
            mainWindow.PictureSettings.drp_modulus.SelectedItem = presetQuery.AnamorphicModulus.ToString();

            #endregion

            #region Filters

            mainWindow.Filters.SetDecomb(presetQuery.Decomb);
            mainWindow.Filters.SetDeInterlace(presetQuery.DeInterlace);
            mainWindow.Filters.SetDeNoise(presetQuery.DeNoise);
            mainWindow.Filters.SetDeTelecine(presetQuery.DeTelecine);
            mainWindow.Filters.SetDeBlock(presetQuery.DeBlock);
            mainWindow.Filters.SetGrayScale(presetQuery.Grayscale);

            #endregion

            #region Video

            mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;

            if (presetQuery.AverageVideoBitrate != null)
            {
                mainWindow.radio_avgBitrate.Checked = true;
                mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;
            }
            if (presetQuery.VideoTargetSize != null)
            {
                mainWindow.radio_targetFilesize.Checked = true;
                mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;
            }

            // Quality
            if (presetQuery.VideoQuality != -1)
            {
                mainWindow.radio_cq.Checked = true;
                mainWindow.slider_videoQuality.Value = QualityToSliderValue(presetQuery.VideoEncoder, presetQuery.VideoQuality);
            }

            mainWindow.check_2PassEncode.CheckState = presetQuery.TwoPass ? CheckState.Checked : CheckState.Unchecked;

            mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;
            mainWindow.checkMaximumFramerate.Checked = presetQuery.Pfr;

            mainWindow.check_turbo.CheckState = presetQuery.TurboFirstPass ? CheckState.Checked : CheckState.Unchecked;

            #endregion

            #region Chapter Markers

            if (presetQuery.ChapterMarkers)
            {
                mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;
                mainWindow.Check_ChapterMarkers.Enabled = true;
            }
            else
                mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;

            #endregion

            #region Audio

            mainWindow.AudioSettings.LoadTracks(presetQuery.AudioInformation);

            #endregion

            #region Other

            mainWindow.x264Panel.X264Query = presetQuery.H264Query;

            // Set the preset name
            mainWindow.labelPreset.Text = "Output Settings (Preset: " + name + ")";

            #endregion
        }
Esempio n. 3
0
        /// <summary>
        /// Takes in a query which can be in any order and parses it. 
        /// All varibles are then set so they can be used elsewhere.
        /// </summary>
        /// <param name="input">A ClI Query</param>
        /// <returns>A Parsed Query</returns>
        public static QueryParser Parse(string input)
        {
            var thisQuery = new QueryParser();

            #region Regular Expressions

            // Source
            Match title = Regex.Match(input, @"-t ([0-9]*)");
            Match chapters = Regex.Match(input, @"-c ([0-9-]*)");

            // Output Settings
            Match format = Regex.Match(input, @"-f ([a-z0-9a-z0-9a-z0-9]*)");
            Match grayscale = Regex.Match(input, @" -g");
            Match largerMp4 = Regex.Match(input, @" -4");
            Match ipodAtom = Regex.Match(input, @" -I");

            // Picture Settings Tab
            Match width = Regex.Match(input, @"-w ([0-9]*)");
            Match height = Regex.Match(input, @"-l ([0-9]*)");
            Match maxWidth = Regex.Match(input, @"-X ([0-9]*)");
            Match maxHeight = Regex.Match(input, @"-Y ([0-9]*)");
            Match crop = Regex.Match(input, @"--crop ([0-9]*):([0-9]*):([0-9]*):([0-9]*)");

            Match looseAnamorphic = Regex.Match(input, @"--loose-anamorphic");
            Match strictAnamorphic = Regex.Match(input, @"--strict-anamorphic");
            Match customAnamorphic = Regex.Match(input, @"--custom-anamorphic");

            Match keepDisplayAsect = Regex.Match(input, @"--keep-display-aspect");
            Match displayWidth = Regex.Match(input, @"--display-width ([0-9]*)");
            Match pixelAspect = Regex.Match(input, @"--pixel-aspect ([0-9]*):([0-9]*)");
            Match modulus = Regex.Match(input, @"--modulus ([0-9]*)");

            // Picture Settings - Filters
            Match decomb = Regex.Match(input, @" --decomb");
            Match decombValue = Regex.Match(input, @" --decomb=\""([a-zA-Z0-9.:]*)\""");
            Match deinterlace = Regex.Match(input, @"--deinterlace=\""([a-zA-Z0-9.:]*)\""");
            Match denoise = Regex.Match(input, @"--denoise=\""([a-zA-Z0-9.:]*)\""");
            Match deblock = Regex.Match(input, @"--deblock=([0-9:]*)");
            Match detelecine = Regex.Match(input, @"--detelecine");
            Match detelecineValue = Regex.Match(input, @" --detelecine=\""([a-zA-Z0-9.:]*)\""");

            // Video Settings Tab
            Match videoEncoder = Regex.Match(input, @"-e ([a-zA-Z0-9]*)");
            Match videoFramerate = Regex.Match(input, @"-r ([0-9.]*)");
            Match videoBitrate = Regex.Match(input, @"-b ([0-9]*)");
            Match videoQuality = Regex.Match(input, @"-q ([0-9.]*)");
            Match videoFilesize = Regex.Match(input, @"-S ([0-9.]*)");
            Match twoPass = Regex.Match(input, @" -2");
            Match turboFirstPass = Regex.Match(input, @" -T");
            Match optimizeMP4 = Regex.Match(input, @" -O");
            Match pfr = Regex.Match(input, @" --pfr");

            // Audio Settings Tab
            Match noAudio = Regex.Match(input, @"-a none");
            Match audioTracks = Regex.Match(input, @"-a ([0-9,]*)");
            Match audioTrackMixes = Regex.Match(input, @"-6 ([0-9a-zA-Z,]*)");
            Match audioEncoders = Regex.Match(input, @"-E ([a-zA-Z0-9+,:]*)");
            Match audioBitrates = Regex.Match(input, @"-B ([0-9a-zA-Z,]*)"); // Auto = a-z
            Match audioSampleRates = Regex.Match(input, @"-R ([0-9a-zA-Z.,]*)"); // Auto = a-z
            Match drcValues = Regex.Match(input, @"-D ([0-9.,]*)");

            Match subtitles = Regex.Match(input, @"-s ([0-9a-zA-Z]*)");
            Match subScan = Regex.Match(input, @" -U");
            Match forcedSubtitles = Regex.Match(input, @" -F");

            // Chapters Tab
            Match chapterMarkers = Regex.Match(input, @" -m");
            Match chapterMarkersFileMode = Regex.Match(input, @"--markers");

            // H264 Tab
            Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");

            // Program Options
            Match verbose = Regex.Match(input, @" -v");

            #endregion

            #region Set Varibles

            try
            {
                #region Source Tab

                if (title.Success)
                    thisQuery.Title = int.Parse(title.ToString().Replace("-t ", string.Empty));

                if (chapters.Success)
                {
                    string[] actTitles = chapters.ToString().Replace("-c ", string.Empty).Split('-');
                    thisQuery.ChapterStart = int.Parse(actTitles[0]);
                    if (actTitles.Length > 1)
                    {
                        thisQuery.ChapterFinish = int.Parse(actTitles[1]);
                    }

                    if ((thisQuery.ChapterStart == 1) && (thisQuery.ChapterFinish == 0))
                        thisQuery.ChapterFinish = thisQuery.ChapterStart;
                }

                #endregion

                #region Output Settings

                if (format.Success)
                    thisQuery.Format = format.ToString().Replace("-f ", string.Empty);
                thisQuery.LargeMP4 = largerMp4.Success;
                thisQuery.IpodAtom = ipodAtom.Success;
                thisQuery.OptimizeMP4 = optimizeMP4.Success;

                #endregion

                #region Picture Tab

                if (width.Success)
                    thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", string.Empty));

                if (height.Success)
                    thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", string.Empty));

                if (maxWidth.Success)
                    thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", string.Empty));

                if (maxHeight.Success)
                    thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", string.Empty));

                if (crop.Success)
                {
                    thisQuery.CropValues = crop.ToString().Replace("--crop ", string.Empty);
                    string[] actCropValues = thisQuery.CropValues.Split(':');
                    thisQuery.CropTop = actCropValues[0];
                    thisQuery.CropBottom = actCropValues[1];
                    thisQuery.CropLeft = actCropValues[2];
                    thisQuery.CropRight = actCropValues[3];
                }

                if (strictAnamorphic.Success)
                    thisQuery.AnamorphicMode = 1;
                else if (looseAnamorphic.Success)
                    thisQuery.AnamorphicMode = 2;
                else if (customAnamorphic.Success)
                    thisQuery.AnamorphicMode = 3;
                else
                    thisQuery.AnamorphicMode = 0;

                thisQuery.KeepDisplayAsect = keepDisplayAsect.Success;

                if (displayWidth.Success)
                    thisQuery.DisplayWidthValue =
                        double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", string.Empty));

                if (pixelAspect.Success)
                    thisQuery.PixelAspectWidth = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", string.Empty));

                if (pixelAspect.Success && pixelAspect.Groups.Count >= 3)
                    thisQuery.PixelAspectHeight = int.Parse(pixelAspect.Groups[2].Value.Replace("--pixel-aspect ", string.Empty));

                if (modulus.Success)
                    thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", string.Empty));

                #endregion

                #region Filters

                thisQuery.Decomb = "Off";
                if (decomb.Success)
                {
                    thisQuery.Decomb = "Default";
                    if (decombValue.Success)
                        thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", string.Empty).Replace("\"", string.Empty);
                }

                thisQuery.DeInterlace = "Off";
                if (deinterlace.Success)
                {
                    thisQuery.DeInterlace = deinterlace.ToString().Replace("--deinterlace=", string.Empty).Replace("\"", string.Empty);
                    thisQuery.DeInterlace =
                        thisQuery.DeInterlace.Replace("fast", "Fast").Replace("slow", "Slow").Replace("slower", "Slower");
                    thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("slowest", "Slowest");
                }

                thisQuery.DeNoise = "Off";
                if (denoise.Success)
                {
                    thisQuery.DeNoise = denoise.ToString().Replace("--denoise=", string.Empty).Replace("\"", string.Empty);
                    thisQuery.DeNoise =
                        thisQuery.DeNoise.Replace("weak", "Weak").Replace("medium", "Medium").Replace("strong", "Strong");
                }

                string deblockValue = string.Empty;
                thisQuery.DeBlock = 0;
                if (deblock.Success)
                    deblockValue = deblock.ToString().Replace("--deblock=", string.Empty);

                int dval = 0;
                if (deblockValue != string.Empty)
                    int.TryParse(deblockValue, out dval);
                thisQuery.DeBlock = dval;

                thisQuery.DeTelecine = "Off";
                if (detelecine.Success)
                {
                    thisQuery.DeTelecine = "Default";
                    if (detelecineValue.Success)
                        thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", string.Empty).Replace("\"", string.Empty);
                }

                #endregion

                #region Video Settings Tab

                string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", string.Empty);
                switch (videoEncoderConvertion)
                {
                    case "ffmpeg":
                        videoEncoderConvertion = "MPEG-4 (FFmpeg)";
                        break;
                    case "x264":
                        videoEncoderConvertion = "H.264 (x264)";
                        break;
                    case "theora":
                        videoEncoderConvertion = "VP3 (Theora)";
                        break;
                    default:
                        videoEncoderConvertion = "MPEG-4 (FFmpeg)";
                        break;
                }
                thisQuery.VideoEncoder = videoEncoderConvertion;
                thisQuery.VideoFramerate = videoFramerate.Success
                                               ? videoFramerate.ToString().Replace("-r ", string.Empty)
                                               : "Same as source";

                thisQuery.Pfr = pfr.Success;
                thisQuery.Grayscale = grayscale.Success;
                thisQuery.TwoPass = twoPass.Success;
                thisQuery.TurboFirstPass = turboFirstPass.Success;

                if (videoBitrate.Success)
                    thisQuery.AverageVideoBitrate = videoBitrate.ToString().Replace("-b ", string.Empty);
                if (videoFilesize.Success)
                    thisQuery.VideoTargetSize = videoFilesize.ToString().Replace("-S ", string.Empty);

                if (videoQuality.Success)
                {
                    float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", string.Empty), Culture);
                    thisQuery.VideoQuality = qConvert;
                }
                else
                    thisQuery.VideoQuality = -1;

                #endregion

                #region Audio Tab

                // Find out how many tracks we need to add by checking how many encoders or audio tracks are selected.
                int encoderCount = 0;
                if (audioEncoders.Success)
                {
                    string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
                    encoderCount = audioDataCounters.Length;
                }

                // Get the data from the regular expression results
                string[] trackData = null;
                string[] trackMixes = null;
                string[] trackEncoders = null;
                string[] trackBitrates = null;
                string[] trackSamplerates = null;
                string[] trackDRCvalues = null;

                if (audioTracks.Success)
                    trackData = audioTracks.ToString().Replace("-a ", string.Empty).Split(',');
                if (audioTrackMixes.Success)
                    trackMixes = audioTrackMixes.ToString().Replace("-6 ", string.Empty).Split(',');
                if (audioEncoders.Success)
                    trackEncoders = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
                if (audioBitrates.Success)
                    trackBitrates = audioBitrates.ToString().Replace("-B ", string.Empty).Split(',');
                if (audioSampleRates.Success)
                    trackSamplerates = audioSampleRates.ToString().Replace("-R ", string.Empty).Split(',');
                if (drcValues.Success)
                    trackDRCvalues = drcValues.ToString().Replace("-D ", string.Empty).Split(',');

                // Create new Audio Track Classes and store them in the ArrayList
                ArrayList allAudioTrackInfo = new ArrayList();
                for (int x = 0; x < encoderCount; x++)
                {
                    AudioTrack track = new AudioTrack();
                    if (trackData != null)
                        if (trackData.Length >= (x + 1)) // Audio Track
                            track.Track = trackData[x].Trim();

                    if (trackMixes != null)
                        if (trackMixes.Length >= (x + 1)) // Audio Mix
                            track.MixDown = GetMixDown(trackMixes[x].Trim());

                    if (trackEncoders != null)
                        if (trackEncoders.Length >= (x + 1)) // Audio Mix
                            track.Encoder = GetAudioEncoder(trackEncoders[x].Trim());

                    if (trackBitrates != null)
                        if (trackBitrates.Length >= (x + 1)) // Audio Encoder
                            track.Bitrate = trackBitrates[x].Trim() == "auto" ? "Auto" : trackBitrates[x].Trim();

                    if (trackSamplerates != null)
                        if (trackSamplerates.Length >= (x + 1)) // Audio SampleRate
                            track.SampleRate = trackSamplerates[x].Trim() == "0" ? "Auto" : trackSamplerates[x].Trim();

                    if (trackDRCvalues != null)
                        if (trackDRCvalues.Length >= (x + 1)) // Audio DRC Values
                            track.DRC = trackDRCvalues[x].Trim();

                    allAudioTrackInfo.Add(track);
                }
                thisQuery.AudioInformation = allAudioTrackInfo;

                // Subtitle Stuff
                if (subtitles.Success)
                    thisQuery.Subtitles = subtitles.ToString().Replace("-s ", string.Empty);
                else
                    thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";

                thisQuery.ForcedSubtitles = forcedSubtitles.Success;

                #endregion

                #region Chapters Tab

                if (chapterMarkersFileMode.Success || chapterMarkers.Success)
                    thisQuery.ChapterMarkers = true;

                #endregion

                #region H.264 and other

                if (x264.Success)
                    thisQuery.H264Query = x264.ToString().Replace("-x ", string.Empty);

                thisQuery.Verbose = verbose.Success;

                #endregion
            }
            catch (Exception exc)
            {
                Main.ShowExceptiowWindow("An error has occured in the Query Parser.", exc.ToString());
            }

            #endregion

            return thisQuery;
        }