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 using System;\r
8 using System.Globalization;\r
9 using System.Text.RegularExpressions;\r
10 using System.Windows.Forms;\r
11 using System.Collections;\r
12 \r
13 namespace Handbrake.Functions\r
14 {\r
15     internal class QueryParser\r
16     {\r
17         private static readonly CultureInfo Culture = new CultureInfo("en-US", false);\r
18 \r
19         #region Varibles\r
20 \r
21         #region Source\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
25         #endregion\r
26 \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
32         #endregion\r
33 \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
50         #endregion\r
51 \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
58         #endregion\r
59 \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
69         #endregion\r
70 \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
75         #endregion\r
76 \r
77         #region Chapter Markers\r
78         public Boolean ChapterMarkers { get; private set; }\r
79         #endregion\r
80 \r
81         #region Other\r
82         public string H264Query { get; private set; }\r
83         public Boolean Verbose { get; private set; }\r
84         #endregion\r
85 \r
86         #endregion\r
87 \r
88         /// <summary>\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
91         /// </summary>\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
95         {\r
96             var thisQuery = new QueryParser();\r
97 \r
98             #region Regular Expressions\r
99 \r
100             //Source\r
101             Match title = Regex.Match(input, @"-t ([0-9]*)");\r
102             Match chapters = Regex.Match(input, @"-c ([0-9-]*)");\r
103 \r
104             //Output Settings\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
109 \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
116 \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
120 \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
125 \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
134 \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
144 \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
153 \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
157 \r
158             // Chapters Tab\r
159             Match chapterMarkers = Regex.Match(input, @" -m");\r
160             Match chapterMarkersFileMode = Regex.Match(input, @"--markers");\r
161 \r
162             //H264 Tab\r
163             Match x264 = Regex.Match(input, @"-x ([.,/a-zA-Z0-9=:-]*)");\r
164 \r
165             //Program Options\r
166             Match verbose = Regex.Match(input, @" -v");\r
167 \r
168             #endregion\r
169 \r
170             #region Set Varibles\r
171 \r
172             try\r
173             {\r
174                 #region Source Tab\r
175 \r
176                 if (title.Success)\r
177                     thisQuery.DVDTitle = int.Parse(title.ToString().Replace("-t ", ""));\r
178 \r
179                 if (chapters.Success)\r
180                 {\r
181                     string[] actTitles = chapters.ToString().Replace("-c ", "").Split('-');\r
182                     thisQuery.DVDChapterStart = int.Parse(actTitles[0]);\r
183                     if (actTitles.Length > 1)\r
184                     {\r
185                         thisQuery.DVDChapterFinish = int.Parse(actTitles[1]);\r
186                     }\r
187 \r
188                     if ((thisQuery.DVDChapterStart == 1) && (thisQuery.DVDChapterFinish == 0))\r
189                         thisQuery.DVDChapterFinish = thisQuery.DVDChapterStart;\r
190                 }\r
191                 #endregion\r
192 \r
193                 #region Output Settings\r
194 \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
200 \r
201                 #endregion\r
202 \r
203                 #region Picture Tab\r
204 \r
205                 if (width.Success)\r
206                     thisQuery.Width = int.Parse(width.Groups[0].Value.Replace("-w ", ""));\r
207                                                               \r
208                 if (height.Success)\r
209                     thisQuery.Height = int.Parse(height.Groups[0].Value.Replace("-l ", ""));\r
210 \r
211                 if (maxWidth.Success)\r
212                     thisQuery.MaxWidth = int.Parse(maxWidth.Groups[0].Value.Replace("-X ", ""));\r
213 \r
214                 if (maxHeight.Success)\r
215                     thisQuery.MaxHeight = int.Parse(maxHeight.Groups[0].Value.Replace("-Y ", ""));\r
216 \r
217                 if (crop.Success)\r
218                 {\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
225                 } \r
226 \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
233                 else\r
234                     thisQuery.AnamorphicMode = 0;\r
235 \r
236                 thisQuery.keepDisplayAsect = keepDisplayAsect.Success;\r
237 \r
238                 if (displayWidth.Success)\r
239                     thisQuery.displayWidthValue = double.Parse(displayWidth.Groups[0].Value.Replace("--display-width ", ""));\r
240 \r
241                 if (pixelAspect.Success)\r
242                     thisQuery.pixelAspectWidth = int.Parse(pixelAspect.Groups[0].Value.Replace("--pixel-aspect ", ""));\r
243 \r
244                 if (pixelAspect.Success)\r
245                     thisQuery.pixelAspectHeight = int.Parse(pixelAspect.Groups[1].Value.Replace("--pixel-aspect ", ""));\r
246 \r
247                 if (modulus.Success)\r
248                     thisQuery.AnamorphicModulus = int.Parse(modulus.Groups[0].Value.Replace("--modulus ", ""));\r
249 \r
250 \r
251                 #endregion\r
252 \r
253                 #region Filters\r
254 \r
255                 thisQuery.Decomb = "Off";\r
256                 if (decomb.Success)\r
257                 {\r
258                     thisQuery.Decomb = "Default";\r
259                     if (decombValue.Success)\r
260                         thisQuery.Decomb = decombValue.ToString().Replace("--decomb=", "").Replace("\"", "");\r
261                 }\r
262 \r
263                 thisQuery.DeInterlace = "None";\r
264                 if (deinterlace.Success)\r
265                 {\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
269                 }\r
270 \r
271                 thisQuery.DeNoise = "None";\r
272                 if (denoise.Success)\r
273                 {\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
276                 }\r
277 \r
278                 string deblockValue = "";\r
279                 thisQuery.DeBlock = 0;\r
280                 if (deblock.Success)\r
281                     deblockValue = deblock.ToString().Replace("--deblock=", "");\r
282 \r
283                 int dval = 0;\r
284                 if (deblockValue != "")\r
285                     int.TryParse(deblockValue, out dval);\r
286                 thisQuery.DeBlock = dval;\r
287 \r
288                 thisQuery.DeTelecine = "Off";\r
289                 if (detelecine.Success)\r
290                 {\r
291                     thisQuery.DeTelecine = "Default";\r
292                     if (detelecineValue.Success)\r
293                         thisQuery.DeTelecine = detelecineValue.ToString().Replace("--detelecine=", "").Replace("\"", "");\r
294                 }\r
295 \r
296                 #endregion\r
297 \r
298                 #region Video Settings Tab\r
299 \r
300                 string videoEncoderConvertion = videoEncoder.ToString().Replace("-e ", "");\r
301                 switch (videoEncoderConvertion)\r
302                 {\r
303                     case "ffmpeg":\r
304                         videoEncoderConvertion = "MPEG-4 (FFmpeg)";\r
305                         break;\r
306                     case "x264":\r
307                         videoEncoderConvertion = "H.264 (x264)";\r
308                         break;\r
309                     case "theora":\r
310                         videoEncoderConvertion = "VP3 (Theora)";\r
311                         break;\r
312                     default:\r
313                         videoEncoderConvertion = "MPEG-4 (FFmpeg)";\r
314                         break;\r
315                 }\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
321 \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
326 \r
327                 if (videoQuality.Success)\r
328                 {\r
329                     float qConvert = float.Parse(videoQuality.ToString().Replace("-q ", ""), Culture);\r
330                     thisQuery.VideoQuality = qConvert;\r
331                 }\r
332                 #endregion\r
333 \r
334                 #region Audio Tab\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
338                 {\r
339                     string[] audioDataCounters = audioEncoders.ToString().Replace("-E ", "").Split(',');\r
340                     encoderCount = audioDataCounters.Length;\r
341                 }\r
342 \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
350 \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
363 \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
367                 {\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
372 \r
373                     if (trackMixes != null)\r
374                         if (trackMixes.Length >= (x + 1))                        // Audio Mix\r
375                             track.MixDown = getMixDown(trackMixes[x].Trim());\r
376 \r
377                     if (trackEncoders != null)\r
378                         if (trackEncoders.Length >= (x + 1))                     // Audio Mix\r
379                             track.Encoder = getAudioEncoder(trackEncoders[x].Trim());\r
380 \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
384 \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
388 \r
389                     if (trackDRCvalues != null)\r
390                         if (trackDRCvalues.Length >= (x + 1))                   // Audio DRC Values\r
391                             track.DRC = trackDRCvalues[x].Trim();\r
392 \r
393                     AllAudioTrackInfo.Add(track);\r
394                 }\r
395                 thisQuery.AudioInformation = AllAudioTrackInfo;\r
396 \r
397                 // Subtitle Stuff\r
398                 if (subtitles.Success)\r
399                     thisQuery.Subtitles = subtitles.ToString().Replace("-s ", "");\r
400                 else\r
401                     thisQuery.Subtitles = subScan.Success ? "Autoselect" : "None";\r
402 \r
403                 thisQuery.ForcedSubtitles = forcedSubtitles.Success;\r
404                 #endregion\r
405 \r
406                 #region Chapters Tab\r
407                 if (chapterMarkersFileMode.Success || chapterMarkers.Success)\r
408                     thisQuery.ChapterMarkers = true;\r
409                 #endregion\r
410 \r
411                 #region H.264 and other\r
412 \r
413                 if (x264.Success)\r
414                     thisQuery.H264Query = x264.ToString().Replace("-x ", "");\r
415 \r
416                 thisQuery.Verbose = verbose.Success;\r
417 \r
418                 #endregion\r
419             }\r
420             catch (Exception exc)\r
421             {\r
422                 MessageBox.Show(\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
425             }\r
426 \r
427             #endregion\r
428 \r
429             return thisQuery;\r
430         }\r
431         private static string getMixDown(string mixdown)\r
432         {\r
433             switch (mixdown.Trim())\r
434             {\r
435                 case "mono":\r
436                     return "Mono";\r
437                 case "stereo":\r
438                     return "Stereo";\r
439                 case "dpl1":\r
440                     return "Dolby Surround";\r
441                 case "dpl2":\r
442                     return "Dolby Pro Logic II";\r
443                 case "6ch":\r
444                     return "6 Channel Discrete";\r
445                 default:\r
446                     return "Automatic";\r
447             }\r
448         }\r
449         private static string getAudioEncoder(string audioEnc)\r
450         {\r
451             switch (audioEnc)\r
452             {\r
453                 case "faac":\r
454                     return "AAC (faac)";\r
455                 case "lame":\r
456                     return "MP3 (lame)";\r
457                 case "vorbis":\r
458                     return "Vorbis (vorbis)";\r
459                 case "ac3":\r
460                     return "AC3 Passthru";\r
461                 case "dts":\r
462                     return "DTS Passthru";\r
463                 default:\r
464                     return "AAC (faac)";\r
465             }\r
466         }\r
467     }\r
468 \r
469     public class AudioTrack\r
470     {\r
471         public AudioTrack()\r
472         {\r
473             // Default Values\r
474             Track = "Automatic";\r
475             MixDown = "Automatic";\r
476             SampleRate = "Auto";\r
477             Bitrate = "Auto";\r
478             DRC = "1";\r
479         }\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
486     }\r
487 }