OSDN Git Service

WinGUI:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / QueryParser.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text;\r
4 using System.Text.RegularExpressions;\r
5 using System.Windows.Forms;\r
6 \r
7 namespace Handbrake.Functions\r
8 {\r
9     class QueryParser\r
10     {\r
11 \r
12         // All the Main Window GUI options\r
13         #region Varibles\r
14  \r
15         private string q_source;\r
16         /// <summary>\r
17         /// Returns a String \r
18         /// Full path of the source.\r
19         /// </summary>\r
20         public string Source\r
21         {\r
22             get\r
23             {\r
24                 return this.q_source;\r
25             }\r
26         }\r
27 \r
28         private string q_destination;\r
29         /// <summary>\r
30         /// Returns a String \r
31         /// Full path of the destination.\r
32         /// </summary>\r
33         public string Destination\r
34         {\r
35             get\r
36             {\r
37                 return this.q_destination;\r
38             }\r
39         }\r
40 \r
41         private int q_dvdTitle;\r
42         /// <summary>\r
43         /// Returns an Integer\r
44         /// DVD Title number.\r
45         /// </summary>\r
46         public int DVDTitle\r
47         {\r
48             get\r
49             {\r
50                 return this.q_dvdTitle;\r
51             }\r
52         }\r
53 \r
54         private string q_dvdChapters;\r
55         /// <summary>\r
56         /// Returns an String\r
57         /// DVD Chapter number or chapter range.\r
58         /// </summary>\r
59         public string DVDChapters\r
60         {\r
61             get\r
62             {\r
63                 return this.q_dvdChapters;\r
64             }\r
65         }\r
66 \r
67         private string q_videoEncoder;\r
68         /// <summary>\r
69         /// Returns an String\r
70         /// The Video Encoder used.\r
71         /// </summary>\r
72         public string VideoEncoder\r
73         {\r
74             get\r
75             {\r
76                 return this.q_videoEncoder;\r
77             }\r
78         }\r
79 \r
80         private string q_audioEncoder;\r
81         /// <summary>\r
82         /// Returns an String\r
83         /// The Audio Encoder used.\r
84         /// </summary>\r
85         public string AudioEncoder\r
86         {\r
87             get\r
88             {\r
89                 return this.q_audioEncoder;\r
90             }\r
91         }\r
92 \r
93         private int q_videoWidth;\r
94         /// <summary>\r
95         /// Returns an Integer\r
96         /// The selected Width for the encoding.\r
97         /// </summary>\r
98         public int Width\r
99         {\r
100             get\r
101             {\r
102                 return this.q_videoWidth;\r
103             }\r
104         }\r
105 \r
106         private int q_videoHeight;\r
107         /// <summary>\r
108         /// Returns an Integer\r
109         /// The selected Height for the encoding.\r
110         /// </summary>\r
111         public int Height\r
112         {\r
113             get\r
114             {\r
115                 return this.q_videoHeight;\r
116             }\r
117         }\r
118 \r
119         private string q_cropValues;\r
120         /// <summary>\r
121         /// Returns an String\r
122         /// Cropping values.\r
123         /// </summary>\r
124         public string CropValues\r
125         {\r
126             get\r
127             {\r
128                 return this.q_cropValues;\r
129             }\r
130         }\r
131 \r
132         private Boolean q_detelecine;\r
133         /// <summary>\r
134         /// Returns a boolean to indicate wither DeTelecine is on or off\r
135         /// </summary>\r
136         public Boolean DeTelecine\r
137         {\r
138             get\r
139             {\r
140                 return this.q_detelecine;\r
141             }\r
142         }\r
143 \r
144         private Boolean q_deBlock;\r
145         /// <summary>\r
146         /// Returns a boolean to indicate wither DeBlock is on or off.\r
147         /// </summary>\r
148         public Boolean DeBlock\r
149         {\r
150             get\r
151             {\r
152                 return this.q_deBlock;\r
153             }\r
154         }\r
155 \r
156         private string q_deinterlace;\r
157         /// <summary>\r
158         /// Returns a string with the De-Interlace option used.\r
159         /// </summary>\r
160         public string DeInterlace\r
161         {\r
162             get\r
163             {\r
164                 return this.q_deinterlace;\r
165             }\r
166         }\r
167 \r
168         private string q_denoise;\r
169         /// <summary>\r
170         /// Returns a string with the DeNoise option used.\r
171         /// </summary>\r
172         public string DeNoise\r
173         {\r
174             get\r
175             {\r
176                 return this.q_denoise;\r
177             }\r
178         }\r
179 \r
180         private Boolean q_anamorphic;\r
181         /// <summary>\r
182         /// Returns a boolean to indicate wither Anamorphic is on or off.\r
183         /// </summary>\r
184         public Boolean Anamorphic\r
185         {\r
186             get\r
187             {\r
188                 return this.q_anamorphic;\r
189             }\r
190         }\r
191 \r
192         private Boolean q_chapterMarkers;\r
193         /// <summary>\r
194         /// Returns a boolean to indicate wither Chapter Markers is on or off.\r
195         /// </summary>\r
196         public Boolean ChapterMarkers\r
197         {\r
198             get\r
199             {\r
200                 return this.q_chapterMarkers;\r
201             }\r
202         }\r
203 \r
204         private Boolean q_grayscale;\r
205         /// <summary>\r
206         /// Returns a boolean to indicate wither Grayscale is on or off.\r
207         /// </summary>\r
208         public Boolean Grayscale\r
209         {\r
210             get\r
211             {\r
212                 return this.q_grayscale;\r
213             }\r
214         }\r
215 \r
216         private Boolean q_twoPass;\r
217         /// <summary>\r
218         /// Returns a boolean to indicate wither Two Pass Encoding is on or off.\r
219         /// </summary>\r
220         public Boolean TwoPass\r
221         {\r
222             get\r
223             {\r
224                 return this.q_twoPass;\r
225             }\r
226         }\r
227 \r
228         private Boolean q_turboFirst;\r
229         /// <summary>\r
230         /// Returns a boolean to indicate wither Chapter Markers is on or off.\r
231         /// </summary>\r
232         public Boolean TurboFirstPass\r
233         {\r
234             get\r
235             {\r
236                 return this.q_turboFirst;\r
237             }\r
238         }\r
239 \r
240         private Boolean q_largeMp4;\r
241         /// <summary>\r
242         /// Returns a boolean to indicate wither Larger MP4 files is on or off.\r
243         /// </summary>\r
244         public Boolean LargeMP4\r
245         {\r
246             get\r
247             {\r
248                 return this.q_largeMp4;\r
249             }\r
250         }\r
251 \r
252         private string q_videoFramerate;\r
253         /// <summary>\r
254         /// Returns a string with the video Framerate\r
255         /// </summary>\r
256         public string VideoFramerate\r
257         {\r
258             get\r
259             {\r
260                 return this.q_videoFramerate;\r
261             }\r
262         }\r
263 \r
264         private string q_avgBitrate;\r
265         /// <summary>\r
266         /// Returns a string with the average video bitrate\r
267         /// </summary>\r
268         public string AverageVideoBitrate\r
269         {\r
270             get\r
271             {\r
272                 return this.q_avgBitrate;\r
273             }\r
274         }\r
275 \r
276         private string  q_videoTargetSize;\r
277         /// <summary>\r
278         /// Returns a string with the video target size\r
279         /// </summary>\r
280         public string VideoTargetSize\r
281         {\r
282             get\r
283             {\r
284                 return this.q_videoTargetSize;\r
285             }\r
286         }\r
287 \r
288         private int q_videoQuality;\r
289         /// <summary>\r
290         /// Returns a int with the video quality value\r
291         /// </summary>\r
292         public int VideoQuality\r
293         {\r
294             get\r
295             {\r
296                 return this.q_videoQuality;\r
297             }\r
298         }\r
299 \r
300         private Boolean q_crf;\r
301         /// <summary>\r
302         /// Returns a boolean to indicate if CRF is on or off.\r
303         /// </summary>\r
304         public Boolean CRF\r
305         {\r
306             get\r
307             {\r
308                 return this.q_crf;\r
309             }\r
310         }\r
311 \r
312         private string q_audioBitrate;\r
313         /// <summary>\r
314         /// Returns a string with the audio bitrate\r
315         /// </summary>\r
316         public string AudioBitrate\r
317         {\r
318             get\r
319             {\r
320                 return this.q_audioBitrate;\r
321             }\r
322         }\r
323 \r
324         private string q_audioSamplerate;\r
325         /// <summary>\r
326         /// Returns a string with the audio sample rate\r
327         /// </summary>\r
328         public string AudioSampleBitrate\r
329         {\r
330             get\r
331             {\r
332                 return this.q_audioSamplerate;\r
333             }\r
334         }\r
335 \r
336         private string q_audioTrack1;\r
337         /// <summary>\r
338         /// Returns a string with the First selected Audio track\r
339         /// </summary>\r
340         public string AudioTrack1\r
341         {\r
342             get\r
343             {\r
344                 return this.q_audioTrack1;\r
345             }\r
346         }\r
347 \r
348         private string q_audioTrackMix;\r
349         /// <summary>\r
350         /// Returns a string with the First selected Audio track Mix\r
351         /// </summary>\r
352         public string AudioTrackMix\r
353         {\r
354             get\r
355             {\r
356                 return this.q_audioTrackMix;\r
357             }\r
358         }\r
359 \r
360         private string q_subtitles;\r
361         /// <summary>\r
362         /// Returns a string with the selected subtitle track\r
363         /// </summary>\r
364         public string Subtitles\r
365         {\r
366             get\r
367             {\r
368                 return this.q_subtitles;\r
369             }\r
370         }\r
371 \r
372         private string q_h264;\r
373         /// <summary>\r
374         /// Returns a string with the Advanced H264 query string\r
375         /// </summary>\r
376         public string H264Query\r
377         {\r
378             get\r
379             {\r
380                 return this.q_h264;\r
381             }\r
382         }\r
383         private Boolean q_verbose;\r
384         /// <summary>\r
385         /// Returns a string with the Advanced H264 query string\r
386         /// </summary>\r
387         public Boolean Verbose\r
388         {\r
389             get\r
390             {\r
391                 return this.q_verbose;\r
392             }\r
393         }\r
394         #endregion\r
395 \r
396         // Takes in a query which can be in any order and parses it. All varibles are then set so they can be used elsewhere.\r
397         public static QueryParser Parse(String input)\r
398         {\r
399             QueryParser thisQuery = new QueryParser();\r
400 \r
401             // Example Input\r
402             // -i "C:\Documents and Settings\Scott\Desktop\Files\DVD\Test Image.iso" \r
403             //-t 1 -c 3-5 -o "C:\test.mp4" -e x264 -E faac -w 720 -l 400 --crop 0:0:0:0\r
404             //-s 1 --deinterlace="1:-1:1" -g  -m  -b 2500 -2  -r 25 -T  -4  --deblock --detelecine \r
405             //--denoise=7:7:5:5 -x bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=2 -B 160 -R 48 -a 1 -6 stereo -v \r
406             //-q 0.45 -Q -S 2134\r
407 \r
408 \r
409             //Source\r
410             Regex r1 = new Regex(@"(-i)(?:\s\"")([a-zA-Z0-9:\\\s\.]+)(?:\"")");\r
411             Match source = r1.Match(input.Replace('"', '\"'));\r
412             Match title = Regex.Match(input, @"-t ([0-9]*)");\r
413             Match chapters = Regex.Match(input, @"-c ([0-9-]*)");\r
414 \r
415             //Destination\r
416             Regex r2 = new Regex(@"(-o)(?:\s\"")([a-zA-Z0-9:\\\s\.]+)(?:\"")");\r
417             Match destination = r2.Match(input.Replace('"', '\"'));\r
418             Match width = Regex.Match(input, @"-w ([0-9]*)");\r
419             Match height = Regex.Match(input, @"-l ([0-9]*)");\r
420             Match videoEncoder = Regex.Match(input, @"-e ([a-zA-Z0-9]*)");\r
421             Match audioEncoder = Regex.Match(input, @"-E ([a-zA-Z0-9]*)");\r
422 \r
423             //Picture Settings Tab\r
424             Match deinterlace = Regex.Match(input, @"--deinterlace=([0-9:-]*)");  // DOES NOT WORK. Needs Fixed\r
425             Match denoise = Regex.Match(input, @"--denoise=([0-9:]*)");\r
426             Match deblock = Regex.Match(input, @"--deblock");\r
427             Match detelecine = Regex.Match(input, @"--detelecine");\r
428             Match anamorphic = Regex.Match(input, @"-p");\r
429             Match chapterMarkers = Regex.Match(input, @"-m");\r
430             Match crop = Regex.Match(input, @"--crop ([0-9]):([0-9]):([0-9]):([0-9])");\r
431 \r
432             //Video Settings Tab\r
433             Match videoFramerate = Regex.Match(input, @"-r ([0-9]*)");\r
434             Match videoBitrate = Regex.Match(input, @"-b ([0-9]*)");\r
435             Match videoQuality = Regex.Match(input, @"-q ([0-9.]*)");\r
436             Match videoFilesize = Regex.Match(input, @"-S ([0-9.]*)");\r
437             Match CRF = Regex.Match(input, @"-Q");\r
438             Match twoPass = Regex.Match(input, @"-2");\r
439             Match turboFirstPass = Regex.Match(input, @"-T");\r
440             Match grayscale = Regex.Match(input, @"-g");\r
441             Match largerMp4 = Regex.Match(input, @"-4");\r
442 \r
443             //Audio Settings Tab\r
444             Match subtitles = Regex.Match(input, @"-s ([0-9]*)");\r
445             Match audioBitrate = Regex.Match(input, @"-B ([0-9]*)");\r
446             Match audioSampleRate = Regex.Match(input, @"-R ([0-9.]*)");\r
447             Match audioChannelsMix = Regex.Match(input, @"-6 ([a-zA-Z0-9]*)");\r
448             Match audioChannel = Regex.Match(input, @"-a ([0-9]*)");\r
449 \r
450             //H264 Tab\r
451             Match x264 = Regex.Match(input, @"-x ([a-zA-Z0-9=:-]*)");\r
452             \r
453             //Program Options\r
454             Match verbose = Regex.Match(input, @"-v");\r
455              \r
456             \r
457             // ### NOTES ###\r
458             // The following code needs alot of error handling added to it at some point.\r
459             // May be an idea to add additional options such as CPU etc later.\r
460 \r
461             try\r
462             {\r
463                 //Source\r
464                 thisQuery.q_source = source.ToString();\r
465                 thisQuery.q_dvdTitle = int.Parse(title.ToString().Replace("-t ", ""));\r
466                 thisQuery.q_dvdChapters = chapters.ToString();\r
467 \r
468                 //Destination\r
469                 thisQuery.q_destination = destination.ToString();\r
470                 thisQuery.q_videoEncoder = videoEncoder.ToString();\r
471                 thisQuery.q_audioEncoder = audioEncoder.ToString();\r
472                 thisQuery.q_videoWidth = int.Parse(width.ToString().Replace("-w ", ""));\r
473                 thisQuery.q_videoHeight = int.Parse(height.ToString().Replace("-l ", ""));\r
474 \r
475                 //Picture Settings Tab\r
476                 thisQuery.q_cropValues = crop.ToString();\r
477                 thisQuery.q_detelecine = detelecine.Success;\r
478                 thisQuery.q_deBlock = deblock.Success;\r
479                 thisQuery.q_deinterlace = deinterlace.ToString();\r
480                 thisQuery.q_denoise = denoise.ToString();\r
481                 thisQuery.q_anamorphic = anamorphic.Success;\r
482                 thisQuery.q_chapterMarkers = chapterMarkers.Success;\r
483 \r
484                 //Video Settings Tab\r
485                 thisQuery.q_grayscale = grayscale.Success;\r
486                 thisQuery.q_twoPass = twoPass.Success;\r
487                 thisQuery.q_turboFirst = turboFirstPass.Success;\r
488                 thisQuery.q_largeMp4 = largerMp4.Success;\r
489                 thisQuery.q_videoFramerate = videoFramerate.ToString();\r
490                 thisQuery.q_avgBitrate = videoBitrate.ToString();\r
491                 thisQuery.q_videoTargetSize = videoFilesize.ToString();\r
492                 thisQuery.q_videoQuality = int.Parse(videoQuality.ToString());\r
493                 thisQuery.q_crf = CRF.Success;\r
494 \r
495                 //Audio Settings Tab\r
496                 thisQuery.q_audioBitrate = audioBitrate.ToString();\r
497                 thisQuery.q_audioSamplerate = audioSampleRate.ToString();\r
498                 thisQuery.q_audioTrack1 = audioChannel.ToString();\r
499                 thisQuery.q_audioTrackMix = audioChannelsMix.ToString();\r
500                 thisQuery.q_subtitles = subtitles.ToString();\r
501 \r
502                 //H264 Tab\r
503                 thisQuery.q_h264 = x264.ToString();\r
504 \r
505                 //Progam Options\r
506                 thisQuery.q_verbose = verbose.Success;\r
507             }\r
508             catch (Exception exc)\r
509             {\r
510                 MessageBox.Show(exc.ToString());\r
511             }\r
512 \r
513             return thisQuery;\r
514         }\r
515     }\r
516 }\r