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
8 using System.Globalization;
\r
9 using System.Text.RegularExpressions;
\r
10 using System.Windows.Forms;
\r
11 using System.Collections;
\r
12 using Handbrake.Model;
\r
14 namespace Handbrake.Functions
\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 Boolean LargeMP4 { get; set; }
\r
30 public Boolean IpodAtom { get; set; }
\r
31 public Boolean 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 Boolean 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 Boolean Grayscale { get; set; }
\r
60 public Boolean TwoPass { get; set; }
\r
61 public Boolean 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 Boolean ForcedSubtitles { get; set; }
\r
73 public Boolean ChapterMarkers { get; set; }
\r
76 public string H264Query { get; set; }
\r
77 public Boolean 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 Boolean UsesMaxPictureSettings { get; set; }
\r
85 public Boolean UsesPictureFilters { get; set; }
\r
86 public Boolean 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 ", ""));
\r
181 if (chapters.Success)
\r
183 string[] actTitles = chapters.ToString().Replace("-c ", "").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
195 #region Output Settings
\r
197 if (format.Success)
\r
198 thisQuery.Format = format.ToString().Replace("-f ", "");
\r
199 thisQuery.LargeMP4 = largerMp4.Success;
\r
200 thisQuery.IpodAtom = ipodAtom.Success;
\r
201 thisQuery.OptimizeMP4 = optimizeMP4.Success;
\r
205 #region Picture Tab
\r
208 thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", ""));
\r
210 if (height.Success)
\r
211 thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", ""));
\r
213 if (maxWidth.Success)
\r
214 thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", ""));
\r
216 if (maxHeight.Success)
\r
217 thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", ""));
\r
221 thisQuery.CropValues = crop.ToString().Replace("--crop ", "");
\r
222 string[] actCropValues = thisQuery.CropValues.Split(':');
\r
223 thisQuery.CropTop = actCropValues[0];
\r
224 thisQuery.CropBottom = actCropValues[1];
\r
225 thisQuery.CropLeft = actCropValues[2];
\r
226 thisQuery.CropRight = actCropValues[3];
\r
229 if (strictAnamorphic.Success)
\r
230 thisQuery.AnamorphicMode = 1;
\r
231 else if (looseAnamorphic.Success)
\r
232 thisQuery.AnamorphicMode = 2;
\r
233 else if (customAnamorphic.Success)
\r
234 thisQuery.AnamorphicMode = 3;
\r
236 thisQuery.AnamorphicMode = 0;
\r
238 thisQuery.KeepDisplayAsect = keepDisplayAsect.Success;
\r
240 if (displayWidth.Success)
\r
241 thisQuery.DisplayWidthValue = double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", ""));
\r
243 if (pixelAspect.Success)
\r
244 thisQuery.PixelAspectWidth = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", ""));
\r
246 if (pixelAspect.Success && pixelAspect.Groups.Count >=3)
\r
247 thisQuery.PixelAspectHeight = int.Parse(pixelAspect.Groups[2].Value.Replace("--pixel-aspect ", ""));
\r
249 if (modulus.Success)
\r
250 thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", ""));
\r
257 thisQuery.Decomb = "Off";
\r
258 if (decomb.Success)
\r
260 thisQuery.Decomb = "Default";
\r
261 if (decombValue.Success)
\r
262 thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", "").Replace("\"", "");
\r
265 thisQuery.DeInterlace = "Off";
\r
266 if (deinterlace.Success)
\r
268 thisQuery.DeInterlace = deinterlace.ToString().Replace("--deinterlace=", "").Replace("\"", "");
\r
269 thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("fast", "Fast").Replace("slow", "Slow").Replace("slower", "Slower");
\r
270 thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("slowest", "Slowest");
\r
273 thisQuery.DeNoise = "Off";
\r
274 if (denoise.Success)
\r
276 thisQuery.DeNoise = denoise.ToString().Replace("--denoise=", "").Replace("\"", "");
\r
277 thisQuery.DeNoise = thisQuery.DeNoise.Replace("weak", "Weak").Replace("medium", "Medium").Replace("strong", "Strong");
\r
280 string deblockValue = "";
\r
281 thisQuery.DeBlock = 0;
\r
282 if (deblock.Success)
\r
283 deblockValue = deblock.ToString().Replace("--deblock=", "");
\r
286 if (deblockValue != "")
\r
287 int.TryParse(deblockValue, out dval);
\r
288 thisQuery.DeBlock = dval;
\r
290 thisQuery.DeTelecine = "Off";
\r
291 if (detelecine.Success)
\r
293 thisQuery.DeTelecine = "Default";
\r
294 if (detelecineValue.Success)
\r
295 thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", "").Replace("\"", "");
\r
300 #region Video Settings Tab
\r
302 string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", "");
\r
303 switch (videoEncoderConvertion)
\r
306 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
309 videoEncoderConvertion = "H.264 (x264)";
\r
312 videoEncoderConvertion = "VP3 (Theora)";
\r
315 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
318 thisQuery.VideoEncoder = videoEncoderConvertion;
\r
319 thisQuery.VideoFramerate = videoFramerate.Success ? videoFramerate.ToString().Replace("-r ", "") : "Same as source";
\r
320 thisQuery.Grayscale = grayscale.Success;
\r
321 thisQuery.TwoPass = twoPass.Success;
\r
322 thisQuery.TurboFirstPass = turboFirstPass.Success;
\r
324 if (videoBitrate.Success)
\r
325 thisQuery.AverageVideoBitrate = videoBitrate.ToString().Replace("-b ", "");
\r
326 if (videoFilesize.Success)
\r
327 thisQuery.VideoTargetSize = videoFilesize.ToString().Replace("-S ", "");
\r
329 if (videoQuality.Success)
\r
331 float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", ""), Culture);
\r
332 thisQuery.VideoQuality = qConvert;
\r
335 thisQuery.VideoQuality = -1;
\r
339 // Find out how many tracks we need to add by checking how many encoders or audio tracks are selected.
\r
340 int encoderCount = 0;
\r
341 if (audioEncoders.Success)
\r
343 string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", "").Split(',');
\r
344 encoderCount = audioDataCounters.Length;
\r
347 // Get the data from the regular expression results
\r
348 string[] trackData = null;
\r
349 string[] trackMixes = null;
\r
350 string[] trackEncoders = null;
\r
351 string[] trackBitrates = null;
\r
352 string[] trackSamplerates = null;
\r
353 string[] trackDRCvalues = null;
\r
355 if (audioTracks.Success)
\r
356 trackData = audioTracks.ToString().Replace("-a ", "").Split(',');
\r
357 if (audioTrackMixes.Success)
\r
358 trackMixes = audioTrackMixes.ToString().Replace("-6 ", "").Split(',');
\r
359 if (audioEncoders.Success)
\r
360 trackEncoders = audioEncoders.ToString().Replace("-E ", "").Split(',');
\r
361 if (audioBitrates.Success)
\r
362 trackBitrates = audioBitrates.ToString().Replace("-B ", "").Split(',');
\r
363 if (audioSampleRates.Success)
\r
364 trackSamplerates = audioSampleRates.ToString().Replace("-R ", "").Split(',');
\r
365 if (drcValues.Success)
\r
366 trackDRCvalues = drcValues.ToString().Replace("-D ", "").Split(',');
\r
368 // Create new Audio Track Classes and store them in the ArrayList
\r
369 ArrayList allAudioTrackInfo = new ArrayList();
\r
370 for (int x = 0; x < encoderCount; x++)
\r
372 AudioTrack track = new AudioTrack();
\r
373 if (trackData != null)
\r
374 if (trackData.Length >= (x + 1)) // Audio Track
\r
375 track.Track = trackData[x].Trim();
\r
377 if (trackMixes != null)
\r
378 if (trackMixes.Length >= (x + 1)) // Audio Mix
\r
379 track.MixDown = GetMixDown(trackMixes[x].Trim());
\r
381 if (trackEncoders != null)
\r
382 if (trackEncoders.Length >= (x + 1)) // Audio Mix
\r
383 track.Encoder = GetAudioEncoder(trackEncoders[x].Trim());
\r
385 if (trackBitrates != null)
\r
386 if (trackBitrates.Length >= (x + 1)) // Audio Encoder
\r
387 track.Bitrate = trackBitrates[x].Trim() == "auto" ? "Auto" : trackBitrates[x].Trim();
\r
389 if (trackSamplerates != null)
\r
390 if (trackSamplerates.Length >= (x + 1)) // Audio SampleRate
\r
391 track.SampleRate = trackSamplerates[x].Trim() == "0" ? "Auto" : trackSamplerates[x].Trim();
\r
393 if (trackDRCvalues != null)
\r
394 if (trackDRCvalues.Length >= (x + 1)) // Audio DRC Values
\r
395 track.DRC = trackDRCvalues[x].Trim();
\r
397 allAudioTrackInfo.Add(track);
\r
399 thisQuery.AudioInformation = allAudioTrackInfo;
\r
402 if (subtitles.Success)
\r
403 thisQuery.Subtitles = subtitles.ToString().Replace("-s ", "");
\r
405 thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";
\r
407 thisQuery.ForcedSubtitles = forcedSubtitles.Success;
\r
410 #region Chapters Tab
\r
411 if (chapterMarkersFileMode.Success || chapterMarkers.Success)
\r
412 thisQuery.ChapterMarkers = true;
\r
415 #region H.264 and other
\r
418 thisQuery.H264Query = x264.ToString().Replace("-x ", "");
\r
420 thisQuery.Verbose = verbose.Success;
\r
424 catch (Exception exc)
\r
427 "An error has occured in the Query Parser.\n\n" +
\r
428 exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
437 /// Get the GUI equiv to a CLI mixdown
\r
439 /// <param name="mixdown"></param>
\r
440 /// <returns></returns>
\r
441 private static string GetMixDown(string mixdown)
\r
443 switch (mixdown.Trim())
\r
450 return "Dolby Surround";
\r
452 return "Dolby Pro Logic II";
\r
454 return "6 Channel Discrete";
\r
456 return "Automatic";
\r
461 /// Get the GUI equiv to a CLI audio encoder
\r
463 /// <param name="audioEnc"></param>
\r
464 /// <returns></returns>
\r
465 private static string GetAudioEncoder(string audioEnc)
\r
470 return "AAC (faac)";
\r
472 return "MP3 (lame)";
\r
474 return "Vorbis (vorbis)";
\r
476 return "AC3 Passthru";
\r
478 return "DTS Passthru";
\r
480 return "AAC (faac)";
\r