3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
7 namespace Handbrake.Functions
\r
10 using System.Collections;
\r
11 using System.Globalization;
\r
12 using System.Text.RegularExpressions;
\r
13 using System.Windows.Forms;
\r
16 public class QueryParser
\r
18 private static readonly CultureInfo Culture = new CultureInfo("en-US", false);
\r
23 public int DVDTitle { get; set; }
\r
24 public int DVDChapterStart { get; set; }
\r
25 public int DVDChapterFinish { get; set; }
\r
28 public string Format { get; set; }
\r
29 public bool LargeMP4 { get; set; }
\r
30 public bool IpodAtom { get; set; }
\r
31 public bool OptimizeMP4 { get; set; }
\r
34 public int Width { get; set; }
\r
35 public int Height { get; set; }
\r
36 public int MaxWidth { get; set; }
\r
37 public int MaxHeight { get; set; }
\r
38 public string CropValues { get; set; }
\r
39 public string CropTop { get; set; }
\r
40 public string CropBottom { get; set; }
\r
41 public string CropLeft { get; set; }
\r
42 public string CropRight { get; set; }
\r
43 public int AnamorphicMode { get; set; }
\r
44 public bool KeepDisplayAsect { get; set; }
\r
45 public double DisplayWidthValue { get; set; }
\r
46 public int PixelAspectWidth { get; set; }
\r
47 public int PixelAspectHeight { get; set; }
\r
48 public int AnamorphicModulus { get; set; }
\r
51 public string DeTelecine { get; set; }
\r
52 public int DeBlock { get; set; }
\r
53 public string DeInterlace { get; set; }
\r
54 public string DeNoise { get; set; }
\r
55 public string Decomb { get; set; }
\r
58 public string VideoEncoder { get; set; }
\r
59 public bool Grayscale { get; set; }
\r
60 public bool TwoPass { get; set; }
\r
61 public bool TurboFirstPass { get; set; }
\r
62 public string VideoFramerate { get; set; }
\r
63 public string AverageVideoBitrate { get; set; }
\r
64 public string VideoTargetSize { get; set; }
\r
65 public float VideoQuality { get; set; }
\r
68 public ArrayList AudioInformation { get; set; }
\r
69 public string Subtitles { get; set; }
\r
70 public bool ForcedSubtitles { get; set; }
\r
73 public bool ChapterMarkers { get; set; }
\r
76 public string H264Query { get; set; }
\r
77 public bool Verbose { get; set; }
\r
79 // Preset Information
\r
80 public int PresetBuildNumber { get; set; }
\r
81 public string PresetDescription { get; set; }
\r
82 public string PresetName { get; set; }
\r
83 public string Type { get; set; }
\r
84 public bool UsesMaxPictureSettings { get; set; }
\r
85 public bool UsesPictureFilters { get; set; }
\r
86 public bool UsesPictureSettings { get; set; }
\r
91 /// Takes in a query which can be in any order and parses it.
\r
92 /// All varibles are then set so they can be used elsewhere.
\r
94 /// <param name="input">A ClI Query</param>
\r
95 /// <returns>A Parsed Query</returns>
\r
96 public static QueryParser Parse(string input)
\r
98 var thisQuery = new QueryParser();
\r
100 #region Regular Expressions
\r
103 Match title = Regex.Match(input, @"-t ([0-9]*)");
\r
104 Match chapters = Regex.Match(input, @"-c ([0-9-]*)");
\r
107 Match format = Regex.Match(input, @"-f ([a-z0-9a-z0-9a-z0-9]*)");
\r
108 Match grayscale = Regex.Match(input, @" -g");
\r
109 Match largerMp4 = Regex.Match(input, @" -4");
\r
110 Match ipodAtom = Regex.Match(input, @" -I");
\r
112 // Picture Settings Tab
\r
113 Match width = Regex.Match(input, @"-w ([0-9]*)");
\r
114 Match height = Regex.Match(input, @"-l ([0-9]*)");
\r
115 Match maxWidth = Regex.Match(input, @"-X ([0-9]*)");
\r
116 Match maxHeight = Regex.Match(input, @"-Y ([0-9]*)");
\r
117 Match crop = Regex.Match(input, @"--crop ([0-9]*):([0-9]*):([0-9]*):([0-9]*)");
\r
119 Match looseAnamorphic = Regex.Match(input, @"--loose-anamorphic");
\r
120 Match strictAnamorphic = Regex.Match(input, @"--strict-anamorphic");
\r
121 Match customAnamorphic = Regex.Match(input, @"--custom-anamorphic");
\r
123 Match keepDisplayAsect = Regex.Match(input, @"--keep-display-aspect");
\r
124 Match displayWidth = Regex.Match(input, @"--display-width ([0-9*])");
\r
125 Match pixelAspect = Regex.Match(input, @"--pixel-aspect ([0-9]*):([0-9]*)");
\r
126 Match modulus = Regex.Match(input, @"--modulus ([0-9*])");
\r
128 // Picture Settings - Filters
\r
129 Match decomb = Regex.Match(input, @" --decomb");
\r
130 Match decombValue = Regex.Match(input, @" --decomb=\""([a-zA-Z0-9.:]*)\""");
\r
131 Match deinterlace = Regex.Match(input, @"--deinterlace=\""([a-zA-Z0-9.:]*)\""");
\r
132 Match denoise = Regex.Match(input, @"--denoise=\""([a-zA-Z0-9.:]*)\""");
\r
133 Match deblock = Regex.Match(input, @"--deblock=([0-9:]*)");
\r
134 Match detelecine = Regex.Match(input, @"--detelecine");
\r
135 Match detelecineValue = Regex.Match(input, @" --detelecine=\""([a-zA-Z0-9.:]*)\""");
\r
137 // Video Settings Tab
\r
138 Match videoEncoder = Regex.Match(input, @"-e ([a-zA-Z0-9]*)");
\r
139 Match videoFramerate = Regex.Match(input, @"-r ([0-9.]*)");
\r
140 Match videoBitrate = Regex.Match(input, @"-b ([0-9]*)");
\r
141 Match videoQuality = Regex.Match(input, @"-q ([0-9.]*)");
\r
142 Match videoFilesize = Regex.Match(input, @"-S ([0-9.]*)");
\r
143 Match twoPass = Regex.Match(input, @" -2");
\r
144 Match turboFirstPass = Regex.Match(input, @" -T");
\r
145 Match optimizeMP4 = Regex.Match(input, @" -O");
\r
147 // Audio Settings Tab
\r
148 Match noAudio = Regex.Match(input, @"-a none");
\r
149 Match audioTracks = Regex.Match(input, @"-a ([0-9,]*)");
\r
150 Match audioTrackMixes = Regex.Match(input, @"-6 ([0-9a-zA-Z,]*)");
\r
151 Match audioEncoders = Regex.Match(input, @"-E ([a-zA-Z0-9+,]*)");
\r
152 Match audioBitrates = Regex.Match(input, @"-B ([0-9a-zA-Z,]*)"); // Auto = a-z
\r
153 Match audioSampleRates = Regex.Match(input, @"-R ([0-9a-zA-Z.,]*)"); // Auto = a-z
\r
154 Match drcValues = Regex.Match(input, @"-D ([0-9.,]*)");
\r
156 Match subtitles = Regex.Match(input, @"-s ([0-9a-zA-Z]*)");
\r
157 Match subScan = Regex.Match(input, @" -U");
\r
158 Match forcedSubtitles = Regex.Match(input, @" -F");
\r
161 Match chapterMarkers = Regex.Match(input, @" -m");
\r
162 Match chapterMarkersFileMode = Regex.Match(input, @"--markers");
\r
165 Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");
\r
168 Match verbose = Regex.Match(input, @" -v");
\r
172 #region Set Varibles
\r
179 thisQuery.DVDTitle = int.Parse(title.ToString().Replace("-t ", string.Empty));
\r
181 if (chapters.Success)
\r
183 string[] actTitles = chapters.ToString().Replace("-c ", string.Empty).Split('-');
\r
184 thisQuery.DVDChapterStart = int.Parse(actTitles[0]);
\r
185 if (actTitles.Length > 1)
\r
187 thisQuery.DVDChapterFinish = int.Parse(actTitles[1]);
\r
190 if ((thisQuery.DVDChapterStart == 1) && (thisQuery.DVDChapterFinish == 0))
\r
191 thisQuery.DVDChapterFinish = thisQuery.DVDChapterStart;
\r
196 #region Output Settings
\r
198 if (format.Success)
\r
199 thisQuery.Format = format.ToString().Replace("-f ", string.Empty);
\r
200 thisQuery.LargeMP4 = largerMp4.Success;
\r
201 thisQuery.IpodAtom = ipodAtom.Success;
\r
202 thisQuery.OptimizeMP4 = optimizeMP4.Success;
\r
206 #region Picture Tab
\r
209 thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", string.Empty));
\r
211 if (height.Success)
\r
212 thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", string.Empty));
\r
214 if (maxWidth.Success)
\r
215 thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", string.Empty));
\r
217 if (maxHeight.Success)
\r
218 thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", string.Empty));
\r
222 thisQuery.CropValues = crop.ToString().Replace("--crop ", string.Empty);
\r
223 string[] actCropValues = thisQuery.CropValues.Split(':');
\r
224 thisQuery.CropTop = actCropValues[0];
\r
225 thisQuery.CropBottom = actCropValues[1];
\r
226 thisQuery.CropLeft = actCropValues[2];
\r
227 thisQuery.CropRight = actCropValues[3];
\r
230 if (strictAnamorphic.Success)
\r
231 thisQuery.AnamorphicMode = 1;
\r
232 else if (looseAnamorphic.Success)
\r
233 thisQuery.AnamorphicMode = 2;
\r
234 else if (customAnamorphic.Success)
\r
235 thisQuery.AnamorphicMode = 3;
\r
237 thisQuery.AnamorphicMode = 0;
\r
239 thisQuery.KeepDisplayAsect = keepDisplayAsect.Success;
\r
241 if (displayWidth.Success)
\r
242 thisQuery.DisplayWidthValue =
\r
243 double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", string.Empty));
\r
245 if (pixelAspect.Success)
\r
246 thisQuery.PixelAspectWidth = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", string.Empty));
\r
248 if (pixelAspect.Success && pixelAspect.Groups.Count >= 3)
\r
249 thisQuery.PixelAspectHeight = int.Parse(pixelAspect.Groups[2].Value.Replace("--pixel-aspect ", string.Empty));
\r
251 if (modulus.Success)
\r
252 thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", string.Empty));
\r
258 thisQuery.Decomb = "Off";
\r
259 if (decomb.Success)
\r
261 thisQuery.Decomb = "Default";
\r
262 if (decombValue.Success)
\r
263 thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", string.Empty).Replace("\"", string.Empty);
\r
266 thisQuery.DeInterlace = "Off";
\r
267 if (deinterlace.Success)
\r
269 thisQuery.DeInterlace = deinterlace.ToString().Replace("--deinterlace=", string.Empty).Replace("\"", string.Empty);
\r
270 thisQuery.DeInterlace =
\r
271 thisQuery.DeInterlace.Replace("fast", "Fast").Replace("slow", "Slow").Replace("slower", "Slower");
\r
272 thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("slowest", "Slowest");
\r
275 thisQuery.DeNoise = "Off";
\r
276 if (denoise.Success)
\r
278 thisQuery.DeNoise = denoise.ToString().Replace("--denoise=", string.Empty).Replace("\"", string.Empty);
\r
279 thisQuery.DeNoise =
\r
280 thisQuery.DeNoise.Replace("weak", "Weak").Replace("medium", "Medium").Replace("strong", "Strong");
\r
283 string deblockValue = string.Empty;
\r
284 thisQuery.DeBlock = 0;
\r
285 if (deblock.Success)
\r
286 deblockValue = deblock.ToString().Replace("--deblock=", string.Empty);
\r
289 if (deblockValue != string.Empty)
\r
290 int.TryParse(deblockValue, out dval);
\r
291 thisQuery.DeBlock = dval;
\r
293 thisQuery.DeTelecine = "Off";
\r
294 if (detelecine.Success)
\r
296 thisQuery.DeTelecine = "Default";
\r
297 if (detelecineValue.Success)
\r
298 thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", string.Empty).Replace("\"", string.Empty);
\r
303 #region Video Settings Tab
\r
305 string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", string.Empty);
\r
306 switch (videoEncoderConvertion)
\r
309 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
312 videoEncoderConvertion = "H.264 (x264)";
\r
315 videoEncoderConvertion = "VP3 (Theora)";
\r
318 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
321 thisQuery.VideoEncoder = videoEncoderConvertion;
\r
322 thisQuery.VideoFramerate = videoFramerate.Success
\r
323 ? videoFramerate.ToString().Replace("-r ", string.Empty)
\r
324 : "Same as source";
\r
325 thisQuery.Grayscale = grayscale.Success;
\r
326 thisQuery.TwoPass = twoPass.Success;
\r
327 thisQuery.TurboFirstPass = turboFirstPass.Success;
\r
329 if (videoBitrate.Success)
\r
330 thisQuery.AverageVideoBitrate = videoBitrate.ToString().Replace("-b ", string.Empty);
\r
331 if (videoFilesize.Success)
\r
332 thisQuery.VideoTargetSize = videoFilesize.ToString().Replace("-S ", string.Empty);
\r
334 if (videoQuality.Success)
\r
336 float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", string.Empty), Culture);
\r
337 thisQuery.VideoQuality = qConvert;
\r
340 thisQuery.VideoQuality = -1;
\r
346 // Find out how many tracks we need to add by checking how many encoders or audio tracks are selected.
\r
347 int encoderCount = 0;
\r
348 if (audioEncoders.Success)
\r
350 string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
\r
351 encoderCount = audioDataCounters.Length;
\r
354 // Get the data from the regular expression results
\r
355 string[] trackData = null;
\r
356 string[] trackMixes = null;
\r
357 string[] trackEncoders = null;
\r
358 string[] trackBitrates = null;
\r
359 string[] trackSamplerates = null;
\r
360 string[] trackDRCvalues = null;
\r
362 if (audioTracks.Success)
\r
363 trackData = audioTracks.ToString().Replace("-a ", string.Empty).Split(',');
\r
364 if (audioTrackMixes.Success)
\r
365 trackMixes = audioTrackMixes.ToString().Replace("-6 ", string.Empty).Split(',');
\r
366 if (audioEncoders.Success)
\r
367 trackEncoders = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
\r
368 if (audioBitrates.Success)
\r
369 trackBitrates = audioBitrates.ToString().Replace("-B ", string.Empty).Split(',');
\r
370 if (audioSampleRates.Success)
\r
371 trackSamplerates = audioSampleRates.ToString().Replace("-R ", string.Empty).Split(',');
\r
372 if (drcValues.Success)
\r
373 trackDRCvalues = drcValues.ToString().Replace("-D ", string.Empty).Split(',');
\r
375 // Create new Audio Track Classes and store them in the ArrayList
\r
376 ArrayList allAudioTrackInfo = new ArrayList();
\r
377 for (int x = 0; x < encoderCount; x++)
\r
379 AudioTrack track = new AudioTrack();
\r
380 if (trackData != null)
\r
381 if (trackData.Length >= (x + 1)) // Audio Track
\r
382 track.Track = trackData[x].Trim();
\r
384 if (trackMixes != null)
\r
385 if (trackMixes.Length >= (x + 1)) // Audio Mix
\r
386 track.MixDown = GetMixDown(trackMixes[x].Trim());
\r
388 if (trackEncoders != null)
\r
389 if (trackEncoders.Length >= (x + 1)) // Audio Mix
\r
390 track.Encoder = GetAudioEncoder(trackEncoders[x].Trim());
\r
392 if (trackBitrates != null)
\r
393 if (trackBitrates.Length >= (x + 1)) // Audio Encoder
\r
394 track.Bitrate = trackBitrates[x].Trim() == "auto" ? "Auto" : trackBitrates[x].Trim();
\r
396 if (trackSamplerates != null)
\r
397 if (trackSamplerates.Length >= (x + 1)) // Audio SampleRate
\r
398 track.SampleRate = trackSamplerates[x].Trim() == "0" ? "Auto" : trackSamplerates[x].Trim();
\r
400 if (trackDRCvalues != null)
\r
401 if (trackDRCvalues.Length >= (x + 1)) // Audio DRC Values
\r
402 track.DRC = trackDRCvalues[x].Trim();
\r
404 allAudioTrackInfo.Add(track);
\r
406 thisQuery.AudioInformation = allAudioTrackInfo;
\r
409 if (subtitles.Success)
\r
410 thisQuery.Subtitles = subtitles.ToString().Replace("-s ", string.Empty);
\r
412 thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";
\r
414 thisQuery.ForcedSubtitles = forcedSubtitles.Success;
\r
418 #region Chapters Tab
\r
420 if (chapterMarkersFileMode.Success || chapterMarkers.Success)
\r
421 thisQuery.ChapterMarkers = true;
\r
425 #region H.264 and other
\r
428 thisQuery.H264Query = x264.ToString().Replace("-x ", string.Empty);
\r
430 thisQuery.Verbose = verbose.Success;
\r
434 catch (Exception exc)
\r
437 "An error has occured in the Query Parser.\n\n" +
\r
438 exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
447 /// Get the GUI equiv to a CLI mixdown
\r
449 /// <param name="mixdown"></param>
\r
450 /// <returns></returns>
\r
451 private static string GetMixDown(string mixdown)
\r
453 switch (mixdown.Trim())
\r
460 return "Dolby Surround";
\r
462 return "Dolby Pro Logic II";
\r
464 return "6 Channel Discrete";
\r
466 return "Automatic";
\r
471 /// Get the GUI equiv to a CLI audio encoder
\r
473 /// <param name="audioEnc"></param>
\r
474 /// <returns></returns>
\r
475 private static string GetAudioEncoder(string audioEnc)
\r
480 return "AAC (faac)";
\r
482 return "MP3 (lame)";
\r
484 return "Vorbis (vorbis)";
\r
486 return "AC3 Passthru";
\r
488 return "DTS Passthru";
\r
490 return "AAC (faac)";
\r