OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / QueryParser.cs
1 /*  QueryParser.cs $\r
2         \r
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
6 \r
7 namespace Handbrake.Functions\r
8 {\r
9     using System;\r
10     using System.Collections;\r
11     using System.Globalization;\r
12     using System.Text.RegularExpressions;\r
13     using System.Windows.Forms;\r
14     using Model;\r
15 \r
16     public class QueryParser\r
17     {\r
18         private static readonly CultureInfo Culture = new CultureInfo("en-US", false);\r
19 \r
20         #region Varibles\r
21 \r
22         // Source\r
23         public int DVDTitle { get; set; }\r
24         public int DVDChapterStart { get; set; }\r
25         public int DVDChapterFinish { get; set; }\r
26 \r
27         // Output Settings\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
32 \r
33         // Picture Settings\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
49 \r
50         // Video Filters\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
56 \r
57         // Video Settings\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
66 \r
67         // Audio Settings\r
68         public ArrayList AudioInformation { get; set; }\r
69         public string Subtitles { get; set; }\r
70         public bool ForcedSubtitles { get; set; }\r
71 \r
72         // Chapter Markers\r
73         public bool ChapterMarkers { get; set; }\r
74 \r
75         // Other\r
76         public string H264Query { get; set; }\r
77         public bool Verbose { get; set; }\r
78 \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
87 \r
88         #endregion\r
89 \r
90         /// <summary>\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
93         /// </summary>\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
97         {\r
98             var thisQuery = new QueryParser();\r
99 \r
100             #region Regular Expressions\r
101 \r
102             // Source\r
103             Match title = Regex.Match(input, @"-t ([0-9]*)");\r
104             Match chapters = Regex.Match(input, @"-c ([0-9-]*)");\r
105 \r
106             // Output Settings\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
111 \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
118 \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
122 \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
127 \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
136 \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
146 \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
155 \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
159 \r
160             // Chapters Tab\r
161             Match chapterMarkers = Regex.Match(input, @" -m");\r
162             Match chapterMarkersFileMode = Regex.Match(input, @"--markers");\r
163 \r
164             // H264 Tab\r
165             Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");\r
166 \r
167             // Program Options\r
168             Match verbose = Regex.Match(input, @" -v");\r
169 \r
170             #endregion\r
171 \r
172             #region Set Varibles\r
173 \r
174             try\r
175             {\r
176                 #region Source Tab\r
177 \r
178                 if (title.Success)\r
179                     thisQuery.DVDTitle = int.Parse(title.ToString().Replace("-t ", string.Empty));\r
180 \r
181                 if (chapters.Success)\r
182                 {\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
186                     {\r
187                         thisQuery.DVDChapterFinish = int.Parse(actTitles[1]);\r
188                     }\r
189 \r
190                     if ((thisQuery.DVDChapterStart == 1) && (thisQuery.DVDChapterFinish == 0))\r
191                         thisQuery.DVDChapterFinish = thisQuery.DVDChapterStart;\r
192                 }\r
193 \r
194                 #endregion\r
195 \r
196                 #region Output Settings\r
197 \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
203 \r
204                 #endregion\r
205 \r
206                 #region Picture Tab\r
207 \r
208                 if (width.Success)\r
209                     thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", string.Empty));\r
210 \r
211                 if (height.Success)\r
212                     thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", string.Empty));\r
213 \r
214                 if (maxWidth.Success)\r
215                     thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", string.Empty));\r
216 \r
217                 if (maxHeight.Success)\r
218                     thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", string.Empty));\r
219 \r
220                 if (crop.Success)\r
221                 {\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
228                 }\r
229 \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
236                 else\r
237                     thisQuery.AnamorphicMode = 0;\r
238 \r
239                 thisQuery.KeepDisplayAsect = keepDisplayAsect.Success;\r
240 \r
241                 if (displayWidth.Success)\r
242                     thisQuery.DisplayWidthValue =\r
243                         double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", string.Empty));\r
244 \r
245                 if (pixelAspect.Success)\r
246                     thisQuery.PixelAspectWidth = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", string.Empty));\r
247 \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
250 \r
251                 if (modulus.Success)\r
252                     thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", string.Empty));\r
253 \r
254                 #endregion\r
255 \r
256                 #region Filters\r
257 \r
258                 thisQuery.Decomb = "Off";\r
259                 if (decomb.Success)\r
260                 {\r
261                     thisQuery.Decomb = "Default";\r
262                     if (decombValue.Success)\r
263                         thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", string.Empty).Replace("\"", string.Empty);\r
264                 }\r
265 \r
266                 thisQuery.DeInterlace = "Off";\r
267                 if (deinterlace.Success)\r
268                 {\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
273                 }\r
274 \r
275                 thisQuery.DeNoise = "Off";\r
276                 if (denoise.Success)\r
277                 {\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
281                 }\r
282 \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
287 \r
288                 int dval = 0;\r
289                 if (deblockValue != string.Empty)\r
290                     int.TryParse(deblockValue, out dval);\r
291                 thisQuery.DeBlock = dval;\r
292 \r
293                 thisQuery.DeTelecine = "Off";\r
294                 if (detelecine.Success)\r
295                 {\r
296                     thisQuery.DeTelecine = "Default";\r
297                     if (detelecineValue.Success)\r
298                         thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", string.Empty).Replace("\"", string.Empty);\r
299                 }\r
300 \r
301                 #endregion\r
302 \r
303                 #region Video Settings Tab\r
304 \r
305                 string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", string.Empty);\r
306                 switch (videoEncoderConvertion)\r
307                 {\r
308                     case "ffmpeg":\r
309                         videoEncoderConvertion = "MPEG-4 (FFmpeg)";\r
310                         break;\r
311                     case "x264":\r
312                         videoEncoderConvertion = "H.264 (x264)";\r
313                         break;\r
314                     case "theora":\r
315                         videoEncoderConvertion = "VP3 (Theora)";\r
316                         break;\r
317                     default:\r
318                         videoEncoderConvertion = "MPEG-4 (FFmpeg)";\r
319                         break;\r
320                 }\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
328 \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
333 \r
334                 if (videoQuality.Success)\r
335                 {\r
336                     float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", string.Empty), Culture);\r
337                     thisQuery.VideoQuality = qConvert;\r
338                 }\r
339                 else\r
340                     thisQuery.VideoQuality = -1;\r
341 \r
342                 #endregion\r
343 \r
344                 #region Audio Tab\r
345 \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
349                 {\r
350                     string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", string.Empty).Split(',');\r
351                     encoderCount = audioDataCounters.Length;\r
352                 }\r
353 \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
361 \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
374 \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
378                 {\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
383 \r
384                     if (trackMixes != null)\r
385                         if (trackMixes.Length >= (x + 1)) // Audio Mix\r
386                             track.MixDown = GetMixDown(trackMixes[x].Trim());\r
387 \r
388                     if (trackEncoders != null)\r
389                         if (trackEncoders.Length >= (x + 1)) // Audio Mix\r
390                             track.Encoder = GetAudioEncoder(trackEncoders[x].Trim());\r
391 \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
395 \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
399 \r
400                     if (trackDRCvalues != null)\r
401                         if (trackDRCvalues.Length >= (x + 1)) // Audio DRC Values\r
402                             track.DRC = trackDRCvalues[x].Trim();\r
403 \r
404                     allAudioTrackInfo.Add(track);\r
405                 }\r
406                 thisQuery.AudioInformation = allAudioTrackInfo;\r
407 \r
408                 // Subtitle Stuff\r
409                 if (subtitles.Success)\r
410                     thisQuery.Subtitles = subtitles.ToString().Replace("-s ", string.Empty);\r
411                 else\r
412                     thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";\r
413 \r
414                 thisQuery.ForcedSubtitles = forcedSubtitles.Success;\r
415 \r
416                 #endregion\r
417 \r
418                 #region Chapters Tab\r
419 \r
420                 if (chapterMarkersFileMode.Success || chapterMarkers.Success)\r
421                     thisQuery.ChapterMarkers = true;\r
422 \r
423                 #endregion\r
424 \r
425                 #region H.264 and other\r
426 \r
427                 if (x264.Success)\r
428                     thisQuery.H264Query = x264.ToString().Replace("-x ", string.Empty);\r
429 \r
430                 thisQuery.Verbose = verbose.Success;\r
431 \r
432                 #endregion\r
433             }\r
434             catch (Exception exc)\r
435             {\r
436                 MessageBox.Show(\r
437                     "An error has occured in the Query Parser.\n\n" +\r
438                     exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
439             }\r
440 \r
441             #endregion\r
442 \r
443             return thisQuery;\r
444         }\r
445 \r
446         /// <summary>\r
447         /// Get the GUI equiv to a CLI mixdown\r
448         /// </summary>\r
449         /// <param name="mixdown"></param>\r
450         /// <returns></returns>\r
451         private static string GetMixDown(string mixdown)\r
452         {\r
453             switch (mixdown.Trim())\r
454             {\r
455                 case "mono":\r
456                     return "Mono";\r
457                 case "stereo":\r
458                     return "Stereo";\r
459                 case "dpl1":\r
460                     return "Dolby Surround";\r
461                 case "dpl2":\r
462                     return "Dolby Pro Logic II";\r
463                 case "6ch":\r
464                     return "6 Channel Discrete";\r
465                 default:\r
466                     return "Automatic";\r
467             }\r
468         }\r
469 \r
470         /// <summary>\r
471         /// Get the GUI equiv to a CLI audio encoder\r
472         /// </summary>\r
473         /// <param name="audioEnc"></param>\r
474         /// <returns></returns>\r
475         private static string GetAudioEncoder(string audioEnc)\r
476         {\r
477             switch (audioEnc)\r
478             {\r
479                 case "faac":\r
480                     return "AAC (faac)";\r
481                 case "lame":\r
482                     return "MP3 (lame)";\r
483                 case "vorbis":\r
484                     return "Vorbis (vorbis)";\r
485                 case "ac3":\r
486                     return "AC3 Passthru";\r
487                 case "dts":\r
488                     return "DTS Passthru";\r
489                 default:\r
490                     return "AAC (faac)";\r
491             }\r
492         }\r
493     }\r
494 }