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 (mainWindow.drop_chapterFinish.Text == mainWindow.drop_chapterStart.Text && mainWindow.drop_chapterStart.Text != "Auto")
\r
38 query += " -c " + mainWindow.drop_chapterStart.Text;
\r
39 else if (mainWindow.drop_chapterStart.Text == "Auto" && mainWindow.drop_chapterFinish.Text != "Auto")
\r
40 query += " -c " + "0-" + mainWindow.drop_chapterFinish.Text;
\r
41 else if (mainWindow.drop_chapterStart.Text != "Auto" && mainWindow.drop_chapterFinish.Text != "Auto")
\r
42 query += " -c " + mainWindow.drop_chapterStart.Text + "-" + mainWindow.drop_chapterFinish.Text;
\r
45 if (mainWindow.text_destination.Text != "")
\r
46 query += " -o " + '"' + mainWindow.text_destination.Text + '"';
\r
48 query += generateTabbedComponentsQuery(mainWindow);
\r
53 /// Generates a CLI query for the preview function.
\r
54 /// This basically forces a shortened version of the encdode.
\r
56 /// <param name="mainWindow"></param>
\r
57 /// <param name="duration">Duration</param>
\r
58 /// <param name="preview">Start at preview</param>
\r
59 /// <returns>Returns a CLI query String.</returns>
\r
60 public string GeneratePreviewQuery(frmMain mainWindow, string duration, string preview)
\r
63 int.TryParse(duration, out seconds);
\r
68 if ((mainWindow.text_source.Text != "") && (mainWindow.text_source.Text.Trim() != "Click 'Source' to continue"))
\r
69 query = " -i " + '"' + mainWindow.text_source.Text + '"';
\r
71 if (mainWindow.drp_dvdtitle.Text != "Automatic")
\r
73 string[] titleInfo = mainWindow.drp_dvdtitle.Text.Split(' ');
\r
74 query += " -t " + titleInfo[0];
\r
77 query += " --start-at-preview " + preview;
\r
78 query += " --stop-at duration:" + duration + " ";
\r
81 if (mainWindow.text_destination.Text != "")
\r
82 query += " -o " + '"' + mainWindow.text_destination.Text.Replace(".m", "_sample.m") + '"';
\r
84 query += generateTabbedComponentsQuery(mainWindow);
\r
89 /// Generates part of the CLI query, for the tabbed components only.
\r
91 /// <param name="mainWindow"></param>
\r
92 /// <returns></returns>
\r
93 public string generateTabbedComponentsQuery(frmMain mainWindow)
\r
97 #region Output Settings Box
\r
98 query += " -f " + mainWindow.drop_format.Text.ToLower().Replace(" file", "");
\r
100 // These are output settings features
\r
101 if (mainWindow.check_largeFile.Checked)
\r
104 if (mainWindow.check_iPodAtom.Checked)
\r
107 if (mainWindow.check_optimiseMP4.Checked)
\r
111 #region Picture Settings Tab
\r
113 // Use MaxWidth for built-in presets and width for user settings.
\r
114 if (mainWindow.maxWidth == 0)
\r
117 if (mainWindow.text_width.Text != "")
\r
118 query += " -w " + mainWindow.text_width.Text;
\r
122 if (mainWindow.text_width.Text != "")
\r
123 query += " -X " + mainWindow.text_width.Text;
\r
126 // Use MaxHeight for built-in presets and height for user settings.
\r
127 if (mainWindow.maxHeight == 0)
\r
129 if (mainWindow.text_height.Text != "")
\r
130 query += " -l " + mainWindow.text_height.Text;
\r
134 if (mainWindow.text_height.Text != "")
\r
135 query += " -Y " + mainWindow.text_height.Text;
\r
138 string cropTop = mainWindow.text_top.Text;
\r
139 string cropBottom = mainWindow.text_bottom.Text;
\r
140 string cropLeft = mainWindow.text_left.Text;
\r
141 string cropRight = mainWindow.text_right.Text;
\r
143 if (mainWindow.check_customCrop.Checked)
\r
145 if (mainWindow.text_top.Text == string.Empty)
\r
147 if (mainWindow.text_bottom.Text == string.Empty)
\r
149 if (mainWindow.text_left.Text == string.Empty)
\r
151 if (mainWindow.text_right.Text == string.Empty)
\r
154 query += " --crop " + cropTop + ":" + cropBottom + ":" + cropLeft + ":" + cropRight;
\r
157 if (mainWindow.drp_anamorphic.SelectedIndex == 1)
\r
159 else if (mainWindow.drp_anamorphic.SelectedIndex == 2)
\r
162 if (mainWindow.slider_deblock.Value != 4)
\r
163 query += " --deblock=" + mainWindow.slider_deblock.Value;
\r
169 query += mainWindow.ctl_detelecine.getCLIQuery;
\r
170 query += mainWindow.ctl_decomb.getCLIQuery;
\r
171 query += mainWindow.ctl_deinterlace.getCLIQuery;
\r
172 query += mainWindow.ctl_denoise.getCLIQuery;
\r
175 #region Video Settings Tab
\r
177 switch (mainWindow.drp_videoEncoder.Text)
\r
179 case "MPEG-4 (FFmpeg)":
\r
180 query += " -e ffmpeg";
\r
182 case "H.264 (x264)":
\r
183 query += " -e x264";
\r
185 case "VP3 (Theora)":
\r
186 query += " -e theora";
\r
189 query += " -e x264";
\r
193 if (mainWindow.check_grayscale.Checked)
\r
197 if (mainWindow.radio_avgBitrate.Checked)
\r
198 query += " -b " + mainWindow.text_bitrate.Text;
\r
200 if (mainWindow.radio_targetFilesize.Checked)
\r
201 query += " -S " + mainWindow.text_filesize.Text;
\r
203 // Video Quality Setting
\r
204 if (mainWindow.radio_cq.Checked)
\r
207 switch (mainWindow.drp_videoEncoder.Text)
\r
209 case "MPEG-4 (FFmpeg)":
\r
210 value = 31 - (mainWindow.slider_videoQuality.Value -1);
\r
211 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
213 case "H.264 (x264)":
\r
215 CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
\r
216 double.TryParse(Properties.Settings.Default.x264cqstep,
\r
217 NumberStyles.Number,
\r
220 value = 51 - mainWindow.slider_videoQuality.Value * divided;
\r
221 value = Math.Round(value, 2);
\r
222 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
224 case "VP3 (Theora)":
\r
225 value = mainWindow.slider_videoQuality.Value;
\r
226 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
231 if (mainWindow.check_2PassEncode.Checked)
\r
234 if (mainWindow.check_turbo.Checked)
\r
237 if (mainWindow.drp_videoFramerate.Text != "Same as source")
\r
238 query += " -r " + mainWindow.drp_videoFramerate.Text;
\r
241 #region Audio Settings Tab
\r
243 ListView audioTracks = mainWindow.lv_audioList;
\r
244 List<string> tracks = new List<string>();
\r
245 List<string> codecs = new List<string>();
\r
246 List<string> mixdowns = new List<string>();
\r
247 List<string> samplerates = new List<string>();
\r
248 List<string> bitrates = new List<string>();
\r
249 List<string> drcs = new List<string>();
\r
252 if (audioTracks.Items.Count == 0)
\r
253 query += " -a none ";
\r
255 // Gather information about each audio track and store them in the declared lists.
\r
256 foreach (ListViewItem row in audioTracks.Items)
\r
258 // Audio Track (-a)
\r
259 if (row.Text == "Automatic")
\r
261 else if (row.Text != "None")
\r
263 string[] tempSub = row.Text.Split(' ');
\r
264 tracks.Add(tempSub[0]);
\r
267 // Audio Codec (-E)
\r
268 if (row.SubItems[1].Text != String.Empty)
\r
269 codecs.Add(getAudioEncoder(row.SubItems[1].Text));
\r
271 // Audio Mixdown (-6)
\r
272 if (row.SubItems[2].Text != String.Empty)
\r
273 mixdowns.Add(getMixDown(row.SubItems[2].Text));
\r
275 // Sample Rate (-R)
\r
276 if (row.SubItems[3].Text != String.Empty)
\r
277 samplerates.Add(row.SubItems[3].Text.Replace("Auto", "0"));
\r
279 // Audio Bitrate (-B)
\r
280 if (row.SubItems[4].Text != String.Empty)
\r
281 bitrates.Add(row.SubItems[4].Text.Replace("Auto", "0"));
\r
284 if (row.SubItems[5].Text != String.Empty)
\r
285 drcs.Add(row.SubItems[5].Text);
\r
288 // Audio Track (-a)
\r
289 String audioItems = "";
\r
290 Boolean firstLoop = true;
\r
292 foreach (String item in tracks)
\r
296 audioItems = item; firstLoop = false;
\r
299 audioItems += "," + item;
\r
301 if (audioItems.Trim() != String.Empty)
\r
302 query += " -a " + audioItems;
\r
303 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
305 // Audio Codec (-E)
\r
306 foreach (String item in codecs)
\r
310 audioItems = item; firstLoop = false;
\r
313 audioItems += "," + item;
\r
315 if (audioItems.Trim() != String.Empty)
\r
316 query += " -E " + audioItems;
\r
317 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
319 // Audio Mixdown (-6)
\r
320 foreach (String item in mixdowns)
\r
324 audioItems = item; firstLoop = false;
\r
327 audioItems += "," + item;
\r
329 if (audioItems.Trim() != String.Empty)
\r
330 query += " -6 " + audioItems;
\r
331 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
333 // Sample Rate (-R)
\r
334 foreach (String item in samplerates)
\r
338 audioItems = item; firstLoop = false;
\r
341 audioItems += "," + item;
\r
343 if (audioItems.Trim() != String.Empty)
\r
344 query += " -R " + audioItems;
\r
345 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
347 // Audio Bitrate (-B)
\r
348 foreach (String item in bitrates)
\r
352 audioItems = item; firstLoop = false;
\r
355 audioItems += "," + item;
\r
357 if (audioItems.Trim() != String.Empty)
\r
358 query += " -B " + audioItems;
\r
359 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
362 foreach (String item in drcs)
\r
366 audioItems = item; firstLoop = false;
\r
369 audioItems += "," + item;
\r
371 if (audioItems.Trim() != String.Empty)
\r
372 query += " -D " + audioItems;
\r
375 string subtitles = mainWindow.drp_subtitle.Text;
\r
376 if (subtitles == "Autoselect")
\r
378 else if (subtitles != "" && subtitles != "None")
\r
380 string[] tempSub = subtitles.Split(' ');
\r
381 query += " -s " + tempSub[0];
\r
384 if (mainWindow.check_forced.Checked)
\r
389 #region Chapter Markers
\r
391 // Attach Source name and dvd title to the start of the chapters.csv filename.
\r
392 // This is for the queue. It allows different chapter name files for each title.
\r
393 string[] destName = mainWindow.text_destination.Text.Split('\\');
\r
394 string dest_name = destName[destName.Length - 1];
\r
395 dest_name = dest_name.Replace("\"", "");
\r
396 dest_name = dest_name.Replace(".mp4", "").Replace(".m4v", "").Replace(".mkv", "");
\r
398 string source_title = mainWindow.drp_dvdtitle.Text;
\r
399 string[] titlesplit = source_title.Split(' ');
\r
400 source_title = titlesplit[0];
\r
402 if (mainWindow.Check_ChapterMarkers.Checked)
\r
404 if (dest_name.Trim() != String.Empty)
\r
406 string path = source_title != "Automatic"
\r
407 ? Path.Combine(Path.GetTempPath(), dest_name + "-" + source_title + "-chapters.csv")
\r
408 : Path.Combine(Path.GetTempPath(), dest_name + "-chapters.csv");
\r
410 if (chapterCSVSave(mainWindow, path) == false)
\r
413 query += " --markers=" + "\"" + path + "\"";
\r
421 if (mainWindow.x264Panel.x264Query != "")
\r
422 query += " -x " + mainWindow.x264Panel.x264Query;
\r
425 #region Processors / Other
\r
426 string processors = Properties.Settings.Default.Processors;
\r
427 if (processors != "Automatic")
\r
428 query += " -C " + processors + " ";
\r
430 query += " -v " + Properties.Settings.Default.verboseLevel;
\r
437 /// Get the CLI equive of the audio mixdown from the widget name.
\r
439 /// <param name="selectedAudio"></param>
\r
440 /// <returns></returns>
\r
442 private static string getMixDown(string selectedAudio)
\r
444 switch (selectedAudio)
\r
452 case "Dolby Surround":
\r
454 case "Dolby Pro Logic II":
\r
456 case "6 Channel Discrete":
\r
464 /// Get the CLI equiv of the audio encoder from the widget name.
\r
466 /// <param name="selectedEncoder"></param>
\r
467 /// <returns></returns>
\r
469 private static string getAudioEncoder(string selectedEncoder)
\r
471 switch (selectedEncoder)
\r
487 /// This function saves the data in the chapters tab, dataGridView into a CSV file called chapters.csv
\r
488 /// in a directory specified by file_path_name
\r
490 /// <param name="mainWindow"></param>
\r
491 /// <param name="file_path_name"></param>
\r
492 /// <returns></returns>
\r
493 private static Boolean chapterCSVSave(frmMain mainWindow, string file_path_name)
\r
497 StringBuilder csv = new StringBuilder();
\r
499 foreach (DataGridViewRow row in mainWindow.data_chpt.Rows)
\r
501 csv.Append(row.Cells[0].Value.ToString());
\r
503 csv.Append(row.Cells[1].Value.ToString());
\r
504 csv.Append(Environment.NewLine);
\r
506 StreamWriter file = new StreamWriter(file_path_name);
\r
507 file.Write(csv.ToString());
\r
513 catch (Exception exc)
\r
515 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