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