2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace HandBrake.ApplicationServices.Utilities
\r
9 using System.Collections;
\r
10 using System.Globalization;
\r
11 using System.Text.RegularExpressions;
\r
13 using HandBrake.ApplicationServices.Functions;
\r
14 using HandBrake.ApplicationServices.Model.Encoding;
\r
17 /// Parse a CLI Query
\r
19 public class QueryParserUtility
\r
24 private static readonly CultureInfo Culture = new CultureInfo("en-US", false);
\r
28 #region Source Title / Chapters
\r
30 /// Gets or sets Title.
\r
32 public int Title { get; set; }
\r
35 /// Gets or sets ChapterStart.
\r
37 public int ChapterStart { get; set; }
\r
40 /// Gets or sets ChapterFinish.
\r
42 public int ChapterFinish { get; set; }
\r
45 #region Output Settings
\r
47 /// Gets or sets the file Format. e.g mkv or mp4
\r
49 public string Format { get; set; }
\r
52 /// Gets or sets a value indicating whether LargeMP4 support is enabled.
\r
53 /// This is the 64bit MP4 file that allows >4GB files
\r
55 public bool LargeMP4 { get; set; }
\r
58 /// Gets or sets a value indicating whether IpodAtom is inserted
\r
60 public bool IpodAtom { get; set; }
\r
63 /// Gets or sets a value indicating whether OptimizeMP4 is enabed for web streaming
\r
65 public bool OptimizeMP4 { get; set; }
\r
68 #region Picture Settings
\r
71 /// Gets or sets Width.
\r
73 public int Width { get; set; }
\r
76 /// Gets or sets Height.
\r
78 public int Height { get; set; }
\r
81 /// Gets or sets MaxWidth.
\r
83 public int MaxWidth { get; set; }
\r
86 /// Gets or sets MaxHeight.
\r
88 public int MaxHeight { get; set; }
\r
91 /// Gets or sets CropValues.
\r
93 public string CropValues { get; set; }
\r
96 /// Gets or sets CropTop.
\r
98 public string CropTop { get; set; }
\r
101 /// Gets or sets CropBottom.
\r
103 public string CropBottom { get; set; }
\r
106 /// Gets or sets CropLeft.
\r
108 public string CropLeft { get; set; }
\r
111 /// Gets or sets CropRight.
\r
113 public string CropRight { get; set; }
\r
116 /// Gets or sets AnamorphicMode.
\r
118 public int AnamorphicMode { get; set; }
\r
121 /// Gets or sets a value indicating whether KeepDisplayAsect.
\r
123 public bool KeepDisplayAsect { get; set; }
\r
126 /// Gets or sets DisplayWidthValue.
\r
128 public double DisplayWidthValue { get; set; }
\r
131 /// Gets or sets PixelAspectWidth.
\r
133 public int PixelAspectWidth { get; set; }
\r
136 /// Gets or sets PixelAspectHeight.
\r
138 public int PixelAspectHeight { get; set; }
\r
141 /// Gets or sets AnamorphicModulus.
\r
143 public int AnamorphicModulus { get; set; }
\r
146 #region Video Filters
\r
149 /// Gets or sets DeTelecine.
\r
151 public string DeTelecine { get; set; }
\r
154 /// Gets or sets DeBlock.
\r
156 public int DeBlock { get; set; }
\r
159 /// Gets or sets DeInterlace.
\r
161 public string DeInterlace { get; set; }
\r
164 /// Gets or sets DeNoise.
\r
166 public string DeNoise { get; set; }
\r
169 /// Gets or sets Decomb.
\r
171 public string Decomb { get; set; }
\r
174 #region Video Settings
\r
176 /// Gets or sets VideoEncoder.
\r
178 public string VideoEncoder { get; set; }
\r
181 /// Gets or sets a value indicating whether Grayscale.
\r
183 public bool Grayscale { get; set; }
\r
186 /// Gets or sets a value indicating whether TwoPass.
\r
188 public bool TwoPass { get; set; }
\r
191 /// Gets or sets a value indicating whether TurboFirstPass.
\r
193 public bool TurboFirstPass { get; set; }
\r
196 /// Gets or sets VideoFramerate.
\r
198 public string VideoFramerate { get; set; }
\r
201 /// Gets or sets a value indicating whether Pfr.
\r
203 public bool Pfr { get; set; }
\r
206 /// Gets or sets AverageVideoBitrate.
\r
208 public string AverageVideoBitrate { get; set; }
\r
211 /// Gets or sets VideoTargetSize.
\r
213 public string VideoTargetSize { get; set; }
\r
216 /// Gets or sets VideoQuality.
\r
218 public float VideoQuality { get; set; }
\r
221 #region Audio Settings
\r
224 /// Gets or sets AudioInformation.
\r
226 public ArrayList AudioInformation { get; set; }
\r
229 /// Gets or sets Subtitles.
\r
231 public string Subtitles { get; set; }
\r
234 /// Gets or sets a value indicating whether ForcedSubtitles.
\r
236 public bool ForcedSubtitles { get; set; }
\r
241 /// Gets or sets a value indicating whether ChapterMarkers.
\r
243 public bool ChapterMarkers { get; set; }
\r
246 /// Gets or sets H264Query.
\r
248 public string H264Query { get; set; }
\r
251 /// Gets or sets a value indicating whether Verbose.
\r
253 public bool Verbose { get; set; }
\r
256 #region Preset Information
\r
259 /// Gets or sets PresetBuildNumber.
\r
261 public int PresetBuildNumber { get; set; }
\r
264 /// Gets or sets PresetDescription.
\r
266 public string PresetDescription { get; set; }
\r
269 /// Gets or sets PresetName.
\r
271 public string PresetName { get; set; }
\r
274 /// Gets or sets Type.
\r
276 public string Type { get; set; }
\r
279 /// Gets or sets a value indicating whether UsesMaxPictureSettings.
\r
281 public bool UsesMaxPictureSettings { get; set; }
\r
284 /// Gets or sets a value indicating whether UsesPictureFilters.
\r
286 public bool UsesPictureFilters { get; set; }
\r
289 /// Gets or sets a value indicating whether UsesPictureSettings.
\r
291 public bool UsesPictureSettings { get; set; }
\r
297 /// Takes in a query which can be in any order and parses it.
\r
298 /// All varibles are then set so they can be used elsewhere.
\r
300 /// <param name="input">A ClI Query</param>
\r
301 /// <returns>A Parsed Query</returns>
\r
302 public static QueryParserUtility Parse(string input)
\r
304 var thisQuery = new QueryParserUtility();
\r
306 #region Regular Expressions
\r
309 Match title = Regex.Match(input, @"-t ([0-9]*)");
\r
310 Match chapters = Regex.Match(input, @"-c ([0-9-]*)");
\r
313 Match format = Regex.Match(input, @"-f ([a-z0-9a-z0-9a-z0-9]*)");
\r
314 Match grayscale = Regex.Match(input, @" -g");
\r
315 Match largerMp4 = Regex.Match(input, @" -4");
\r
316 Match ipodAtom = Regex.Match(input, @" -I");
\r
318 // Picture Settings Tab
\r
319 Match width = Regex.Match(input, @"-w ([0-9]*)");
\r
320 Match height = Regex.Match(input, @"-l ([0-9]*)");
\r
321 Match maxWidth = Regex.Match(input, @"-X ([0-9]*)");
\r
322 Match maxHeight = Regex.Match(input, @"-Y ([0-9]*)");
\r
323 Match crop = Regex.Match(input, @"--crop ([0-9]*):([0-9]*):([0-9]*):([0-9]*)");
\r
325 Match looseAnamorphic = Regex.Match(input, @"--loose-anamorphic");
\r
326 Match strictAnamorphic = Regex.Match(input, @"--strict-anamorphic");
\r
327 Match customAnamorphic = Regex.Match(input, @"--custom-anamorphic");
\r
329 Match keepDisplayAsect = Regex.Match(input, @"--keep-display-aspect");
\r
330 Match displayWidth = Regex.Match(input, @"--display-width ([0-9]*)");
\r
331 Match pixelAspect = Regex.Match(input, @"--pixel-aspect ([0-9]*):([0-9]*)");
\r
332 Match modulus = Regex.Match(input, @"--modulus ([0-9]*)");
\r
334 // Picture Settings - Filters
\r
335 Match decomb = Regex.Match(input, @" --decomb");
\r
336 Match decombValue = Regex.Match(input, @" --decomb=\""([a-zA-Z0-9.:]*)\""");
\r
337 Match deinterlace = Regex.Match(input, @"--deinterlace=\""([a-zA-Z0-9.:]*)\""");
\r
338 Match denoise = Regex.Match(input, @"--denoise=\""([a-zA-Z0-9.:]*)\""");
\r
339 Match deblock = Regex.Match(input, @"--deblock=([0-9:]*)");
\r
340 Match detelecine = Regex.Match(input, @"--detelecine");
\r
341 Match detelecineValue = Regex.Match(input, @" --detelecine=\""([a-zA-Z0-9.:]*)\""");
\r
343 // Video Settings Tab
\r
344 Match videoEncoder = Regex.Match(input, @"-e ([a-zA-Z0-9]*)");
\r
345 Match videoFramerate = Regex.Match(input, @"-r ([0-9.]*)");
\r
346 Match videoBitrate = Regex.Match(input, @"-b ([0-9]*)");
\r
347 Match videoQuality = Regex.Match(input, @"-q ([0-9.]*)");
\r
348 Match videoFilesize = Regex.Match(input, @"-S ([0-9.]*)");
\r
349 Match twoPass = Regex.Match(input, @" -2");
\r
350 Match turboFirstPass = Regex.Match(input, @" -T");
\r
351 Match optimizeMP4 = Regex.Match(input, @" -O");
\r
352 Match pfr = Regex.Match(input, @" --pfr");
\r
354 // Audio Settings Tab
\r
355 Match noAudio = Regex.Match(input, @"-a none");
\r
356 Match audioTracks = Regex.Match(input, @"-a ([0-9,]*)");
\r
357 Match audioTrackMixes = Regex.Match(input, @"-6 ([0-9a-zA-Z,]*)");
\r
358 Match audioEncoders = Regex.Match(input, @"-E ([a-zA-Z0-9+,:]*)");
\r
359 Match audioBitrates = Regex.Match(input, @"-B ([0-9a-zA-Z,]*)"); // Auto = a-z
\r
360 Match audioSampleRates = Regex.Match(input, @"-R ([0-9a-zA-Z.,]*)"); // Auto = a-z
\r
361 Match drcValues = Regex.Match(input, @"-D ([0-9.,]*)");
\r
363 Match subtitles = Regex.Match(input, @"-s ([0-9a-zA-Z]*)");
\r
364 Match subScan = Regex.Match(input, @" -U");
\r
365 Match forcedSubtitles = Regex.Match(input, @" -F");
\r
368 Match chapterMarkers = Regex.Match(input, @" -m");
\r
369 Match chapterMarkersFileMode = Regex.Match(input, @"--markers");
\r
372 Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");
\r
375 Match verbose = Regex.Match(input, @" -v");
\r
379 #region Set Varibles
\r
386 thisQuery.Title = int.Parse(title.ToString().Replace("-t ", string.Empty));
\r
388 if (chapters.Success)
\r
390 string[] actTitles = chapters.ToString().Replace("-c ", string.Empty).Split('-');
\r
391 thisQuery.ChapterStart = int.Parse(actTitles[0]);
\r
392 if (actTitles.Length > 1)
\r
394 thisQuery.ChapterFinish = int.Parse(actTitles[1]);
\r
397 if ((thisQuery.ChapterStart == 1) && (thisQuery.ChapterFinish == 0))
\r
398 thisQuery.ChapterFinish = thisQuery.ChapterStart;
\r
403 #region Output Settings
\r
405 if (format.Success)
\r
406 thisQuery.Format = format.ToString().Replace("-f ", string.Empty);
\r
407 thisQuery.LargeMP4 = largerMp4.Success;
\r
408 thisQuery.IpodAtom = ipodAtom.Success;
\r
409 thisQuery.OptimizeMP4 = optimizeMP4.Success;
\r
413 #region Picture Tab
\r
416 thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", string.Empty));
\r
418 if (height.Success)
\r
419 thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", string.Empty));
\r
421 if (maxWidth.Success)
\r
422 thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", string.Empty));
\r
424 if (maxHeight.Success)
\r
425 thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", string.Empty));
\r
429 thisQuery.CropValues = crop.ToString().Replace("--crop ", string.Empty);
\r
430 string[] actCropValues = thisQuery.CropValues.Split(':');
\r
431 thisQuery.CropTop = actCropValues[0];
\r
432 thisQuery.CropBottom = actCropValues[1];
\r
433 thisQuery.CropLeft = actCropValues[2];
\r
434 thisQuery.CropRight = actCropValues[3];
\r
437 if (strictAnamorphic.Success)
\r
438 thisQuery.AnamorphicMode = 1;
\r
439 else if (looseAnamorphic.Success)
\r
440 thisQuery.AnamorphicMode = 2;
\r
441 else if (customAnamorphic.Success)
\r
442 thisQuery.AnamorphicMode = 3;
\r
444 thisQuery.AnamorphicMode = 0;
\r
446 thisQuery.KeepDisplayAsect = keepDisplayAsect.Success;
\r
448 if (displayWidth.Success)
\r
449 thisQuery.DisplayWidthValue =
\r
450 double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", string.Empty));
\r
452 if (pixelAspect.Success)
\r
453 thisQuery.PixelAspectWidth = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", string.Empty));
\r
455 if (pixelAspect.Success && pixelAspect.Groups.Count >= 3)
\r
456 thisQuery.PixelAspectHeight = int.Parse(pixelAspect.Groups[2].Value.Replace("--pixel-aspect ", string.Empty));
\r
458 if (modulus.Success)
\r
459 thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", string.Empty));
\r
465 thisQuery.Decomb = "Off";
\r
466 if (decomb.Success)
\r
468 thisQuery.Decomb = "Default";
\r
469 if (decombValue.Success)
\r
470 thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", string.Empty).Replace("\"", string.Empty);
\r
473 thisQuery.DeInterlace = "Off";
\r
474 if (deinterlace.Success)
\r
476 thisQuery.DeInterlace = deinterlace.ToString().Replace("--deinterlace=", string.Empty).Replace("\"", string.Empty);
\r
477 thisQuery.DeInterlace =
\r
478 thisQuery.DeInterlace.Replace("fast", "Fast").Replace("slow", "Slow").Replace("slower", "Slower");
\r
479 thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("slowest", "Slowest");
\r
482 thisQuery.DeNoise = "Off";
\r
483 if (denoise.Success)
\r
485 thisQuery.DeNoise = denoise.ToString().Replace("--denoise=", string.Empty).Replace("\"", string.Empty);
\r
486 thisQuery.DeNoise =
\r
487 thisQuery.DeNoise.Replace("weak", "Weak").Replace("medium", "Medium").Replace("strong", "Strong");
\r
490 string deblockValue = string.Empty;
\r
491 thisQuery.DeBlock = 0;
\r
492 if (deblock.Success)
\r
493 deblockValue = deblock.ToString().Replace("--deblock=", string.Empty);
\r
496 if (deblockValue != string.Empty)
\r
497 int.TryParse(deblockValue, out dval);
\r
498 thisQuery.DeBlock = dval;
\r
500 thisQuery.DeTelecine = "Off";
\r
501 if (detelecine.Success)
\r
503 thisQuery.DeTelecine = "Default";
\r
504 if (detelecineValue.Success)
\r
505 thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", string.Empty).Replace("\"", string.Empty);
\r
510 #region Video Settings Tab
\r
512 string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", string.Empty);
\r
513 switch (videoEncoderConvertion)
\r
516 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
519 videoEncoderConvertion = "H.264 (x264)";
\r
522 videoEncoderConvertion = "VP3 (Theora)";
\r
525 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
528 thisQuery.VideoEncoder = videoEncoderConvertion;
\r
529 thisQuery.VideoFramerate = videoFramerate.Success
\r
530 ? videoFramerate.ToString().Replace("-r ", string.Empty)
\r
531 : "Same as source";
\r
533 thisQuery.Pfr = pfr.Success;
\r
534 thisQuery.Grayscale = grayscale.Success;
\r
535 thisQuery.TwoPass = twoPass.Success;
\r
536 thisQuery.TurboFirstPass = turboFirstPass.Success;
\r
538 if (videoBitrate.Success)
\r
539 thisQuery.AverageVideoBitrate = videoBitrate.ToString().Replace("-b ", string.Empty);
\r
540 if (videoFilesize.Success)
\r
541 thisQuery.VideoTargetSize = videoFilesize.ToString().Replace("-S ", string.Empty);
\r
543 if (videoQuality.Success)
\r
545 float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", string.Empty), Culture);
\r
546 thisQuery.VideoQuality = qConvert;
\r
549 thisQuery.VideoQuality = -1;
\r
555 // Find out how many tracks we need to add by checking how many encoders or audio tracks are selected.
\r
556 int encoderCount = 0;
\r
557 if (audioEncoders.Success)
\r
559 string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
\r
560 encoderCount = audioDataCounters.Length;
\r
563 // Get the data from the regular expression results
\r
564 string[] trackData = null;
\r
565 string[] trackMixes = null;
\r
566 string[] trackEncoders = null;
\r
567 string[] trackBitrates = null;
\r
568 string[] trackSamplerates = null;
\r
569 string[] trackDRCvalues = null;
\r
571 if (audioTracks.Success)
\r
572 trackData = audioTracks.ToString().Replace("-a ", string.Empty).Split(',');
\r
573 if (audioTrackMixes.Success)
\r
574 trackMixes = audioTrackMixes.ToString().Replace("-6 ", string.Empty).Split(',');
\r
575 if (audioEncoders.Success)
\r
576 trackEncoders = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
\r
577 if (audioBitrates.Success)
\r
578 trackBitrates = audioBitrates.ToString().Replace("-B ", string.Empty).Split(',');
\r
579 if (audioSampleRates.Success)
\r
580 trackSamplerates = audioSampleRates.ToString().Replace("-R ", string.Empty).Split(',');
\r
581 if (drcValues.Success)
\r
582 trackDRCvalues = drcValues.ToString().Replace("-D ", string.Empty).Split(',');
\r
584 // Create new Audio Track Classes and store them in the ArrayList
\r
585 ArrayList allAudioTrackInfo = new ArrayList();
\r
586 for (int x = 0; x < encoderCount; x++)
\r
588 AudioTrack track = new AudioTrack();
\r
589 if (trackData != null)
\r
590 if (trackData.Length >= (x + 1)) // Audio Track
\r
591 track.Track = trackData[x].Trim();
\r
593 if (trackMixes != null)
\r
594 if (trackMixes.Length >= (x + 1)) // Audio Mix
\r
595 track.MixDown = Converters.GetMixDown(trackMixes[x].Trim());
\r
597 if (trackEncoders != null)
\r
598 if (trackEncoders.Length >= (x + 1)) // Audio Mix
\r
599 track.Encoder = Converters.GetAudioEncoder(trackEncoders[x].Trim());
\r
601 if (trackBitrates != null)
\r
602 if (trackBitrates.Length >= (x + 1)) // Audio Encoder
\r
603 track.Bitrate = trackBitrates[x].Trim() == "auto" ? "Auto" : trackBitrates[x].Trim();
\r
605 if (trackSamplerates != null)
\r
606 if (trackSamplerates.Length >= (x + 1)) // Audio SampleRate
\r
607 track.SampleRate = trackSamplerates[x].Trim() == "0" ? "Auto" : trackSamplerates[x].Trim();
\r
609 if (trackDRCvalues != null)
\r
610 if (trackDRCvalues.Length >= (x + 1)) // Audio DRC Values
\r
611 track.DRC = trackDRCvalues[x].Trim();
\r
613 allAudioTrackInfo.Add(track);
\r
615 thisQuery.AudioInformation = allAudioTrackInfo;
\r
618 if (subtitles.Success)
\r
619 thisQuery.Subtitles = subtitles.ToString().Replace("-s ", string.Empty);
\r
621 thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";
\r
623 thisQuery.ForcedSubtitles = forcedSubtitles.Success;
\r
627 #region Chapters Tab
\r
629 if (chapterMarkersFileMode.Success || chapterMarkers.Success)
\r
630 thisQuery.ChapterMarkers = true;
\r
634 #region H.264 and other
\r
637 thisQuery.H264Query = x264.ToString().Replace("-x ", string.Empty);
\r
639 thisQuery.Verbose = verbose.Success;
\r
643 catch (Exception exc)
\r
645 throw new Exception("An error has occured in the QueryParser Utility.", exc);
\r