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
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").Replace(".avi", "_sample.avi").Replace(".ogm", "_sample.ogm") + '"';
\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 "MPEG-4 (XviD)":
\r
183 query += " -e xvid";
\r
185 case "H.264 (x264)":
\r
186 query += " -e x264";
\r
188 case "VP3 (Theora)":
\r
189 query += " -e theora";
\r
192 query += " -e x264";
\r
196 if (mainWindow.check_grayscale.Checked)
\r
200 if (mainWindow.radio_avgBitrate.Checked)
\r
201 query += " -b " + mainWindow.text_bitrate.Text;
\r
203 if (mainWindow.radio_targetFilesize.Checked)
\r
204 query += " -S " + mainWindow.text_filesize.Text;
\r
206 // Video Quality Setting
\r
207 if (mainWindow.radio_cq.Checked)
\r
210 switch (mainWindow.drp_videoEncoder.Text)
\r
212 case "MPEG-4 (FFmpeg)":
\r
213 value = 31 - (mainWindow.slider_videoQuality.Value -1);
\r
214 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
216 case "MPEG-4 (XviD)":
\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 float.TryParse(Properties.Settings.Default.x264cqstep, out divided);
\r
223 value = 51 - mainWindow.slider_videoQuality.Value * divided;
\r
224 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
226 case "VP3 (Theora)":
\r
227 value = mainWindow.slider_videoQuality.Value;
\r
228 query += " -q " + value.ToString(new CultureInfo("en-US"));
\r
233 if (mainWindow.check_2PassEncode.Checked)
\r
236 if (mainWindow.check_turbo.Checked)
\r
239 if (mainWindow.drp_videoFramerate.Text != "Same as source")
\r
240 query += " -r " + mainWindow.drp_videoFramerate.Text;
\r
243 #region Audio Settings Tab
\r
245 ListView audioTracks = mainWindow.lv_audioList;
\r
246 List<string> tracks = new List<string>();
\r
247 List<string> codecs = new List<string>();
\r
248 List<string> mixdowns = new List<string>();
\r
249 List<string> samplerates = new List<string>();
\r
250 List<string> bitrates = new List<string>();
\r
251 List<string> drcs = new List<string>();
\r
254 if (audioTracks.Items.Count == 0)
\r
255 query += " -a none ";
\r
257 // Gather information about each audio track and store them in the declared lists.
\r
258 foreach (ListViewItem row in audioTracks.Items)
\r
260 // Audio Track (-a)
\r
261 if (row.Text == "Automatic")
\r
263 else if (row.Text != "None")
\r
265 string[] tempSub = row.Text.Split(' ');
\r
266 tracks.Add(tempSub[0]);
\r
269 // Audio Codec (-E)
\r
270 if (row.SubItems[1].Text != String.Empty)
\r
271 codecs.Add(getAudioEncoder(row.SubItems[1].Text));
\r
273 // Audio Mixdown (-6)
\r
274 if (row.SubItems[2].Text != String.Empty)
\r
275 mixdowns.Add(getMixDown(row.SubItems[2].Text));
\r
277 // Sample Rate (-R)
\r
278 if (row.SubItems[3].Text != String.Empty)
\r
279 samplerates.Add(row.SubItems[3].Text.Replace("Auto", "0"));
\r
281 // Audio Bitrate (-B)
\r
282 if (row.SubItems[4].Text != String.Empty)
\r
283 bitrates.Add(row.SubItems[4].Text.Replace("Auto", "0"));
\r
286 if (row.SubItems[5].Text != String.Empty)
\r
287 drcs.Add(row.SubItems[5].Text);
\r
290 // Audio Track (-a)
\r
291 String audioItems = "";
\r
292 Boolean firstLoop = true;
\r
294 foreach (String item in tracks)
\r
298 audioItems = item; firstLoop = false;
\r
301 audioItems += "," + item;
\r
303 if (audioItems.Trim() != String.Empty)
\r
304 query += " -a " + audioItems;
\r
305 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
307 // Audio Codec (-E)
\r
308 foreach (String item in codecs)
\r
312 audioItems = item; firstLoop = false;
\r
315 audioItems += "," + item;
\r
317 if (audioItems.Trim() != String.Empty)
\r
318 query += " -E " + audioItems;
\r
319 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
321 // Audio Mixdown (-6)
\r
322 foreach (String item in mixdowns)
\r
326 audioItems = item; firstLoop = false;
\r
329 audioItems += "," + item;
\r
331 if (audioItems.Trim() != String.Empty)
\r
332 query += " -6 " + audioItems;
\r
333 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
335 // Sample Rate (-R)
\r
336 foreach (String item in samplerates)
\r
340 audioItems = item; firstLoop = false;
\r
343 audioItems += "," + item;
\r
345 if (audioItems.Trim() != String.Empty)
\r
346 query += " -R " + audioItems;
\r
347 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
349 // Audio Bitrate (-B)
\r
350 foreach (String item in bitrates)
\r
354 audioItems = item; firstLoop = false;
\r
357 audioItems += "," + item;
\r
359 if (audioItems.Trim() != String.Empty)
\r
360 query += " -B " + audioItems;
\r
361 firstLoop = true; audioItems = ""; // Reset for another pass.
\r
364 foreach (String item in drcs)
\r
368 audioItems = item; firstLoop = false;
\r
371 audioItems += "," + item;
\r
373 if (audioItems.Trim() != String.Empty)
\r
374 query += " -D " + audioItems;
\r
377 string subtitles = mainWindow.drp_subtitle.Text;
\r
378 if (subtitles == "Autoselect")
\r
380 else if (subtitles != "" && subtitles != "None")
\r
383 tempSub = subtitles.Split(' ');
\r
384 query += " -s " + tempSub[0];
\r
387 if (mainWindow.check_forced.Checked)
\r
392 #region Chapter Markers
\r
394 // Attach Source name and dvd title to the start of the chapters.csv filename.
\r
395 // This is for the queue. It allows different chapter name files for each title.
\r
396 string[] destName = mainWindow.text_destination.Text.Split('\\');
\r
397 string dest_name = destName[destName.Length - 1];
\r
398 dest_name = dest_name.Replace("\"", "");
\r
399 dest_name = dest_name.Replace(".mp4", "").Replace(".m4v", "").Replace(".avi", "").Replace(".mkv", "").Replace(".ogm", "");
\r
401 string source_title = mainWindow.drp_dvdtitle.Text;
\r
402 string[] titlesplit = source_title.Split(' ');
\r
403 source_title = titlesplit[0];
\r
405 if (mainWindow.Check_ChapterMarkers.Checked)
\r
407 if (dest_name.Trim() != String.Empty)
\r
410 if (source_title != "Automatic")
\r
411 path = Path.Combine(Path.GetTempPath(), dest_name + "-" + source_title + "-chapters.csv");
\r
413 path = Path.Combine(Path.GetTempPath(), dest_name + "-chapters.csv");
\r
415 if (chapterCSVSave(mainWindow, path) == false)
\r
418 query += " --markers=" + "\"" + path + "\"";
\r
426 if (mainWindow.rtf_x264Query.Text != "")
\r
427 query += " -x " + mainWindow.rtf_x264Query.Text;
\r
430 #region Processors / Other
\r
431 string processors = Properties.Settings.Default.Processors;
\r
432 if (processors != "Automatic")
\r
433 query += " -C " + processors + " ";
\r
442 /// Get the CLI equive of the audio mixdown from the widget name.
\r
444 /// <param name="selectedAudio"></param>
\r
445 /// <returns></returns>
\r
447 private string getMixDown(string selectedAudio)
\r
449 switch (selectedAudio)
\r
457 case "Dolby Surround":
\r
459 case "Dolby Pro Logic II":
\r
461 case "6 Channel Discrete":
\r
469 /// Get the CLI equiv of the audio encoder from the widget name.
\r
471 /// <param name="selectedEncoder"></param>
\r
472 /// <returns></returns>
\r
474 private string getAudioEncoder(string selectedEncoder)
\r
476 switch (selectedEncoder)
\r
492 /// This function saves the data in the chapters tab, dataGridView into a CSV file called chapters.csv
\r
493 /// in a directory specified by file_path_name
\r
495 /// <param name="mainWindow"></param>
\r
496 /// <param name="file_path_name"></param>
\r
497 /// <returns></returns>
\r
498 private Boolean chapterCSVSave(frmMain mainWindow, string file_path_name)
\r
502 StringBuilder csv = new StringBuilder();
\r
504 foreach (DataGridViewRow row in mainWindow.data_chpt.Rows)
\r
506 csv.Append(row.Cells[0].Value.ToString());
\r
508 csv.Append(row.Cells[1].Value.ToString());
\r
509 csv.Append(Environment.NewLine);
\r
511 StreamWriter file = new StreamWriter(file_path_name);
\r
512 file.Write(csv.ToString());
\r
518 catch (Exception exc)
\r
520 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