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.Functions
\r
9 using System.Collections;
\r
10 using System.Globalization;
\r
11 using System.Text.RegularExpressions;
\r
15 /// Parse a CLI Query
\r
17 public class QueryParser
\r
22 private static readonly CultureInfo Culture = new CultureInfo("en-US", false);
\r
26 #region Source Title / Chapters
\r
28 /// Gets or sets Title.
\r
30 public int Title { get; set; }
\r
33 /// Gets or sets ChapterStart.
\r
35 public int ChapterStart { get; set; }
\r
38 /// Gets or sets ChapterFinish.
\r
40 public int ChapterFinish { get; set; }
\r
43 #region Output Settings
\r
45 /// Gets or sets the file Format. e.g mkv or mp4
\r
47 public string Format { get; set; }
\r
50 /// Gets or sets a value indicating whether LargeMP4 support is enabled.
\r
51 /// This is the 64bit MP4 file that allows >4GB files
\r
53 public bool LargeMP4 { get; set; }
\r
56 /// Gets or sets a value indicating whether IpodAtom is inserted
\r
58 public bool IpodAtom { get; set; }
\r
61 /// Gets or sets a value indicating whether OptimizeMP4 is enabed for web streaming
\r
63 public bool OptimizeMP4 { get; set; }
\r
66 #region Picture Settings
\r
69 /// Gets or sets Width.
\r
71 public int Width { get; set; }
\r
74 /// Gets or sets Height.
\r
76 public int Height { get; set; }
\r
79 /// Gets or sets MaxWidth.
\r
81 public int MaxWidth { get; set; }
\r
84 /// Gets or sets MaxHeight.
\r
86 public int MaxHeight { get; set; }
\r
89 /// Gets or sets CropValues.
\r
91 public string CropValues { get; set; }
\r
94 /// Gets or sets CropTop.
\r
96 public string CropTop { get; set; }
\r
99 /// Gets or sets CropBottom.
\r
101 public string CropBottom { get; set; }
\r
104 /// Gets or sets CropLeft.
\r
106 public string CropLeft { get; set; }
\r
109 /// Gets or sets CropRight.
\r
111 public string CropRight { get; set; }
\r
114 /// Gets or sets AnamorphicMode.
\r
116 public int AnamorphicMode { get; set; }
\r
119 /// Gets or sets a value indicating whether KeepDisplayAsect.
\r
121 public bool KeepDisplayAsect { get; set; }
\r
124 /// Gets or sets DisplayWidthValue.
\r
126 public double DisplayWidthValue { get; set; }
\r
129 /// Gets or sets PixelAspectWidth.
\r
131 public int PixelAspectWidth { get; set; }
\r
134 /// Gets or sets PixelAspectHeight.
\r
136 public int PixelAspectHeight { get; set; }
\r
139 /// Gets or sets AnamorphicModulus.
\r
141 public int AnamorphicModulus { get; set; }
\r
144 #region Video Filters
\r
147 /// Gets or sets DeTelecine.
\r
149 public string DeTelecine { get; set; }
\r
152 /// Gets or sets DeBlock.
\r
154 public int DeBlock { get; set; }
\r
157 /// Gets or sets DeInterlace.
\r
159 public string DeInterlace { get; set; }
\r
162 /// Gets or sets DeNoise.
\r
164 public string DeNoise { get; set; }
\r
167 /// Gets or sets Decomb.
\r
169 public string Decomb { get; set; }
\r
172 #region Video Settings
\r
174 /// Gets or sets VideoEncoder.
\r
176 public string VideoEncoder { get; set; }
\r
179 /// Gets or sets a value indicating whether Grayscale.
\r
181 public bool Grayscale { get; set; }
\r
184 /// Gets or sets a value indicating whether TwoPass.
\r
186 public bool TwoPass { get; set; }
\r
189 /// Gets or sets a value indicating whether TurboFirstPass.
\r
191 public bool TurboFirstPass { get; set; }
\r
194 /// Gets or sets VideoFramerate.
\r
196 public string VideoFramerate { get; set; }
\r
199 /// Gets or sets a value indicating whether Pfr.
\r
201 public bool Pfr { get; set; }
\r
204 /// Gets or sets AverageVideoBitrate.
\r
206 public string AverageVideoBitrate { get; set; }
\r
209 /// Gets or sets VideoTargetSize.
\r
211 public string VideoTargetSize { get; set; }
\r
214 /// Gets or sets VideoQuality.
\r
216 public float VideoQuality { get; set; }
\r
219 #region Audio Settings
\r
222 /// Gets or sets AudioInformation.
\r
224 public ArrayList AudioInformation { get; set; }
\r
227 /// Gets or sets Subtitles.
\r
229 public string Subtitles { get; set; }
\r
232 /// Gets or sets a value indicating whether ForcedSubtitles.
\r
234 public bool ForcedSubtitles { get; set; }
\r
239 /// Gets or sets a value indicating whether ChapterMarkers.
\r
241 public bool ChapterMarkers { get; set; }
\r
244 /// Gets or sets H264Query.
\r
246 public string H264Query { get; set; }
\r
249 /// Gets or sets a value indicating whether Verbose.
\r
251 public bool Verbose { get; set; }
\r
254 #region Preset Information
\r
257 /// Gets or sets PresetBuildNumber.
\r
259 public int PresetBuildNumber { get; set; }
\r
262 /// Gets or sets PresetDescription.
\r
264 public string PresetDescription { get; set; }
\r
267 /// Gets or sets PresetName.
\r
269 public string PresetName { get; set; }
\r
272 /// Gets or sets Type.
\r
274 public string Type { get; set; }
\r
277 /// Gets or sets a value indicating whether UsesMaxPictureSettings.
\r
279 public bool UsesMaxPictureSettings { get; set; }
\r
282 /// Gets or sets a value indicating whether UsesPictureFilters.
\r
284 public bool UsesPictureFilters { get; set; }
\r
287 /// Gets or sets a value indicating whether UsesPictureSettings.
\r
289 public bool UsesPictureSettings { get; set; }
\r
295 /// Takes in a query which can be in any order and parses it.
\r
296 /// All varibles are then set so they can be used elsewhere.
\r
298 /// <param name="input">A ClI Query</param>
\r
299 /// <returns>A Parsed Query</returns>
\r
300 public static QueryParser Parse(string input)
\r
302 var thisQuery = new QueryParser();
\r
304 #region Regular Expressions
\r
307 Match title = Regex.Match(input, @"-t ([0-9]*)");
\r
308 Match chapters = Regex.Match(input, @"-c ([0-9-]*)");
\r
311 Match format = Regex.Match(input, @"-f ([a-z0-9a-z0-9a-z0-9]*)");
\r
312 Match grayscale = Regex.Match(input, @" -g");
\r
313 Match largerMp4 = Regex.Match(input, @" -4");
\r
314 Match ipodAtom = Regex.Match(input, @" -I");
\r
316 // Picture Settings Tab
\r
317 Match width = Regex.Match(input, @"-w ([0-9]*)");
\r
318 Match height = Regex.Match(input, @"-l ([0-9]*)");
\r
319 Match maxWidth = Regex.Match(input, @"-X ([0-9]*)");
\r
320 Match maxHeight = Regex.Match(input, @"-Y ([0-9]*)");
\r
321 Match crop = Regex.Match(input, @"--crop ([0-9]*):([0-9]*):([0-9]*):([0-9]*)");
\r
323 Match looseAnamorphic = Regex.Match(input, @"--loose-anamorphic");
\r
324 Match strictAnamorphic = Regex.Match(input, @"--strict-anamorphic");
\r
325 Match customAnamorphic = Regex.Match(input, @"--custom-anamorphic");
\r
327 Match keepDisplayAsect = Regex.Match(input, @"--keep-display-aspect");
\r
328 Match displayWidth = Regex.Match(input, @"--display-width ([0-9]*)");
\r
329 Match pixelAspect = Regex.Match(input, @"--pixel-aspect ([0-9]*):([0-9]*)");
\r
330 Match modulus = Regex.Match(input, @"--modulus ([0-9]*)");
\r
332 // Picture Settings - Filters
\r
333 Match decomb = Regex.Match(input, @" --decomb");
\r
334 Match decombValue = Regex.Match(input, @" --decomb=\""([a-zA-Z0-9.:]*)\""");
\r
335 Match deinterlace = Regex.Match(input, @"--deinterlace=\""([a-zA-Z0-9.:]*)\""");
\r
336 Match denoise = Regex.Match(input, @"--denoise=\""([a-zA-Z0-9.:]*)\""");
\r
337 Match deblock = Regex.Match(input, @"--deblock=([0-9:]*)");
\r
338 Match detelecine = Regex.Match(input, @"--detelecine");
\r
339 Match detelecineValue = Regex.Match(input, @" --detelecine=\""([a-zA-Z0-9.:]*)\""");
\r
341 // Video Settings Tab
\r
342 Match videoEncoder = Regex.Match(input, @"-e ([a-zA-Z0-9]*)");
\r
343 Match videoFramerate = Regex.Match(input, @"-r ([0-9.]*)");
\r
344 Match videoBitrate = Regex.Match(input, @"-b ([0-9]*)");
\r
345 Match videoQuality = Regex.Match(input, @"-q ([0-9.]*)");
\r
346 Match videoFilesize = Regex.Match(input, @"-S ([0-9.]*)");
\r
347 Match twoPass = Regex.Match(input, @" -2");
\r
348 Match turboFirstPass = Regex.Match(input, @" -T");
\r
349 Match optimizeMP4 = Regex.Match(input, @" -O");
\r
350 Match pfr = Regex.Match(input, @" --pfr");
\r
352 // Audio Settings Tab
\r
353 Match noAudio = Regex.Match(input, @"-a none");
\r
354 Match audioTracks = Regex.Match(input, @"-a ([0-9,]*)");
\r
355 Match audioTrackMixes = Regex.Match(input, @"-6 ([0-9a-zA-Z,]*)");
\r
356 Match audioEncoders = Regex.Match(input, @"-E ([a-zA-Z0-9+,]*)");
\r
357 Match audioBitrates = Regex.Match(input, @"-B ([0-9a-zA-Z,]*)"); // Auto = a-z
\r
358 Match audioSampleRates = Regex.Match(input, @"-R ([0-9a-zA-Z.,]*)"); // Auto = a-z
\r
359 Match drcValues = Regex.Match(input, @"-D ([0-9.,]*)");
\r
361 Match subtitles = Regex.Match(input, @"-s ([0-9a-zA-Z]*)");
\r
362 Match subScan = Regex.Match(input, @" -U");
\r
363 Match forcedSubtitles = Regex.Match(input, @" -F");
\r
366 Match chapterMarkers = Regex.Match(input, @" -m");
\r
367 Match chapterMarkersFileMode = Regex.Match(input, @"--markers");
\r
370 Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");
\r
373 Match verbose = Regex.Match(input, @" -v");
\r
377 #region Set Varibles
\r
384 thisQuery.Title = int.Parse(title.ToString().Replace("-t ", string.Empty));
\r
386 if (chapters.Success)
\r
388 string[] actTitles = chapters.ToString().Replace("-c ", string.Empty).Split('-');
\r
389 thisQuery.ChapterStart = int.Parse(actTitles[0]);
\r
390 if (actTitles.Length > 1)
\r
392 thisQuery.ChapterFinish = int.Parse(actTitles[1]);
\r
395 if ((thisQuery.ChapterStart == 1) && (thisQuery.ChapterFinish == 0))
\r
396 thisQuery.ChapterFinish = thisQuery.ChapterStart;
\r
401 #region Output Settings
\r
403 if (format.Success)
\r
404 thisQuery.Format = format.ToString().Replace("-f ", string.Empty);
\r
405 thisQuery.LargeMP4 = largerMp4.Success;
\r
406 thisQuery.IpodAtom = ipodAtom.Success;
\r
407 thisQuery.OptimizeMP4 = optimizeMP4.Success;
\r
411 #region Picture Tab
\r
414 thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", string.Empty));
\r
416 if (height.Success)
\r
417 thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", string.Empty));
\r
419 if (maxWidth.Success)
\r
420 thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", string.Empty));
\r
422 if (maxHeight.Success)
\r
423 thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", string.Empty));
\r
427 thisQuery.CropValues = crop.ToString().Replace("--crop ", string.Empty);
\r
428 string[] actCropValues = thisQuery.CropValues.Split(':');
\r
429 thisQuery.CropTop = actCropValues[0];
\r
430 thisQuery.CropBottom = actCropValues[1];
\r
431 thisQuery.CropLeft = actCropValues[2];
\r
432 thisQuery.CropRight = actCropValues[3];
\r
435 if (strictAnamorphic.Success)
\r
436 thisQuery.AnamorphicMode = 1;
\r
437 else if (looseAnamorphic.Success)
\r
438 thisQuery.AnamorphicMode = 2;
\r
439 else if (customAnamorphic.Success)
\r
440 thisQuery.AnamorphicMode = 3;
\r
442 thisQuery.AnamorphicMode = 0;
\r
444 thisQuery.KeepDisplayAsect = keepDisplayAsect.Success;
\r
446 if (displayWidth.Success)
\r
447 thisQuery.DisplayWidthValue =
\r
448 double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", string.Empty));
\r
450 if (pixelAspect.Success)
\r
451 thisQuery.PixelAspectWidth = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", string.Empty));
\r
453 if (pixelAspect.Success && pixelAspect.Groups.Count >= 3)
\r
454 thisQuery.PixelAspectHeight = int.Parse(pixelAspect.Groups[2].Value.Replace("--pixel-aspect ", string.Empty));
\r
456 if (modulus.Success)
\r
457 thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", string.Empty));
\r
463 thisQuery.Decomb = "Off";
\r
464 if (decomb.Success)
\r
466 thisQuery.Decomb = "Default";
\r
467 if (decombValue.Success)
\r
468 thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", string.Empty).Replace("\"", string.Empty);
\r
471 thisQuery.DeInterlace = "Off";
\r
472 if (deinterlace.Success)
\r
474 thisQuery.DeInterlace = deinterlace.ToString().Replace("--deinterlace=", string.Empty).Replace("\"", string.Empty);
\r
475 thisQuery.DeInterlace =
\r
476 thisQuery.DeInterlace.Replace("fast", "Fast").Replace("slow", "Slow").Replace("slower", "Slower");
\r
477 thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("slowest", "Slowest");
\r
480 thisQuery.DeNoise = "Off";
\r
481 if (denoise.Success)
\r
483 thisQuery.DeNoise = denoise.ToString().Replace("--denoise=", string.Empty).Replace("\"", string.Empty);
\r
484 thisQuery.DeNoise =
\r
485 thisQuery.DeNoise.Replace("weak", "Weak").Replace("medium", "Medium").Replace("strong", "Strong");
\r
488 string deblockValue = string.Empty;
\r
489 thisQuery.DeBlock = 0;
\r
490 if (deblock.Success)
\r
491 deblockValue = deblock.ToString().Replace("--deblock=", string.Empty);
\r
494 if (deblockValue != string.Empty)
\r
495 int.TryParse(deblockValue, out dval);
\r
496 thisQuery.DeBlock = dval;
\r
498 thisQuery.DeTelecine = "Off";
\r
499 if (detelecine.Success)
\r
501 thisQuery.DeTelecine = "Default";
\r
502 if (detelecineValue.Success)
\r
503 thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", string.Empty).Replace("\"", string.Empty);
\r
508 #region Video Settings Tab
\r
510 string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", string.Empty);
\r
511 switch (videoEncoderConvertion)
\r
514 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
517 videoEncoderConvertion = "H.264 (x264)";
\r
520 videoEncoderConvertion = "VP3 (Theora)";
\r
523 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
526 thisQuery.VideoEncoder = videoEncoderConvertion;
\r
527 thisQuery.VideoFramerate = videoFramerate.Success
\r
528 ? videoFramerate.ToString().Replace("-r ", string.Empty)
\r
529 : "Same as source";
\r
531 thisQuery.Pfr = pfr.Success;
\r
532 thisQuery.Grayscale = grayscale.Success;
\r
533 thisQuery.TwoPass = twoPass.Success;
\r
534 thisQuery.TurboFirstPass = turboFirstPass.Success;
\r
536 if (videoBitrate.Success)
\r
537 thisQuery.AverageVideoBitrate = videoBitrate.ToString().Replace("-b ", string.Empty);
\r
538 if (videoFilesize.Success)
\r
539 thisQuery.VideoTargetSize = videoFilesize.ToString().Replace("-S ", string.Empty);
\r
541 if (videoQuality.Success)
\r
543 float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", string.Empty), Culture);
\r
544 thisQuery.VideoQuality = qConvert;
\r
547 thisQuery.VideoQuality = -1;
\r
553 // Find out how many tracks we need to add by checking how many encoders or audio tracks are selected.
\r
554 int encoderCount = 0;
\r
555 if (audioEncoders.Success)
\r
557 string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
\r
558 encoderCount = audioDataCounters.Length;
\r
561 // Get the data from the regular expression results
\r
562 string[] trackData = null;
\r
563 string[] trackMixes = null;
\r
564 string[] trackEncoders = null;
\r
565 string[] trackBitrates = null;
\r
566 string[] trackSamplerates = null;
\r
567 string[] trackDRCvalues = null;
\r
569 if (audioTracks.Success)
\r
570 trackData = audioTracks.ToString().Replace("-a ", string.Empty).Split(',');
\r
571 if (audioTrackMixes.Success)
\r
572 trackMixes = audioTrackMixes.ToString().Replace("-6 ", string.Empty).Split(',');
\r
573 if (audioEncoders.Success)
\r
574 trackEncoders = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');
\r
575 if (audioBitrates.Success)
\r
576 trackBitrates = audioBitrates.ToString().Replace("-B ", string.Empty).Split(',');
\r
577 if (audioSampleRates.Success)
\r
578 trackSamplerates = audioSampleRates.ToString().Replace("-R ", string.Empty).Split(',');
\r
579 if (drcValues.Success)
\r
580 trackDRCvalues = drcValues.ToString().Replace("-D ", string.Empty).Split(',');
\r
582 // Create new Audio Track Classes and store them in the ArrayList
\r
583 ArrayList allAudioTrackInfo = new ArrayList();
\r
584 for (int x = 0; x < encoderCount; x++)
\r
586 AudioTrack track = new AudioTrack();
\r
587 if (trackData != null)
\r
588 if (trackData.Length >= (x + 1)) // Audio Track
\r
589 track.Track = trackData[x].Trim();
\r
591 if (trackMixes != null)
\r
592 if (trackMixes.Length >= (x + 1)) // Audio Mix
\r
593 track.MixDown = GetMixDown(trackMixes[x].Trim());
\r
595 if (trackEncoders != null)
\r
596 if (trackEncoders.Length >= (x + 1)) // Audio Mix
\r
597 track.Encoder = GetAudioEncoder(trackEncoders[x].Trim());
\r
599 if (trackBitrates != null)
\r
600 if (trackBitrates.Length >= (x + 1)) // Audio Encoder
\r
601 track.Bitrate = trackBitrates[x].Trim() == "auto" ? "Auto" : trackBitrates[x].Trim();
\r
603 if (trackSamplerates != null)
\r
604 if (trackSamplerates.Length >= (x + 1)) // Audio SampleRate
\r
605 track.SampleRate = trackSamplerates[x].Trim() == "0" ? "Auto" : trackSamplerates[x].Trim();
\r
607 if (trackDRCvalues != null)
\r
608 if (trackDRCvalues.Length >= (x + 1)) // Audio DRC Values
\r
609 track.DRC = trackDRCvalues[x].Trim();
\r
611 allAudioTrackInfo.Add(track);
\r
613 thisQuery.AudioInformation = allAudioTrackInfo;
\r
616 if (subtitles.Success)
\r
617 thisQuery.Subtitles = subtitles.ToString().Replace("-s ", string.Empty);
\r
619 thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";
\r
621 thisQuery.ForcedSubtitles = forcedSubtitles.Success;
\r
625 #region Chapters Tab
\r
627 if (chapterMarkersFileMode.Success || chapterMarkers.Success)
\r
628 thisQuery.ChapterMarkers = true;
\r
632 #region H.264 and other
\r
635 thisQuery.H264Query = x264.ToString().Replace("-x ", string.Empty);
\r
637 thisQuery.Verbose = verbose.Success;
\r
641 catch (Exception exc)
\r
643 Main.ShowExceptiowWindow("An error has occured in the Query Parser.", exc.ToString());
\r
652 /// Get the GUI equiv to a CLI mixdown
\r
654 /// <param name="mixdown">The Audio Mixdown</param>
\r
655 /// <returns>The GUI representation of the mixdown</returns>
\r
656 private static string GetMixDown(string mixdown)
\r
658 switch (mixdown.Trim())
\r
665 return "Dolby Surround";
\r
667 return "Dolby Pro Logic II";
\r
669 return "6 Channel Discrete";
\r
671 return "Automatic";
\r
676 /// Get the GUI equiv to a CLI audio encoder
\r
678 /// <param name="audioEnc">The Audio Encoder</param>
\r
679 /// <returns>The GUI representation of that audio encoder</returns>
\r
680 private static string GetAudioEncoder(string audioEnc)
\r
685 return "AAC (faac)";
\r
687 return "MP3 (lame)";
\r
689 return "Vorbis (vorbis)";
\r
691 return "AC3 Passthru";
\r
693 return "DTS Passthru";
\r
695 return "AAC (faac)";
\r