1 /* QueryGenerator.cs $
\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
9 using System.Windows.Forms;
\r
10 using System.Globalization;
\r
12 using System.Collections.Generic;
\r
14 namespace Handbrake.Functions
\r
16 class QueryGenerator
\r
19 /// Generates a CLI query based on the GUI widgets.
\r
21 /// <param name="mainWindow"></param>
\r
22 /// <returns>The CLI String</returns>
\r
23 public string GenerateTheQuery(frmMain mainWindow)
\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
31 if (mainWindow.drp_dvdtitle.Text != "Automatic")
\r
33 string[] titleInfo = mainWindow.drp_dvdtitle.Text.Split(' ');
\r
34 query += " -t " + titleInfo[0];
\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
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
49 if (mainWindow.text_destination.Text != "")
\r
50 query += " -o " + '"' + mainWindow.text_destination.Text + '"';
\r
52 query += generateTabbedComponentsQuery(mainWindow);
\r
57 /// Generates a CLI query for the preview function.
\r
58 /// This basically forces a shortened version of the encdode.
\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
67 int.TryParse(duration, out seconds);
\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
75 if (mainWindow.drp_dvdtitle.Text != "Automatic")
\r
77 string[] titleInfo = mainWindow.drp_dvdtitle.Text.Split(' ');
\r
78 query += " -t " + titleInfo[0];
\r
81 if (mainWindow.drop_angle.SelectedIndex != 0)
\r
82 query += " --angle " + mainWindow.drop_angle.SelectedItem;
\r
84 query += " --start-at-preview " + preview;
\r
85 query += " --stop-at duration:" + duration + " ";
\r
88 if (mainWindow.text_destination.Text != "")
\r
89 query += " -o " + '"' + mainWindow.text_destination.Text.Replace(".m", "_sample.m") + '"';
\r
91 query += generateTabbedComponentsQuery(mainWindow);
\r
96 /// Generates part of the CLI query, for the tabbed components only.
\r
98 /// <param name="mainWindow"></param>
\r
99 /// <returns></returns>
\r
100 public static string generateTabbedComponentsQuery(frmMain mainWindow)
\r
104 #region Output Settings Box
\r
105 query += " -f " + mainWindow.drop_format.Text.ToLower().Replace(" file", "");
\r
107 // These are output settings features
\r
108 if (mainWindow.check_largeFile.Checked)
\r
111 if (mainWindow.check_iPodAtom.Checked)
\r
114 if (mainWindow.check_optimiseMP4.Checked)
\r
118 #region Picture Settings Tab
\r
120 // Use MaxWidth for built-in presets and width for user settings.
\r
121 if (mainWindow.maxWidth == 0)
\r
124 if (mainWindow.text_width.Text != "")
\r
125 query += " -w " + mainWindow.text_width.Text;
\r
129 if (mainWindow.text_width.Text != "")
\r
130 query += " -X " + mainWindow.text_width.Text;
\r
133 // Use MaxHeight for built-in presets and height for user settings.
\r
134 if (mainWindow.maxHeight == 0)
\r
136 if (mainWindow.text_height.Text != "")
\r
137 query += " -l " + mainWindow.text_height.Text;
\r
141 if (mainWindow.text_height.Text != "")
\r
142 query += " -Y " + mainWindow.text_height.Text;
\r
145 string cropTop = mainWindow.text_top.Text;
\r
146 string cropBottom = mainWindow.text_bottom.Text;
\r
147 string cropLeft = mainWindow.text_left.Text;
\r
148 string cropRight = mainWindow.text_right.Text;
\r
150 if (mainWindow.check_customCrop.Checked)
\r
152 if (mainWindow.text_top.Text == string.Empty)
\r
154 if (mainWindow.text_bottom.Text == string.Empty)
\r
156 if (mainWindow.text_left.Text == string.Empty)
\r
158 if (mainWindow.text_right.Text == string.Empty)
\r
161 query += " --crop " + cropTop + ":" + cropBottom + ":" + cropLeft + ":" + cropRight;
\r
164 if (mainWindow.drp_anamorphic.SelectedIndex == 1)
\r
166 else if (mainWindow.drp_anamorphic.SelectedIndex == 2)
\r
169 if (mainWindow.slider_deblock.Value != 4)
\r
170 query += " --deblock=" + mainWindow.slider_deblock.Value;
\r
176 query += mainWindow.ctl_detelecine.getCLIQuery;
\r
177 query += mainWindow.ctl_decomb.getCLIQuery;
\r
178 query += mainWindow.ctl_deinterlace.getCLIQuery;
\r
179 query += mainWindow.ctl_denoise.getCLIQuery;
\r
182 #region Video Settings Tab
\r
184 switch (mainWindow.drp_videoEncoder.Text)
\r
186 case "MPEG-4 (FFmpeg)":
\r
187 query += " -e ffmpeg";
\r
189 case "H.264 (x264)":
\r
190 query += " -e x264";
\r
192 case "VP3 (Theora)":
\r
193 query += " -e theora";
\r
196 query += " -e x264";
\r
200 if (mainWindow.check_grayscale.Checked)
\r
204 if (mainWindow.radio_avgBitrate.Checked)
\r
205 query += " -b " + mainWindow.text_bitrate.Text;
\r
207 if (mainWindow.radio_targetFilesize.Checked)
\r
208 query += " -S " + mainWindow.text_filesize.Text;
\r
210 // Video Quality Setting
\r
211 if (mainWindow.radio_cq.Checked)
\r
214 switch (mainWindow.drp_videoEncoder.Text)
\r
216 case "MPEG-4 (FFmpeg)":
\r
217 value = 31 - (mainWindow.slider_videoQuality.Value -1);
\r
218 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
220 case "H.264 (x264)":
\r
222 CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
\r
223 double.TryParse(Properties.Settings.Default.x264cqstep,
\r
224 NumberStyles.Number,
\r
227 value = 51 - mainWindow.slider_videoQuality.Value * divided;
\r
228 value = Math.Round(value, 2);
\r
229 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
231 case "VP3 (Theora)":
\r
232 value = mainWindow.slider_videoQuality.Value;
\r
233 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
238 if (mainWindow.check_2PassEncode.Checked)
\r
241 if (mainWindow.check_turbo.Checked)
\r
244 if (mainWindow.drp_videoFramerate.Text != "Same as source")
\r
245 query += " -r " + mainWindow.drp_videoFramerate.Text;
\r
248 #region Audio Settings Tab
\r
250 ListView audioTracks = mainWindow.audioPanel.getAudioPanel();
\r
251 List<string> tracks = new List<string>();
\r
252 List<string> codecs = new List<string>();
\r
253 List<string> mixdowns = new List<string>();
\r
254 List<string> samplerates = new List<string>();
\r
255 List<string> bitrates = new List<string>();
\r
256 List<string> drcs = new List<string>();
\r
259 if (audioTracks.Items.Count == 0)
\r
260 query += " -a none ";
\r
262 // Gather information about each audio track and store them in the declared lists.
\r
263 foreach (ListViewItem row in audioTracks.Items)
\r
265 // Audio Track (-a)
\r
266 if (row.Text == "Automatic")
\r
268 else if (row.Text != "None")
\r
270 string[] tempSub = row.SubItems[1].Text.Split(' ');
\r
271 tracks.Add(tempSub[0]);
\r
274 // Audio Codec (-E)
\r
275 if (row.SubItems[1].Text != String.Empty)
\r
276 codecs.Add(getAudioEncoder(row.SubItems[2].Text));
\r
278 // Audio Mixdown (-6)
\r
279 if (row.SubItems[2].Text != String.Empty)
\r
280 mixdowns.Add(getMixDown(row.SubItems[3].Text));
\r
282 // Sample Rate (-R)
\r
283 if (row.SubItems[3].Text != String.Empty)
\r
284 samplerates.Add(row.SubItems[4].Text.Replace("Auto", "Auto"));
\r
286 // Audio Bitrate (-B)
\r
287 if (row.SubItems[4].Text != String.Empty)
\r
288 bitrates.Add(row.SubItems[5].Text.Replace("Auto", "auto"));
\r
291 if (row.SubItems[5].Text != String.Empty)
\r
292 drcs.Add(row.SubItems[6].Text);
\r
295 // Audio Track (-a)
\r
296 String audioItems = "";
\r
297 Boolean firstLoop = true;
\r
299 foreach (String item in tracks)
\r
303 audioItems = item; firstLoop = false;
\r
306 audioItems += "," + item;
\r
308 if (audioItems.Trim() != String.Empty)
\r
309 query += " -a " + audioItems;
\r
310 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
312 // Audio Codec (-E)
\r
313 foreach (String item in codecs)
\r
318 audioItems = item; firstLoop = false;
\r
321 audioItems += "," + item;
\r
323 if (audioItems.Trim() != String.Empty)
\r
324 query += " -E " + audioItems;
\r
325 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
327 // Audio Mixdown (-6)
\r
328 foreach (String item in mixdowns)
\r
332 audioItems = item; firstLoop = false;
\r
335 audioItems += "," + item;
\r
337 if (audioItems.Trim() != String.Empty)
\r
338 query += " -6 " + audioItems;
\r
339 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
341 // Sample Rate (-R)
\r
342 foreach (String item in samplerates)
\r
346 audioItems = item; firstLoop = false;
\r
349 audioItems += "," + item;
\r
351 if (audioItems.Trim() != String.Empty)
\r
352 query += " -R " + audioItems;
\r
353 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
355 // Audio Bitrate (-B)
\r
356 foreach (String item in bitrates)
\r
360 audioItems = item; firstLoop = false;
\r
363 audioItems += "," + item;
\r
365 if (audioItems.Trim() != String.Empty)
\r
366 query += " -B " + audioItems;
\r
367 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
370 foreach (String item in drcs)
\r
374 audioItems = item; firstLoop = false;
\r
377 audioItems += "," + item;
\r
379 if (audioItems.Trim() != String.Empty)
\r
380 query += " -D " + audioItems;
\r
383 string subtitles = mainWindow.drp_subtitle.Text;
\r
384 if (subtitles == "Autoselect")
\r
386 else if (subtitles != "" && subtitles != "None")
\r
388 string[] tempSub = subtitles.Split(' ');
\r
389 query += " -s " + tempSub[0];
\r
392 if (mainWindow.check_forced.Checked)
\r
397 #region Chapter Markers
\r
399 // Attach Source name and dvd title to the start of the chapters.csv filename.
\r
400 // This is for the queue. It allows different chapter name files for each title.
\r
401 string[] destName = mainWindow.text_destination.Text.Split('\\');
\r
402 string dest_name = destName[destName.Length - 1];
\r
403 dest_name = dest_name.Replace("\"", "");
\r
404 dest_name = dest_name.Replace(".mp4", "").Replace(".m4v", "").Replace(".mkv", "");
\r
406 string source_title = mainWindow.drp_dvdtitle.Text;
\r
407 string[] titlesplit = source_title.Split(' ');
\r
408 source_title = titlesplit[0];
\r
410 if (mainWindow.Check_ChapterMarkers.Checked)
\r
412 if (dest_name.Trim() != String.Empty)
\r
414 string path = source_title != "Automatic"
\r
415 ? Path.Combine(Path.GetTempPath(), dest_name + "-" + source_title + "-chapters.csv")
\r
416 : Path.Combine(Path.GetTempPath(), dest_name + "-chapters.csv");
\r
418 if (chapterCSVSave(mainWindow, path) == false)
\r
421 query += " --markers=" + "\"" + path + "\"";
\r
429 if (mainWindow.x264Panel.x264Query != "")
\r
430 query += " -x " + mainWindow.x264Panel.x264Query;
\r
433 #region Processors / Other
\r
434 string processors = Properties.Settings.Default.Processors;
\r
435 if (processors != "Automatic")
\r
436 query += " -C " + processors + " ";
\r
438 query += " -v " + Properties.Settings.Default.verboseLevel;
\r
440 if (Properties.Settings.Default.dvdnav =="Checked")
\r
441 query += " --dvdnav";
\r
448 /// Get the CLI equive of the audio mixdown from the widget name.
\r
450 /// <param name="selectedAudio"></param>
\r
451 /// <returns></returns>
\r
453 private static string getMixDown(string selectedAudio)
\r
455 switch (selectedAudio)
\r
463 case "Dolby Surround":
\r
465 case "Dolby Pro Logic II":
\r
467 case "6 Channel Discrete":
\r
475 /// Get the CLI equiv of the audio encoder from the widget name.
\r
477 /// <param name="selectedEncoder"></param>
\r
478 /// <returns></returns>
\r
480 private static string getAudioEncoder(string selectedEncoder)
\r
482 switch (selectedEncoder)
\r
488 case "Vorbis (vorbis)":
\r
490 case "AC3 Passthru":
\r
492 case "DTS Passthru":
\r
500 /// This function saves the data in the chapters tab, dataGridView into a CSV file called chapters.csv
\r
501 /// in a directory specified by file_path_name
\r
503 /// <param name="mainWindow"></param>
\r
504 /// <param name="file_path_name"></param>
\r
505 /// <returns></returns>
\r
506 private static Boolean chapterCSVSave(frmMain mainWindow, string file_path_name)
\r
510 StringBuilder csv = new StringBuilder();
\r
512 foreach (DataGridViewRow row in mainWindow.data_chpt.Rows)
\r
514 csv.Append(row.Cells[0].Value.ToString());
\r
516 csv.Append(row.Cells[1].Value.ToString());
\r
517 csv.Append(Environment.NewLine);
\r
519 StreamWriter file = new StreamWriter(file_path_name);
\r
520 file.Write(csv.ToString());
\r
526 catch (Exception exc)
\r
528 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