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
13 namespace Handbrake.Functions
\r
15 internal class QueryParser
\r
17 private static readonly CultureInfo Culture = new CultureInfo("en-US", false);
\r
22 public int DVDTitle { get; private set; }
\r
23 public int DVDChapterStart { get; private set; }
\r
24 public int DVDChapterFinish { get; private set; }
\r
27 #region Output Settings
\r
28 public string Format { get; private set; }
\r
29 public Boolean LargeMP4 { get; private set; }
\r
30 public Boolean IpodAtom { get; private set; }
\r
31 public Boolean OptimizeMP4 { get; private set; }
\r
34 #region Picture Settings
\r
35 public int Width { get; private set; }
\r
36 public int Height { get; private set; }
\r
37 public int MaxWidth { get; private set; }
\r
38 public int MaxHeight { get; private set; }
\r
39 public string CropValues { get; private set; }
\r
40 public string CropTop { get; private set; }
\r
41 public string CropBottom { get; private set; }
\r
42 public string CropLeft { get; private set; }
\r
43 public string CropRight { get; private set; }
\r
44 public int AnamorphicMode { get; private set; }
\r
45 public Boolean keepDisplayAsect { get; private set; }
\r
46 public double displayWidthValue { get; private set; }
\r
47 public int pixelAspectWidth { get; private set; }
\r
48 public int pixelAspectHeight { get; private set; }
\r
49 public int AnamorphicModulus { get; private set; }
\r
52 #region Video Filters
\r
53 public string DeTelecine { get; private set; }
\r
54 public int DeBlock { get; private set; }
\r
55 public string DeInterlace { get; private set; }
\r
56 public string DeNoise { get; private set; }
\r
57 public string Decomb { get; private set; }
\r
60 #region Video Settings
\r
61 public string VideoEncoder { get; private set; }
\r
62 public Boolean Grayscale { get; private set; }
\r
63 public Boolean TwoPass { get; private set; }
\r
64 public Boolean TurboFirstPass { get; private set; }
\r
65 public string VideoFramerate { get; private set; }
\r
66 public string AverageVideoBitrate { get; private set; }
\r
67 public string VideoTargetSize { get; private set; }
\r
68 public float VideoQuality { get; private set; }
\r
71 #region Audio Settings
\r
72 public ArrayList AudioInformation { get; private set; }
\r
73 public string Subtitles { get; private set; }
\r
74 public Boolean ForcedSubtitles { get; private set; }
\r
77 #region Chapter Markers
\r
78 public Boolean ChapterMarkers { get; private set; }
\r
82 public string H264Query { get; private set; }
\r
83 public Boolean Verbose { get; private set; }
\r
89 /// Takes in a query which can be in any order and parses it.
\r
90 /// All varibles are then set so they can be used elsewhere.
\r
92 /// <param name="input">A ClI Query</param>
\r
93 /// <returns>A Parsed Query</returns>
\r
94 public static QueryParser Parse(String input)
\r
96 var thisQuery = new QueryParser();
\r
98 #region Regular Expressions
\r
101 Match title = Regex.Match(input, @"-t ([0-9]*)");
\r
102 Match chapters = Regex.Match(input, @"-c ([0-9-]*)");
\r
105 Match format = Regex.Match(input, @"-f ([a-z0-9a-z0-9a-z0-9]*)");
\r
106 Match grayscale = Regex.Match(input, @" -g");
\r
107 Match largerMp4 = Regex.Match(input, @" -4");
\r
108 Match ipodAtom = Regex.Match(input, @" -I");
\r
110 //Picture Settings Tab
\r
111 Match width = Regex.Match(input, @"-w ([0-9]*)");
\r
112 Match height = Regex.Match(input, @"-l ([0-9]*)");
\r
113 Match maxWidth = Regex.Match(input, @"-X ([0-9]*)");
\r
114 Match maxHeight = Regex.Match(input, @"-Y ([0-9]*)");
\r
115 Match crop = Regex.Match(input, @"--crop ([0-9]*):([0-9]*):([0-9]*):([0-9]*)");
\r
117 Match looseAnamorphic = Regex.Match(input, @"--loose-anamorphic");
\r
118 Match strictAnamorphic = Regex.Match(input, @"--strict-anamorphic");
\r
119 Match customAnamorphic = Regex.Match(input, @"--custom-anamorphic");
\r
121 Match keepDisplayAsect = Regex.Match(input, @"--keep-display-aspect");
\r
122 Match displayWidth = Regex.Match(input, @"--display-width ([0-9*])");
\r
123 Match pixelAspect = Regex.Match(input, @"--pixel-aspect ([0-9]*):([0-9]*)");
\r
124 Match modulus = Regex.Match(input, @"--modulus ([0-9*])");
\r
126 // Picture Settings - Filters
\r
127 Match decomb = Regex.Match(input, @" --decomb");
\r
128 Match decombValue = Regex.Match(input, @" --decomb=\""([a-zA-Z0-9.:]*)\""");
\r
129 Match deinterlace = Regex.Match(input, @"--deinterlace=\""([a-zA-Z0-9.:]*)\""");
\r
130 Match denoise = Regex.Match(input, @"--denoise=\""([a-zA-Z0-9.:]*)\""");
\r
131 Match deblock = Regex.Match(input, @"--deblock=([0-9:]*)");
\r
132 Match detelecine = Regex.Match(input, @"--detelecine");
\r
133 Match detelecineValue = Regex.Match(input, @" --detelecine=\""([a-zA-Z0-9.:]*)\""");
\r
135 //Video Settings Tab
\r
136 Match videoEncoder = Regex.Match(input, @"-e ([a-zA-Z0-9]*)");
\r
137 Match videoFramerate = Regex.Match(input, @"-r ([0-9.]*)");
\r
138 Match videoBitrate = Regex.Match(input, @"-b ([0-9]*)");
\r
139 Match videoQuality = Regex.Match(input, @"-q ([0-9.]*)");
\r
140 Match videoFilesize = Regex.Match(input, @"-S ([0-9.]*)");
\r
141 Match twoPass = Regex.Match(input, @" -2");
\r
142 Match turboFirstPass = Regex.Match(input, @" -T");
\r
143 Match optimizeMP4 = Regex.Match(input, @" -O");
\r
145 //Audio Settings Tab
\r
146 Match noAudio = Regex.Match(input, @"-a none");
\r
147 Match audioTracks = Regex.Match(input, @"-a ([0-9,]*)");
\r
148 Match audioTrackMixes = Regex.Match(input, @"-6 ([0-9a-zA-Z,]*)");
\r
149 Match audioEncoders = Regex.Match(input, @"-E ([a-zA-Z0-9+,]*)");
\r
150 Match audioBitrates = Regex.Match(input, @"-B ([0-9a-zA-Z,]*)"); // Auto = a-z
\r
151 Match audioSampleRates = Regex.Match(input, @"-R ([0-9a-zA-Z.,]*)"); // Auto = a-z
\r
152 Match drcValues = Regex.Match(input, @"-D ([0-9.,]*)");
\r
154 Match subtitles = Regex.Match(input, @"-s ([0-9a-zA-Z]*)");
\r
155 Match subScan = Regex.Match(input, @" -U");
\r
156 Match forcedSubtitles = Regex.Match(input, @" -F");
\r
159 Match chapterMarkers = Regex.Match(input, @" -m");
\r
160 Match chapterMarkersFileMode = Regex.Match(input, @"--markers");
\r
163 Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");
\r
166 Match verbose = Regex.Match(input, @" -v");
\r
170 #region Set Varibles
\r
177 thisQuery.DVDTitle = int.Parse(title.ToString().Replace("-t ", ""));
\r
179 if (chapters.Success)
\r
181 string[] actTitles = chapters.ToString().Replace("-c ", "").Split('-');
\r
182 thisQuery.DVDChapterStart = int.Parse(actTitles[0]);
\r
183 if (actTitles.Length > 1)
\r
185 thisQuery.DVDChapterFinish = int.Parse(actTitles[1]);
\r
188 if ((thisQuery.DVDChapterStart == 1) && (thisQuery.DVDChapterFinish == 0))
\r
189 thisQuery.DVDChapterFinish = thisQuery.DVDChapterStart;
\r
193 #region Output Settings
\r
195 if (format.Success)
\r
196 thisQuery.Format = format.ToString().Replace("-f ", "");
\r
197 thisQuery.LargeMP4 = largerMp4.Success;
\r
198 thisQuery.IpodAtom = ipodAtom.Success;
\r
199 thisQuery.OptimizeMP4 = optimizeMP4.Success;
\r
203 #region Picture Tab
\r
206 thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", ""));
\r
208 if (height.Success)
\r
209 thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", ""));
\r
211 if (maxWidth.Success)
\r
212 thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", ""));
\r
214 if (maxHeight.Success)
\r
215 thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", ""));
\r
219 thisQuery.CropValues = crop.ToString().Replace("--crop ", "");
\r
220 string[] actCropValues = thisQuery.CropValues.Split(':');
\r
221 thisQuery.CropTop = actCropValues[0];
\r
222 thisQuery.CropBottom = actCropValues[1];
\r
223 thisQuery.CropLeft = actCropValues[2];
\r
224 thisQuery.CropRight = actCropValues[3];
\r
227 if (strictAnamorphic.Success)
\r
228 thisQuery.AnamorphicMode = 1;
\r
229 else if (looseAnamorphic.Success)
\r
230 thisQuery.AnamorphicMode = 2;
\r
231 else if (customAnamorphic.Success)
\r
232 thisQuery.AnamorphicMode = 3;
\r
234 thisQuery.AnamorphicMode = 0;
\r
236 thisQuery.keepDisplayAsect = keepDisplayAsect.Success;
\r
238 if (displayWidth.Success)
\r
239 thisQuery.displayWidthValue = double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", ""));
\r
241 if (pixelAspect.Success)
\r
242 thisQuery.pixelAspectWidth = int.Parse(pixelAspect.Groups[0].Value.Replace("--pixel-aspect ", ""));
\r
244 if (pixelAspect.Success)
\r
245 thisQuery.pixelAspectHeight = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", ""));
\r
247 if (modulus.Success)
\r
248 thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", ""));
\r
255 thisQuery.Decomb = "Off";
\r
256 if (decomb.Success)
\r
258 thisQuery.Decomb = "Default";
\r
259 if (decombValue.Success)
\r
260 thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", "").Replace("\"", "");
\r
263 thisQuery.DeInterlace = "None";
\r
264 if (deinterlace.Success)
\r
266 thisQuery.DeInterlace = deinterlace.ToString().Replace("--deinterlace=", "").Replace("\"", "");
\r
267 thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("fast", "Fast").Replace("slow", "Slow").Replace("slower", "Slower");
\r
268 thisQuery.DeInterlace = thisQuery.DeInterlace.Replace("slowest", "Slowest");
\r
271 thisQuery.DeNoise = "None";
\r
272 if (denoise.Success)
\r
274 thisQuery.DeNoise = denoise.ToString().Replace("--denoise=", "").Replace("\"", "");
\r
275 thisQuery.DeNoise = thisQuery.DeNoise.Replace("weak", "Weak").Replace("medium", "Medium").Replace("strong", "Strong");
\r
278 string deblockValue = "";
\r
279 thisQuery.DeBlock = 0;
\r
280 if (deblock.Success)
\r
281 deblockValue = deblock.ToString().Replace("--deblock=", "");
\r
284 if (deblockValue != "")
\r
285 int.TryParse(deblockValue, out dval);
\r
286 thisQuery.DeBlock = dval;
\r
288 thisQuery.DeTelecine = "Off";
\r
289 if (detelecine.Success)
\r
291 thisQuery.DeTelecine = "Default";
\r
292 if (detelecineValue.Success)
\r
293 thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", "").Replace("\"", "");
\r
298 #region Video Settings Tab
\r
300 string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", "");
\r
301 switch (videoEncoderConvertion)
\r
304 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
307 videoEncoderConvertion = "H.264 (x264)";
\r
310 videoEncoderConvertion = "VP3 (Theora)";
\r
313 videoEncoderConvertion = "MPEG-4 (FFmpeg)";
\r
316 thisQuery.VideoEncoder = videoEncoderConvertion;
\r
317 thisQuery.VideoFramerate = videoFramerate.Success ? videoFramerate.ToString().Replace("-r ", "") : "Same as source";
\r
318 thisQuery.Grayscale = grayscale.Success;
\r
319 thisQuery.TwoPass = twoPass.Success;
\r
320 thisQuery.TurboFirstPass = turboFirstPass.Success;
\r
322 if (videoBitrate.Success)
\r
323 thisQuery.AverageVideoBitrate = videoBitrate.ToString().Replace("-b ", "");
\r
324 if (videoFilesize.Success)
\r
325 thisQuery.VideoTargetSize = videoFilesize.ToString().Replace("-S ", "");
\r
327 if (videoQuality.Success)
\r
329 float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", ""), Culture);
\r
330 thisQuery.VideoQuality = qConvert;
\r
335 // Find out how many tracks we need to add by checking how many encoders or audio tracks are selected.
\r
336 int encoderCount = 0;
\r
337 if (audioEncoders.Success)
\r
339 string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", "").Split(',');
\r
340 encoderCount = audioDataCounters.Length;
\r
343 // Get the data from the regular expression results
\r
344 string[] trackData = null;
\r
345 string[] trackMixes = null;
\r
346 string[] trackEncoders = null;
\r
347 string[] trackBitrates = null;
\r
348 string[] trackSamplerates = null;
\r
349 string[] trackDRCvalues = null;
\r
351 if (audioTracks.Success)
\r
352 trackData = audioTracks.ToString().Replace("-a ", "").Split(',');
\r
353 if (audioTrackMixes.Success)
\r
354 trackMixes = audioTrackMixes.ToString().Replace("-6 ", "").Split(',');
\r
355 if (audioEncoders.Success)
\r
356 trackEncoders = audioEncoders.ToString().Replace("-E ", "").Split(',');
\r
357 if (audioBitrates.Success)
\r
358 trackBitrates = audioBitrates.ToString().Replace("-B ", "").Split(',');
\r
359 if (audioSampleRates.Success)
\r
360 trackSamplerates = audioSampleRates.ToString().Replace("-R ", "").Split(',');
\r
361 if (drcValues.Success)
\r
362 trackDRCvalues = drcValues.ToString().Replace("-D ", "").Split(',');
\r
364 // Create new Audio Track Classes and store them in the ArrayList
\r
365 ArrayList AllAudioTrackInfo = new ArrayList();
\r
366 for (int x = 0; x < encoderCount; x++)
\r
368 AudioTrack track = new AudioTrack();
\r
369 if (trackData != null)
\r
370 if (trackData.Length >= (x + 1)) // Audio Track
\r
371 track.Track = trackData[x].Trim();
\r
373 if (trackMixes != null)
\r
374 if (trackMixes.Length >= (x + 1)) // Audio Mix
\r
375 track.MixDown = getMixDown(trackMixes[x].Trim());
\r
377 if (trackEncoders != null)
\r
378 if (trackEncoders.Length >= (x + 1)) // Audio Mix
\r
379 track.Encoder = getAudioEncoder(trackEncoders[x].Trim());
\r
381 if (trackBitrates != null)
\r
382 if (trackBitrates.Length >= (x + 1)) // Audio Encoder
\r
383 track.Bitrate = trackBitrates[x].Trim() == "auto" ? "Auto" : trackBitrates[x].Trim();
\r
385 if (trackSamplerates != null)
\r
386 if (trackSamplerates.Length >= (x + 1)) // Audio SampleRate
\r
387 track.SampleRate = trackSamplerates[x].Trim() == "0" ? "Auto" : trackSamplerates[x].Trim();
\r
389 if (trackDRCvalues != null)
\r
390 if (trackDRCvalues.Length >= (x + 1)) // Audio DRC Values
\r
391 track.DRC = trackDRCvalues[x].Trim();
\r
393 AllAudioTrackInfo.Add(track);
\r
395 thisQuery.AudioInformation = AllAudioTrackInfo;
\r
398 if (subtitles.Success)
\r
399 thisQuery.Subtitles = subtitles.ToString().Replace("-s ", "");
\r
401 thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";
\r
403 thisQuery.ForcedSubtitles = forcedSubtitles.Success;
\r
406 #region Chapters Tab
\r
407 if (chapterMarkersFileMode.Success || chapterMarkers.Success)
\r
408 thisQuery.ChapterMarkers = true;
\r
411 #region H.264 and other
\r
414 thisQuery.H264Query = x264.ToString().Replace("-x ", "");
\r
416 thisQuery.Verbose = verbose.Success;
\r
420 catch (Exception exc)
\r
423 "An error has occured in the Query Parser. Please report this error on the forum in the 'Windows' support section. \n\n" +
\r
424 exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
431 private static string getMixDown(string mixdown)
\r
433 switch (mixdown.Trim())
\r
440 return "Dolby Surround";
\r
442 return "Dolby Pro Logic II";
\r
444 return "6 Channel Discrete";
\r
446 return "Automatic";
\r
449 private static string getAudioEncoder(string audioEnc)
\r
454 return "AAC (faac)";
\r
456 return "MP3 (lame)";
\r
458 return "Vorbis (vorbis)";
\r
460 return "AC3 Passthru";
\r
462 return "DTS Passthru";
\r
464 return "AAC (faac)";
\r
469 public class AudioTrack
\r
471 public AudioTrack()
\r
474 Track = "Automatic";
\r
475 MixDown = "Automatic";
\r
476 SampleRate = "Auto";
\r
480 public string Track { get; set; }
\r
481 public string MixDown { get; set; }
\r
482 public string Encoder { get; set; }
\r
483 public string Bitrate { get; set; }
\r
484 public string SampleRate { get; set; }
\r
485 public string DRC { get; set; }
\r