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
8 using System.Collections;
\r
10 using System.Windows.Forms;
\r
11 using System.Globalization;
\r
13 using System.Drawing;
\r
14 using System.Diagnostics;
\r
15 using System.Text.RegularExpressions;
\r
17 namespace Handbrake.Functions
\r
23 /// This function takes in a Query which has been parsed by QueryParser and
\r
24 /// set's all the GUI widgets correctly.
\r
26 /// <param name="mainWindow"></param>
\r
27 /// <param name="presetQuery">The Parsed CLI Query</param>
\r
28 /// <param name="name">Name of the preset</param>
\r
29 public void presetLoader(frmMain mainWindow, Functions.QueryParser presetQuery, string name)
\r
31 // ---------------------------
\r
33 // ---------------------------
\r
37 // Reset some vaules to stock first to prevent errors.
\r
38 mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;
\r
40 // Now load all the new settings onto the main window
\r
41 mainWindow.drp_dvdtitle.Text = "Automatic";
\r
42 mainWindow.drop_chapterStart.Text = "Auto";
\r
43 mainWindow.drop_chapterFinish.Text = "Auto";
\r
45 if (presetQuery.Format != null)
\r
47 string destination = mainWindow.text_destination.Text;
\r
48 destination = destination.Replace(".mp4", "." + presetQuery.Format);
\r
49 destination = destination.Replace(".m4v", "." + presetQuery.Format);
\r
50 destination = destination.Replace(".avi", "." + presetQuery.Format);
\r
51 destination = destination.Replace(".mkv", "." + presetQuery.Format);
\r
52 destination = destination.Replace(".ogm", "." + presetQuery.Format);
\r
53 mainWindow.text_destination.Text = destination;
\r
61 mainWindow.drp_videoEncoder.Text = presetQuery.VideoEncoder;
\r
63 if (presetQuery.Format != null)
\r
65 if (presetQuery.Format == "mp4")
\r
66 mainWindow.drop_format.SelectedIndex = 0;
\r
67 else if (presetQuery.Format == "m4v")
\r
68 mainWindow.drop_format.SelectedIndex = 1;
\r
69 else if (presetQuery.Format == "mkv")
\r
70 mainWindow.drop_format.SelectedIndex = 2;
\r
71 else if (presetQuery.Format == "avi")
\r
72 mainWindow.drop_format.SelectedIndex = 3;
\r
73 else if (presetQuery.Format == "ogm")
\r
74 mainWindow.drop_format.SelectedIndex = 4;
\r
77 if (presetQuery.IpodAtom == true)
\r
78 mainWindow.check_iPodAtom.CheckState = CheckState.Checked;
\r
80 mainWindow.check_iPodAtom.CheckState = CheckState.Unchecked;
\r
82 if (presetQuery.OptimizeMP4 == true)
\r
83 mainWindow.check_optimiseMP4.CheckState = CheckState.Checked;
\r
85 mainWindow.check_optimiseMP4.CheckState = CheckState.Unchecked;
\r
89 // Picture Settings Tab
\r
91 mainWindow.check_autoCrop.Checked = true;
\r
92 mainWindow.drp_deInterlace_option.Text = presetQuery.DeInterlace;
\r
93 mainWindow.drp_deNoise.Text = presetQuery.DeNoise;
\r
95 if (presetQuery.Decomb == true)
\r
96 mainWindow.check_decomb.CheckState = CheckState.Checked;
\r
98 mainWindow.check_decomb.CheckState = CheckState.Unchecked;
\r
100 if (presetQuery.DeTelecine == true)
\r
101 mainWindow.check_detelecine.CheckState = CheckState.Checked;
\r
103 mainWindow.check_detelecine.CheckState = CheckState.Unchecked;
\r
105 if (presetQuery.DeBlock != 0)
\r
107 mainWindow.slider_deblock.Value = presetQuery.DeBlock;
\r
108 mainWindow.lbl_deblockVal.Text = presetQuery.DeBlock.ToString();
\r
112 mainWindow.slider_deblock.Value = 4;
\r
113 mainWindow.lbl_deblockVal.Text = "Off";
\r
116 if (presetQuery.Anamorphic == true)
\r
117 mainWindow.drp_anamorphic.SelectedIndex = 1;
\r
119 mainWindow.drp_anamorphic.SelectedIndex = 0;
\r
121 if (presetQuery.LooseAnamorphic == true)
\r
122 mainWindow.drp_anamorphic.SelectedIndex = 2;
\r
125 if (presetQuery.Anamorphic != true)
\r
126 mainWindow.drp_anamorphic.SelectedIndex = 0;
\r
129 if (presetQuery.Width != 0)
\r
130 mainWindow.text_width.Text = presetQuery.Width.ToString();
\r
133 mainWindow.text_width.Text = "";
\r
136 if (presetQuery.Height != 0)
\r
137 mainWindow.text_height.Text = presetQuery.Height.ToString();
\r
140 mainWindow.text_height.Text = "";
\r
145 // Video Settings Tab
\r
147 mainWindow.text_bitrate.Text = presetQuery.AverageVideoBitrate;
\r
148 mainWindow.text_filesize.Text = presetQuery.VideoTargetSize;
\r
149 mainWindow.slider_videoQuality.Value = presetQuery.VideoQuality;
\r
150 if (mainWindow.slider_videoQuality.Value != 0)
\r
152 int ql = presetQuery.VideoQuality;
\r
153 mainWindow.SliderValue.Text = ql.ToString() + "%";
\r
156 if (presetQuery.TwoPass == true)
\r
157 mainWindow.check_2PassEncode.CheckState = CheckState.Checked;
\r
159 mainWindow.check_2PassEncode.CheckState = CheckState.Unchecked;
\r
161 if (presetQuery.Grayscale == true)
\r
162 mainWindow.check_grayscale.CheckState = CheckState.Checked;
\r
164 mainWindow.check_grayscale.CheckState = CheckState.Unchecked;
\r
166 mainWindow.drp_videoFramerate.Text = presetQuery.VideoFramerate;
\r
168 if (presetQuery.TurboFirstPass == true)
\r
169 mainWindow.check_turbo.CheckState = CheckState.Checked;
\r
171 mainWindow.check_turbo.CheckState = CheckState.Unchecked;
\r
173 if (presetQuery.LargeMP4 == true)
\r
174 mainWindow.check_largeFile.CheckState = CheckState.Checked;
\r
177 mainWindow.check_largeFile.CheckState = CheckState.Unchecked;
\r
178 mainWindow.check_largeFile.BackColor = Color.Transparent;
\r
182 // Chapter Markers Tab
\r
183 #region Chapter Markers
\r
185 if (presetQuery.ChapterMarkers == true)
\r
186 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Checked;
\r
188 mainWindow.Check_ChapterMarkers.CheckState = CheckState.Unchecked;
\r
192 // Audio Settings Tab
\r
196 mainWindow.drp_track1Audio.Text = "Automatic";
\r
199 if (presetQuery.AudioEncoder2 != null) // Fix for loading in built in presets. Where 2 encoders but no tracks in the preset.
\r
201 mainWindow.drp_track2Audio.Enabled = true;
\r
202 mainWindow.drp_audsr_2.Enabled = true;
\r
203 mainWindow.drp_audmix_2.Enabled = true;
\r
204 mainWindow.drp_audenc_2.Enabled = true;
\r
205 mainWindow.drp_audbit_2.Enabled = true;
\r
206 mainWindow.drp_audsr_2.Text = "48";
\r
207 mainWindow.drp_track2Audio.Text = "Automatic";
\r
209 else if (presetQuery.AudioTrack2 == "None")
\r
211 mainWindow.drp_track2Audio.Text = "None";
\r
212 mainWindow.drp_track2Audio.SelectedIndex = 0;
\r
213 mainWindow.drp_audsr_2.Enabled = false;
\r
214 mainWindow.drp_audmix_2.Enabled = false;
\r
215 mainWindow.drp_audenc_2.Enabled = false;
\r
216 mainWindow.drp_audbit_2.Enabled = false;
\r
220 mainWindow.drp_track2Audio.Text = presetQuery.AudioTrack2;
\r
221 mainWindow.drp_audsr_2.Enabled = true;
\r
222 mainWindow.drp_audmix_2.Enabled = true;
\r
223 mainWindow.drp_audenc_2.Enabled = true;
\r
224 mainWindow.drp_audbit_2.Enabled = true;
\r
228 if (presetQuery.AudioTrack3 == "None")
\r
230 mainWindow.drp_track3Audio.SelectedIndex = 0;
\r
231 mainWindow.drp_audsr_3.Enabled = false;
\r
232 mainWindow.drp_audmix_3.Enabled = false;
\r
233 mainWindow.drp_audenc_3.Enabled = false;
\r
234 mainWindow.drp_audbit_3.Enabled = false;
\r
235 mainWindow.trackBar3.Enabled = false;
\r
237 mainWindow.drp_track3Audio.Text = "None";
\r
238 mainWindow.drp_audsr_3.Text = "";
\r
239 mainWindow.drp_audmix_3.Text = "Automatic";
\r
240 mainWindow.drp_audenc_3.Text = "";
\r
241 mainWindow.drp_audbit_3.Text = "";
\r
242 mainWindow.trackBar3.Value = 0;
\r
247 mainWindow.drp_track3Audio.Text = presetQuery.AudioTrack3;
\r
248 mainWindow.drp_audsr_3.Enabled = true;
\r
249 mainWindow.drp_audmix_3.Enabled = true;
\r
250 mainWindow.drp_audenc_3.Enabled = true;
\r
251 mainWindow.drp_audbit_3.Enabled = true;
\r
252 mainWindow.trackBar3.Enabled = true;
\r
256 if (presetQuery.AudioTrack4 == "None")
\r
258 mainWindow.drp_track4Audio.SelectedIndex = 0;
\r
259 mainWindow.drp_audsr_4.Enabled = false;
\r
260 mainWindow.drp_audmix_4.Enabled = false;
\r
261 mainWindow.drp_audenc_4.Enabled = false;
\r
262 mainWindow.drp_audbit_4.Enabled = false;
\r
263 mainWindow.trackBar4.Enabled = false;
\r
265 mainWindow.drp_track4Audio.Text = "None";
\r
266 mainWindow.drp_audsr_4.Text = "";
\r
267 mainWindow.drp_audmix_4.Text = "Automatic";
\r
268 mainWindow.drp_audenc_4.Text = "";
\r
269 mainWindow.drp_audbit_4.Text = "";
\r
270 mainWindow.trackBar4.Value = 0;
\r
274 mainWindow.drp_track4Audio.Text = presetQuery.AudioTrack4;
\r
275 mainWindow.drp_audsr_4.Enabled = true;
\r
276 mainWindow.drp_audmix_4.Enabled = true;
\r
277 mainWindow.drp_audenc_4.Enabled = true;
\r
278 mainWindow.drp_audbit_4.Enabled = true;
\r
279 mainWindow.trackBar4.Enabled = true;
\r
282 // Now lets start setting stuff
\r
283 if (presetQuery.AudioEncoder1 != null)
\r
284 mainWindow.drp_audenc_1.Text = presetQuery.AudioEncoder1;
\r
285 mainWindow.drp_audenc_2.Text = presetQuery.AudioEncoder2;
\r
286 mainWindow.drp_audenc_3.Text = presetQuery.AudioEncoder3;
\r
287 mainWindow.drp_audenc_4.Text = presetQuery.AudioEncoder4;
\r
289 mainWindow.drp_audmix_1.Text = presetQuery.AudioTrackMix1;
\r
290 mainWindow.drp_audmix_2.Text = presetQuery.AudioTrackMix2;
\r
291 mainWindow.drp_audmix_3.Text = presetQuery.AudioTrackMix3;
\r
292 mainWindow.drp_audmix_4.Text = presetQuery.AudioTrackMix4;
\r
294 if (presetQuery.AudioBitrate1 != null)
\r
295 mainWindow.drp_audbit_1.Text = presetQuery.AudioBitrate1;
\r
296 mainWindow.drp_audbit_2.Text = presetQuery.AudioBitrate2;
\r
297 mainWindow.drp_audbit_3.Text = presetQuery.AudioBitrate4;
\r
298 mainWindow.drp_audbit_3.Text = presetQuery.AudioBitrate4;
\r
300 if (presetQuery.AudioSamplerate1 != null)
\r
301 mainWindow.drp_audsr_1.Text = presetQuery.AudioSamplerate1;
\r
302 mainWindow.drp_audsr_2.Text = presetQuery.AudioSamplerate2;
\r
303 mainWindow.drp_audsr_3.Text = presetQuery.AudioSamplerate3;
\r
304 mainWindow.drp_audsr_4.Text = presetQuery.AudioSamplerate4;
\r
306 // Dynamic Range Compression (Should be a float but we use double for ease)
\r
308 double actualValue = 0;
\r
310 value = presetQuery.DRC1;
\r
312 value = value - 10;
\r
313 mainWindow.trackBar1.Value = int.Parse(value.ToString());
\r
314 actualValue = presetQuery.DRC1 / 10;
\r
315 mainWindow.lbl_drc1.Text = actualValue.ToString();
\r
317 value = presetQuery.DRC2;
\r
319 value = value - 10;
\r
320 mainWindow.trackBar2.Value = int.Parse(value.ToString());
\r
321 actualValue = presetQuery.DRC2 / 10;
\r
322 mainWindow.lbl_drc2.Text = actualValue.ToString();
\r
324 value = presetQuery.DRC3;
\r
326 value = value - 10;
\r
327 mainWindow.trackBar3.Value = int.Parse(value.ToString());
\r
328 actualValue = presetQuery.DRC3 / 10;
\r
329 mainWindow.lbl_drc3.Text = actualValue.ToString();
\r
331 value = presetQuery.DRC4;
\r
333 value = value - 10;
\r
334 mainWindow.trackBar4.Value = int.Parse(value.ToString());
\r
335 actualValue = presetQuery.DRC4 / 10;
\r
336 mainWindow.lbl_drc4.Text = actualValue.ToString();
\r
340 mainWindow.drp_subtitle.Text = presetQuery.Subtitles;
\r
342 if (presetQuery.ForcedSubtitles == true)
\r
344 mainWindow.check_forced.CheckState = CheckState.Checked;
\r
345 mainWindow.check_forced.Enabled = true;
\r
348 mainWindow.check_forced.CheckState = CheckState.Unchecked;
\r
353 // H264 Tab & Preset Name
\r
355 mainWindow.rtf_x264Query.Text = presetQuery.H264Query;
\r
357 // Set the preset name
\r
358 mainWindow.groupBox_output.Text = "Output Settings (Preset: " + name + ")";
\r
363 /// Select the longest title in the DVD title dropdown menu on frmMain
\r
365 public void selectLongestTitle(frmMain mainWindow)
\r
367 int current_largest = 0;
\r
368 Handbrake.Parsing.Title title2Select;
\r
370 // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"
\r
371 if (mainWindow.drp_dvdtitle.Items[0].ToString() != "Automatic")
\r
372 title2Select = (Handbrake.Parsing.Title)mainWindow.drp_dvdtitle.Items[0];
\r
374 title2Select = null;
\r
376 // So, If there are titles in the DVD Title dropdown menu, lets select the longest.
\r
377 if (title2Select != null)
\r
379 foreach (Handbrake.Parsing.Title x in mainWindow.drp_dvdtitle.Items)
\r
381 string title = x.ToString();
\r
382 if (title != "Automatic")
\r
384 string[] y = title.Split(' ');
\r
385 string time = y[1].Replace("(", "").Replace(")", "");
\r
386 string[] z = time.Split(':');
\r
388 int hours = int.Parse(z[0]) * 60 * 60;
\r
389 int minutes = int.Parse(z[1]) * 60;
\r
390 int seconds = int.Parse(z[2]);
\r
391 int total_sec = hours + minutes + seconds;
\r
393 if (current_largest == 0)
\r
395 current_largest = hours + minutes + seconds;
\r
400 if (total_sec > current_largest)
\r
402 current_largest = total_sec;
\r
409 // Now set the longest title in the gui.
\r
410 mainWindow.drp_dvdtitle.SelectedItem = title2Select;
\r
415 /// Set's up the DataGridView on the Chapters tab (frmMain)
\r
417 /// <param name="mainWindow"></param>
\r
418 public void chapterNaming(frmMain mainWindow)
\r
422 mainWindow.data_chpt.Rows.Clear();
\r
427 if (mainWindow.drop_chapterFinish.Text != "Auto")
\r
428 finish = int.Parse(mainWindow.drop_chapterFinish.Text);
\r
430 if (mainWindow.drop_chapterStart.Text != "Auto")
\r
431 start = int.Parse(mainWindow.drop_chapterStart.Text);
\r
433 rowCount = finish - (start - 1);
\r
435 while (i < rowCount)
\r
437 DataGridViewRow row = new DataGridViewRow();
\r
439 mainWindow.data_chpt.Rows.Insert(i, row);
\r
440 mainWindow.data_chpt.Rows[i].Cells[0].Value = (i + 1);
\r
441 mainWindow.data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1);
\r
445 catch (Exception exc)
\r
447 MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString());
\r
452 /// Function which generates the filename and path automatically based on
\r
453 /// the Source Name, DVD title and DVD Chapters
\r
455 /// <param name="mainWindow"></param>
\r
456 public void autoName(frmMain mainWindow)
\r
458 if (Properties.Settings.Default.autoNaming == "Checked")
\r
460 if (mainWindow.drp_dvdtitle.Text != "Automatic")
\r
462 // Todo: This code is a tad messy. Clean it up at some point.
\r
463 // Get the Source Name
\r
464 string source = mainWindow.text_source.Text;
\r
465 string[] sourceName = source.Split('\\');
\r
466 source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", "");
\r
468 // Get the Selected Title Number
\r
469 string title = mainWindow.drp_dvdtitle.Text;
\r
470 string[] titlesplit = title.Split(' ');
\r
471 title = titlesplit[0];
\r
473 // Get the Chapter Start and Chapter End Numbers
\r
474 string cs = mainWindow.drop_chapterStart.Text;
\r
475 string cf = mainWindow.drop_chapterFinish.Text;
\r
477 // Just incase the above are set to their default Automatic values, set the varible to ""
\r
478 if (title == "Automatic")
\r
485 // If both CS and CF are populated, set the dash varible
\r
490 // Get the destination filename.
\r
491 string destination_filename = "";
\r
492 if (Properties.Settings.Default.autoNameFormat != "")
\r
494 destination_filename = Properties.Settings.Default.autoNameFormat;
\r
495 destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf);
\r
498 destination_filename = source + "_T" + title + "_C" + cs + dash + cf;
\r
500 // If the text box is blank
\r
501 if (!mainWindow.text_destination.Text.Contains("\\"))
\r
503 string filePath = "";
\r
504 if (Properties.Settings.Default.autoNamePath.Trim() != "")
\r
506 if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")
\r
507 filePath = Properties.Settings.Default.autoNamePath + "\\";
\r
510 if (mainWindow.drop_format.SelectedIndex == 0)
\r
511 mainWindow.text_destination.Text = filePath + destination_filename + ".mp4";
\r
512 else if (mainWindow.drop_format.SelectedIndex == 1)
\r
513 mainWindow.text_destination.Text = filePath + destination_filename + ".m4v";
\r
514 else if (mainWindow.drop_format.SelectedIndex == 2)
\r
515 mainWindow.text_destination.Text = filePath + destination_filename + ".mkv";
\r
516 else if (mainWindow.drop_format.SelectedIndex == 3)
\r
517 mainWindow.text_destination.Text = filePath + destination_filename + ".avi";
\r
518 else if (mainWindow.drop_format.SelectedIndex == 4)
\r
519 mainWindow.text_destination.Text = filePath + destination_filename + ".ogm";
\r
521 else // If the text box already has a path and file
\r
523 string dest = mainWindow.text_destination.Text;
\r
524 string[] destName = dest.Split('\\');
\r
525 string[] extension = dest.Split('.');
\r
526 string ext = extension[extension.Length - 1];
\r
528 destName[destName.Length - 1] = destination_filename + "." + ext;
\r
530 string fullDest = "";
\r
531 foreach (string part in destName)
\r
533 if (fullDest != "")
\r
534 fullDest = fullDest + "\\" + part;
\r
536 fullDest = fullDest + part;
\r
539 mainWindow.text_destination.Text = fullDest;
\r
546 /// Checks for updates and returns true if an update is available.
\r
548 /// <param name="debug">Turns on debug mode. Don't use on program startup</param>
\r
549 /// <returns>Boolean True = Update available</returns>
\r
550 public Boolean updateCheck(Boolean debug)
\r
554 Functions.AppcastReader rssRead = new Functions.AppcastReader();
\r
555 rssRead.getInfo(); // Initializes the class.
\r
556 string build = rssRead.build();
\r
558 int latest = int.Parse(build);
\r
559 int current = Properties.Settings.Default.hb_build;
\r
560 int skip = Properties.Settings.Default.skipversion;
\r
562 if (latest == skip)
\r
566 Boolean update = (latest > current);
\r
570 catch (Exception exc)
\r
573 MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
579 /// Get's HandBrakes version data from the CLI.
\r
581 /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>
\r
582 public ArrayList getCliVersionData()
\r
584 ArrayList cliVersionData = new ArrayList();
\r
585 // 0 = SVN Build / Version
\r
588 Process cliProcess = new Process();
\r
589 ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u");
\r
590 handBrakeCLI.UseShellExecute = false;
\r
591 handBrakeCLI.RedirectStandardError = true;
\r
592 handBrakeCLI.RedirectStandardOutput = true;
\r
593 handBrakeCLI.CreateNoWindow = true;
\r
594 cliProcess.StartInfo = handBrakeCLI;
\r
595 cliProcess.Start();
\r
597 // Retrieve standard output and report back to parent thread until the process is complete
\r
599 TextReader stdOutput = cliProcess.StandardError;
\r
601 while (!cliProcess.HasExited)
\r
603 line = stdOutput.ReadLine();
\r
604 Match m = Regex.Match(line, @"HandBrake [0-9\.]*svn[0-9]*[M]* \([0-9]*\)");
\r
605 if (m.Success != false)
\r
607 string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", "");
\r
608 string[] arr = data.Split(' ');
\r
609 cliVersionData.Add(arr[0]);
\r
610 cliVersionData.Add(arr[1]);
\r
611 return cliVersionData;
\r
618 /// Check if the queue recovery file contains records.
\r
619 /// If it does, it means the last queue did not complete before HandBrake closed.
\r
620 /// So, return a boolean if true.
\r
622 public Boolean check_queue_recovery()
\r
626 string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.dat");
\r
627 using (StreamReader reader = new StreamReader(tempPath))
\r
629 string queue_item = reader.ReadLine();
\r
630 if (queue_item == null)
\r
636 else // There exists an item in the recovery queue file, so try and recovr it.
\r
646 // Keep quiet about the error.
\r