OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / QueryGenerator.cs
1 /*  QueryGenerator.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.Windows.Forms;\r
9 using System.Globalization;\r
10 using System.IO;\r
11 using System.Collections.Generic;\r
12 \r
13 namespace Handbrake.Functions\r
14 {\r
15     class QueryGenerator\r
16     {\r
17         /// <summary>\r
18         /// Generates a CLI query based on the GUI widgets.\r
19         /// </summary>\r
20         /// <param name="mainWindow"></param>\r
21         /// <returns>The CLI String</returns>\r
22         public string generateTheQuery(frmMain mainWindow)\r
23         {\r
24             // Source tab\r
25             string query = "";\r
26 \r
27             if ((mainWindow.text_source.Text != "") && (mainWindow.text_source.Text.Trim() != "Click 'Source' to continue"))\r
28                 query = " -i " + '"' + mainWindow.text_source.Text + '"';\r
29 \r
30             if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
31             {\r
32                 string[] titleInfo = mainWindow.drp_dvdtitle.Text.Split(' ');\r
33                 query += " -t " + titleInfo[0];\r
34             }\r
35 \r
36             if (Properties.Settings.Default.dvdnav == "Checked")\r
37                 if (mainWindow.drop_angle.Items.Count != 0)\r
38                     query += " --angle " + mainWindow.drop_angle.SelectedItem;\r
39 \r
40             if (mainWindow.drop_chapterFinish.Text == mainWindow.drop_chapterStart.Text && mainWindow.drop_chapterStart.Text != "Auto")\r
41                 query += " -c " + mainWindow.drop_chapterStart.Text;\r
42             else if (mainWindow.drop_chapterStart.Text == "Auto" && mainWindow.drop_chapterFinish.Text != "Auto")\r
43                 query += " -c " + "0-" + mainWindow.drop_chapterFinish.Text;\r
44             else if (mainWindow.drop_chapterStart.Text != "Auto" && mainWindow.drop_chapterFinish.Text != "Auto")\r
45                 query += " -c " + mainWindow.drop_chapterStart.Text + "-" + mainWindow.drop_chapterFinish.Text;\r
46 \r
47             // Destination tab\r
48             if (mainWindow.text_destination.Text != "")\r
49                 query += " -o " + '"' + mainWindow.text_destination.Text + '"';\r
50 \r
51             query += generateTabbedComponentsQuery(mainWindow);\r
52             return query;\r
53         }\r
54 \r
55         /// <summary>\r
56         /// Generates a CLI query for the preview function.\r
57         /// This basically forces a shortened version of the encdode.\r
58         /// </summary>\r
59         /// <param name="mainWindow"></param>\r
60         /// <param name="duration">Duration</param>\r
61         /// <param name="preview">Start at preview</param>\r
62         /// <returns>Returns a CLI query String.</returns>\r
63         public string generatePreviewQuery(frmMain mainWindow, string duration, string preview)\r
64         {\r
65             int seconds;\r
66             int.TryParse(duration, out seconds);\r
67 \r
68             // Source tab\r
69             string query = "";\r
70 \r
71             if ((mainWindow.text_source.Text != "") && (mainWindow.text_source.Text.Trim() != "Click 'Source' to continue"))\r
72                 query = " -i " + '"' + mainWindow.text_source.Text + '"';\r
73 \r
74             if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
75             {\r
76                 string[] titleInfo = mainWindow.drp_dvdtitle.Text.Split(' ');\r
77                 query += " -t " + titleInfo[0];\r
78             }\r
79 \r
80             if (mainWindow.drop_angle.SelectedIndex != 0)\r
81                 query += " --angle " + mainWindow.drop_angle.SelectedItem;\r
82 \r
83             query += " --start-at-preview " + preview;\r
84             query += " --stop-at duration:" + duration + " ";\r
85 \r
86             // Destination tab\r
87             if (mainWindow.text_destination.Text != "")\r
88                 query += " -o " + '"' + mainWindow.text_destination.Text.Replace(".m", "_sample.m") + '"';\r
89 \r
90             query += generateTabbedComponentsQuery(mainWindow);\r
91             return query;\r
92         }\r
93 \r
94         /// <summary>\r
95         /// Generates part of the CLI query, for the tabbed components only.\r
96         /// </summary>\r
97         /// <param name="mainWindow"></param>\r
98         /// <returns></returns>\r
99         public static string generateTabbedComponentsQuery(frmMain mainWindow)\r
100         {\r
101             string query = "";\r
102 \r
103             #region Output Settings Box\r
104             query += " -f " + mainWindow.drop_format.Text.ToLower().Replace(" file", "");\r
105 \r
106             // These are output settings features\r
107             if (mainWindow.check_largeFile.Checked)\r
108                 query += " -4 ";\r
109 \r
110             if (mainWindow.check_iPodAtom.Checked)\r
111                 query += " -I ";\r
112 \r
113             if (mainWindow.check_optimiseMP4.Checked)\r
114                 query += " -O ";\r
115             #endregion\r
116 \r
117             #region Picture Settings Tab\r
118 \r
119             // Use MaxWidth for built-in presets and width for user settings.\r
120             if (mainWindow.pictureSettings.maxWidth == 0)\r
121             {\r
122 \r
123                 if (mainWindow.pictureSettings.text_width.Text != "")\r
124                     if (mainWindow.pictureSettings.drp_anamorphic.SelectedIndex != 1) // Prevent usage for strict anamorphic\r
125                         query += " -w " + mainWindow.pictureSettings.text_width.Text;\r
126             }\r
127             else\r
128             {\r
129                 if (mainWindow.pictureSettings.text_width.Text != "")\r
130                     query += " -X " + mainWindow.pictureSettings.text_width.Text;\r
131             }\r
132 \r
133             // Use MaxHeight for built-in presets and height for user settings.\r
134             if (mainWindow.pictureSettings.maxHeight == 0)\r
135             {\r
136                 if (mainWindow.pictureSettings.text_height.Text != "")\r
137                     if (mainWindow.pictureSettings.drp_anamorphic.SelectedIndex == 0 || mainWindow.pictureSettings.drp_anamorphic.SelectedIndex == 3) // Prevent usage for strict anamorphic\r
138                         query += " -l " + mainWindow.pictureSettings.text_height.Text;\r
139             }\r
140             else\r
141             {\r
142                 if (mainWindow.pictureSettings.text_height.Text != "")\r
143                     query += " -Y " + mainWindow.pictureSettings.text_height.Text;\r
144             }\r
145 \r
146             string cropTop = mainWindow.pictureSettings.crop_top.Text;\r
147             string cropBottom = mainWindow.pictureSettings.crop_bottom.Text;\r
148             string cropLeft = mainWindow.pictureSettings.crop_left.Text;\r
149             string cropRight = mainWindow.pictureSettings.crop_right.Text;\r
150 \r
151             if (mainWindow.pictureSettings.check_customCrop.Checked)\r
152             {\r
153                 if (mainWindow.pictureSettings.crop_top.Text == string.Empty)\r
154                     cropTop = "0";\r
155                 if (mainWindow.pictureSettings.crop_bottom.Text == string.Empty)\r
156                     cropBottom = "0";\r
157                 if (mainWindow.pictureSettings.crop_left.Text == string.Empty)\r
158                     cropLeft = "0";\r
159                 if (mainWindow.pictureSettings.crop_right.Text == string.Empty)\r
160                     cropRight = "0";\r
161 \r
162                 query += " --crop " + cropTop + ":" + cropBottom + ":" + cropLeft + ":" + cropRight;\r
163             }\r
164 \r
165             switch (mainWindow.pictureSettings.drp_anamorphic.SelectedIndex)\r
166             {\r
167                 case 1:\r
168                     query += " --strict-anamorphic ";\r
169                     break;\r
170                 case 2:\r
171                     query += " --loose-anamorphic ";\r
172                     break;\r
173                 case 3:\r
174                     query += " --custom-anamorphic ";\r
175                     query += " --display-width " + mainWindow.pictureSettings.txt_displayWidth.Text + " ";\r
176                     if (mainWindow.pictureSettings.check_KeepAR.Checked)\r
177                         query += " --keep-display-aspect ";\r
178                     if (mainWindow.pictureSettings.txt_parWidth.Text != "" && mainWindow.pictureSettings.txt_parHeight.Text != "")\r
179                         query += " --pixel-aspect " + mainWindow.pictureSettings.txt_parWidth.Text + ":" + mainWindow.pictureSettings.txt_parHeight.Text + " ";\r
180                     break;\r
181             }\r
182             #endregion\r
183 \r
184             #region Filters\r
185             query += mainWindow.ctl_detelecine.getCLIQuery;\r
186             query += mainWindow.ctl_decomb.getCLIQuery;\r
187             query += mainWindow.ctl_deinterlace.getCLIQuery;\r
188             query += mainWindow.ctl_denoise.getCLIQuery;\r
189 \r
190             if (mainWindow.slider_deblock.Value != 4)\r
191                 query += " --deblock=" + mainWindow.slider_deblock.Value;\r
192             #endregion\r
193 \r
194             #region Video Settings Tab\r
195 \r
196             switch (mainWindow.drp_videoEncoder.Text)\r
197             {\r
198                 case "MPEG-4 (FFmpeg)":\r
199                     query += " -e ffmpeg";\r
200                     break;\r
201                 case "H.264 (x264)":\r
202                     query += " -e x264";\r
203                     break;\r
204                 case "VP3 (Theora)":\r
205                     query += " -e theora";\r
206                     break;\r
207                 default:\r
208                     query += " -e x264";\r
209                     break;\r
210             }\r
211 \r
212             if (mainWindow.check_grayscale.Checked)\r
213                 query += " -g ";\r
214 \r
215             // Video Settings\r
216             if (mainWindow.radio_avgBitrate.Checked)\r
217                 query += " -b " + mainWindow.text_bitrate.Text;\r
218 \r
219             if (mainWindow.radio_targetFilesize.Checked)\r
220                 query += " -S " + mainWindow.text_filesize.Text;\r
221 \r
222             // Video Quality Setting\r
223             if (mainWindow.radio_cq.Checked)\r
224             {\r
225                 double value;\r
226                 switch (mainWindow.drp_videoEncoder.Text)\r
227                 {\r
228                     case "MPEG-4 (FFmpeg)":\r
229                         value = 31 - (mainWindow.slider_videoQuality.Value - 1);\r
230                         query += " -q " + value.ToString(new CultureInfo("en-US"));\r
231                         break;\r
232                     case "H.264 (x264)":\r
233                         double divided;\r
234                         CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");\r
235                         double.TryParse(Properties.Settings.Default.x264cqstep,\r
236                                         NumberStyles.Number,\r
237                                         culture,\r
238                                         out divided);\r
239                         value = 51 - mainWindow.slider_videoQuality.Value * divided;\r
240                         value = Math.Round(value, 2);\r
241                         query += " -q " + value.ToString(new CultureInfo("en-US"));\r
242                         break;\r
243                     case "VP3 (Theora)":\r
244                         value = mainWindow.slider_videoQuality.Value;\r
245                         query += " -q " + value.ToString(new CultureInfo("en-US"));\r
246                         break;\r
247                 }\r
248             }\r
249 \r
250             if (mainWindow.check_2PassEncode.Checked)\r
251                 query += " -2 ";\r
252 \r
253             if (mainWindow.check_turbo.Checked)\r
254                 query += " -T ";\r
255 \r
256             if (mainWindow.drp_videoFramerate.Text != "Same as source")\r
257                 query += " -r " + mainWindow.drp_videoFramerate.Text;\r
258             #endregion\r
259 \r
260             #region Audio Settings Tab\r
261 \r
262             ListView audioTracks = mainWindow.audioPanel.getAudioPanel();\r
263             List<string> tracks = new List<string>();\r
264             List<string> codecs = new List<string>();\r
265             List<string> mixdowns = new List<string>();\r
266             List<string> samplerates = new List<string>();\r
267             List<string> bitrates = new List<string>();\r
268             List<string> drcs = new List<string>();\r
269 \r
270             // No Audio\r
271             if (audioTracks.Items.Count == 0)\r
272                 query += " -a none ";\r
273 \r
274             // Gather information about each audio track and store them in the declared lists.\r
275             foreach (ListViewItem row in audioTracks.Items)\r
276             {\r
277                 // Audio Track (-a)\r
278                 if (row.Text == "Automatic")\r
279                     tracks.Add("1");\r
280                 else if (row.Text != "None")\r
281                 {\r
282                     string[] tempSub = row.SubItems[1].Text.Split(' ');\r
283                     tracks.Add(tempSub[0]);\r
284                 }\r
285 \r
286                 // Audio Codec (-E)\r
287                 if (row.SubItems[1].Text != String.Empty)\r
288                     codecs.Add(getAudioEncoder(row.SubItems[2].Text));\r
289 \r
290                 // Audio Mixdown (-6)\r
291                 if (row.SubItems[2].Text != String.Empty)\r
292                     mixdowns.Add(getMixDown(row.SubItems[3].Text));\r
293 \r
294                 // Sample Rate (-R)\r
295                 if (row.SubItems[3].Text != String.Empty)\r
296                     samplerates.Add(row.SubItems[4].Text.Replace("Auto", "Auto"));\r
297 \r
298                 // Audio Bitrate (-B)\r
299                 if (row.SubItems[4].Text != String.Empty)\r
300                     bitrates.Add(row.SubItems[5].Text.Replace("Auto", "auto"));\r
301 \r
302                 // DRC (-D)\r
303                 if (row.SubItems[5].Text != String.Empty)\r
304                     drcs.Add(row.SubItems[6].Text);\r
305             }\r
306 \r
307             // Audio Track (-a)\r
308             String audioItems = "";\r
309             Boolean firstLoop = true;\r
310 \r
311             foreach (String item in tracks)\r
312             {\r
313                 if (firstLoop)\r
314                 {\r
315                     audioItems = item; firstLoop = false;\r
316                 }\r
317                 else\r
318                     audioItems += "," + item;\r
319             }\r
320             if (audioItems.Trim() != String.Empty)\r
321                 query += " -a " + audioItems;\r
322             firstLoop = true; audioItems = ""; // Reset for another pass.\r
323 \r
324             // Audio Codec (-E)\r
325             foreach (String item in codecs)\r
326             {\r
327 \r
328                 if (firstLoop)\r
329                 {\r
330                     audioItems = item; firstLoop = false;\r
331                 }\r
332                 else\r
333                     audioItems += "," + item;\r
334             }\r
335             if (audioItems.Trim() != String.Empty)\r
336                 query += " -E " + audioItems;\r
337             firstLoop = true; audioItems = ""; // Reset for another pass.\r
338 \r
339             // Audio Mixdown (-6)\r
340             foreach (String item in mixdowns)\r
341             {\r
342                 if (firstLoop)\r
343                 {\r
344                     audioItems = item; firstLoop = false;\r
345                 }\r
346                 else\r
347                     audioItems += "," + item;\r
348             }\r
349             if (audioItems.Trim() != String.Empty)\r
350                 query += " -6 " + audioItems;\r
351             firstLoop = true; audioItems = ""; // Reset for another pass.\r
352 \r
353             // Sample Rate (-R)\r
354             foreach (String item in samplerates)\r
355             {\r
356                 if (firstLoop)\r
357                 {\r
358                     audioItems = item; firstLoop = false;\r
359                 }\r
360                 else\r
361                     audioItems += "," + item;\r
362             }\r
363             if (audioItems.Trim() != String.Empty)\r
364                 query += " -R " + audioItems;\r
365             firstLoop = true; audioItems = ""; // Reset for another pass.\r
366 \r
367             // Audio Bitrate (-B)\r
368             foreach (String item in bitrates)\r
369             {\r
370                 if (firstLoop)\r
371                 {\r
372                     audioItems = item; firstLoop = false;\r
373                 }\r
374                 else\r
375                     audioItems += "," + item;\r
376             }\r
377             if (audioItems.Trim() != String.Empty)\r
378                 query += " -B " + audioItems;\r
379             firstLoop = true; audioItems = ""; // Reset for another pass.\r
380 \r
381             // DRC (-D)\r
382             foreach (String item in drcs)\r
383             {\r
384                 if (firstLoop)\r
385                 {\r
386                     audioItems = item; firstLoop = false;\r
387                 }\r
388                 else\r
389                     audioItems += "," + item;\r
390             }\r
391             if (audioItems.Trim() != String.Empty)\r
392                 query += " -D " + audioItems;\r
393 \r
394             // Subtitles\r
395             string subtitles = mainWindow.drp_subtitle.Text;\r
396             if (subtitles == "Autoselect")\r
397                 query += " -U ";\r
398             else if (subtitles != "" && subtitles != "None")\r
399             {\r
400                 string[] tempSub = subtitles.Split(' ');\r
401                 query += " -s " + tempSub[0];\r
402             }\r
403 \r
404             if (mainWindow.check_forced.Checked)\r
405                 query += " -F ";\r
406 \r
407             #endregion\r
408 \r
409             #region Chapter Markers\r
410 \r
411             // Attach Source name and dvd title to the start of the chapters.csv filename.\r
412             // This is for the queue. It allows different chapter name files for each title.\r
413             string[] destName = mainWindow.text_destination.Text.Split('\\');\r
414             string dest_name = destName[destName.Length - 1];\r
415             dest_name = dest_name.Replace("\"", "");\r
416             dest_name = dest_name.Replace(".mp4", "").Replace(".m4v", "").Replace(".mkv", "");\r
417 \r
418             string source_title = mainWindow.drp_dvdtitle.Text;\r
419             string[] titlesplit = source_title.Split(' ');\r
420             source_title = titlesplit[0];\r
421 \r
422             if (mainWindow.Check_ChapterMarkers.Checked)\r
423             {\r
424                 if (dest_name.Trim() != String.Empty)\r
425                 {\r
426                     string path = source_title != "Automatic"\r
427                                       ? Path.Combine(Path.GetTempPath(), dest_name + "-" + source_title + "-chapters.csv")\r
428                                       : Path.Combine(Path.GetTempPath(), dest_name + "-chapters.csv");\r
429 \r
430                     if (chapterCSVSave(mainWindow, path) == false)\r
431                         query += " -m ";\r
432                     else\r
433                         query += " --markers=" + "\"" + path + "\"";\r
434                 }\r
435                 else\r
436                     query += " -m";\r
437             }\r
438             #endregion\r
439 \r
440             #region  H264 Tab\r
441             if (mainWindow.x264Panel.x264Query != "")\r
442                 query += " -x " + mainWindow.x264Panel.x264Query;\r
443             #endregion\r
444 \r
445             #region Processors / Other\r
446             string processors = Properties.Settings.Default.Processors;\r
447             if (processors != "Automatic")\r
448                 query += " -C " + processors + " ";\r
449 \r
450             query += " -v " + Properties.Settings.Default.verboseLevel;\r
451 \r
452             if (Properties.Settings.Default.dvdnav == "Checked")\r
453                 query += " --dvdnav";\r
454             #endregion\r
455 \r
456             return query;\r
457         }\r
458 \r
459         private static string getMixDown(string selectedAudio)\r
460         {\r
461             switch (selectedAudio)\r
462             {\r
463                 case "Automatic":\r
464                     return "auto";\r
465                 case "Mono":\r
466                     return "mono";\r
467                 case "Stereo":\r
468                     return "stereo";\r
469                 case "Dolby Surround":\r
470                     return "dpl1";\r
471                 case "Dolby Pro Logic II":\r
472                     return "dpl2";\r
473                 case "6 Channel Discrete":\r
474                     return "6ch";\r
475                 default:\r
476                     return "auto";\r
477             }\r
478         }\r
479         private static string getAudioEncoder(string selectedEncoder)\r
480         {\r
481             switch (selectedEncoder)\r
482             {\r
483                 case "AAC (faac)":\r
484                     return "faac";\r
485                 case "MP3 (lame)":\r
486                     return "lame";\r
487                 case "Vorbis (vorbis)":\r
488                     return "vorbis";\r
489                 case "AC3 Passthru":\r
490                     return "ac3";\r
491                 case "DTS Passthru":\r
492                     return "dts";\r
493                 default:\r
494                     return "";\r
495             }\r
496         }\r
497         private static Boolean chapterCSVSave(frmMain mainWindow, string filePathName)\r
498         {\r
499             try\r
500             {\r
501                 string csv = "";\r
502 \r
503                 foreach (DataGridViewRow row in mainWindow.data_chpt.Rows)\r
504                 {\r
505                     csv += row.Cells[0].Value.ToString();\r
506                     csv += ",";\r
507                     csv += row.Cells[1].Value.ToString();\r
508                     csv += Environment.NewLine;\r
509                 }\r
510                 StreamWriter file = new StreamWriter(filePathName);\r
511                 file.Write(csv);\r
512                 file.Close();\r
513                 file.Dispose();\r
514                 return true;\r
515 \r
516             }\r
517             catch (Exception exc)\r
518             {\r
519                 MessageBox.Show("Unable to save Chapter Makrers file! \nChapter marker names will NOT be saved in your encode \n\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
520                 return false;\r
521             }\r
522         }\r
523     }\r
524 }