OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmMain.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Collections.Specialized;\r
4 using System.ComponentModel;\r
5 using System.Data;\r
6 using System.Drawing;\r
7 using System.Text;\r
8 using System.Windows.Forms;\r
9 using System.Net;\r
10 using System.IO;\r
11 using System.Diagnostics;\r
12 using System.Threading;\r
13 using System.Runtime.InteropServices;\r
14 using System.Globalization;\r
15 using System.Text.RegularExpressions;\r
16 \r
17 \r
18 namespace Handbrake\r
19 {\r
20     public partial class frmMain : Form\r
21     {\r
22         // -------------------------------------------------------------- \r
23         // Applicaiton Startup Stuff\r
24         // --------------------------------------------------------------\r
25 \r
26         #region Application Startup\r
27 \r
28         // Some stuff that needs to be Initialized. \r
29         private Process hbProc;\r
30         private Parsing.DVD thisDVD;\r
31         private frmQueue queueWindow = new frmQueue();\r
32 \r
33         // The main window beings here...\r
34         public frmMain()\r
35         {\r
36             // Load the splash screen in this thread\r
37             Form splash = new frmSplashScreen();\r
38             splash.Show();\r
39 \r
40             //Create a label that can be updated from the parent thread.\r
41             Label lblStatus = new Label();\r
42 \r
43             //Size the label\r
44             lblStatus.Size = new Size(250, 20);\r
45 \r
46             //Position the label\r
47             lblStatus.Location = new Point(10, 280);\r
48 \r
49             //Put the label on the splash form\r
50             splash.Controls.Add(lblStatus);\r
51 \r
52             //Fire a thread to wait for 2 seconds.  The splash screen will exit when the time expires\r
53             Thread timer = new Thread(splashTimer);\r
54             timer.Start();\r
55 \r
56             InitializeComponent();\r
57 \r
58             // show the form, but leave disabled until preloading is complete\r
59             this.Enabled = false;\r
60 \r
61             // Show the main form\r
62             this.Show();\r
63 \r
64             // Forces frmMain to draw\r
65             Application.DoEvents();\r
66 \r
67             // Set the Version number lable to the corect version.\r
68             Version.Text = "Version " + Properties.Settings.Default.CliVersion;\r
69 \r
70             // update the status\r
71             lblStatus.Text = "Checking for updates ...";\r
72             // redraw the splash screen\r
73             Application.DoEvents();\r
74             // Run the update checker.\r
75             updateCheck();\r
76             Thread.Sleep(200);\r
77 \r
78             // Update the presets\r
79             lblStatus.Text = "Updaing Presets ...";\r
80             Application.DoEvents();\r
81             updatePresets();\r
82             Thread.Sleep(200);\r
83 \r
84             // Now load the users default if required. (Will overide the above setting)\r
85             lblStatus.Text = "Loading User Default Settings...";\r
86             Application.DoEvents();\r
87             loadNormalPreset();\r
88             loadUserDefaults();\r
89             Thread.Sleep(100);\r
90 \r
91             // Enable or disable tooltips\r
92             lblStatus.Text = "Loading Tooltips ...";\r
93             Application.DoEvents();\r
94             tooltip();\r
95             Thread.Sleep(100);\r
96 \r
97             // Hide the preset bar if required.\r
98             hidePresetBar();\r
99 \r
100             //Finished Loading\r
101             lblStatus.Text = "Loading Complete!";\r
102             Application.DoEvents();\r
103             Thread.Sleep(200);\r
104 \r
105             // Wait until splash screen is done\r
106             while (timer.IsAlive)\r
107             { Thread.Sleep(100); }\r
108 \r
109             //Close the splash screen\r
110             splash.Close();\r
111             splash.Dispose();\r
112 \r
113             // Turn the interface back to the user\r
114             this.Enabled = true;\r
115         }\r
116 \r
117         private void splashTimer(object sender)\r
118         {\r
119             Thread.Sleep(1000);  //sit for 1 seconds then exit\r
120         }\r
121 \r
122         private void showSplash(object sender)\r
123         {\r
124             // Display splash screen for 1.5 Seconds\r
125             Form splash = new frmSplashScreen();\r
126             splash.Show();\r
127             Thread.Sleep(1500);\r
128             splash.Close(); // Then close.\r
129         }\r
130 \r
131         private void loadNormalPreset()\r
132         {\r
133             ListViewItem item = listview_presets.FindItemWithText("Normal");\r
134 \r
135             if (item != null)\r
136             {\r
137                 //listview_presets.SelectedItems.Clear();\r
138                 item.Selected = true;\r
139             }\r
140         }\r
141 \r
142         private void loadUserDefaults()\r
143         {\r
144             string userDefaults = Properties.Settings.Default.defaultUserSettings;\r
145             try\r
146             {\r
147                     // Some things that need to be done to reset some gui components:\r
148                     CheckPixelRatio.CheckState = CheckState.Unchecked;\r
149 \r
150                     // Send the query from the file to the Query Parser class\r
151                     Functions.QueryParser presetQuery = Functions.QueryParser.Parse(userDefaults);\r
152 \r
153                     // Now load the preset\r
154                     presetLoader(presetQuery, "User Defaults ");\r
155             }\r
156             catch (Exception exc)\r
157             {\r
158                     MessageBox.Show("Unable to load profile.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
159                     MessageBox.Show(exc.ToString());\r
160             }\r
161         }\r
162 \r
163         private Boolean updateCheck()\r
164         {\r
165             try\r
166             {\r
167                 if (Properties.Settings.Default.updateStatus == "Checked")\r
168                 {\r
169                     String updateFile = Properties.Settings.Default.updateFile;\r
170                     WebClient client = new WebClient();\r
171                     String data = client.DownloadString(updateFile);\r
172                     String[] versionData = data.Split('\n');\r
173 \r
174                     int verdata = int.Parse(versionData[0].Replace(".", ""));\r
175                     int vergui = int.Parse(Properties.Settings.Default.GuiVersion.Replace(".", ""));\r
176                     int verd1 = int.Parse(versionData[1].Replace(".", ""));\r
177                     int cliversion = int.Parse(Properties.Settings.Default.CliVersion.Replace(".", ""));\r
178 \r
179                     Boolean update = ((verdata > vergui) || (verd1 > cliversion));\r
180 \r
181                     lbl_update.Visible = update;\r
182 \r
183                     return update;\r
184                 }\r
185                 else\r
186                 {\r
187                     return false;\r
188                 }\r
189             }\r
190             catch (Exception)\r
191             {\r
192                 // Silently ignore the error\r
193                 return false;\r
194             }\r
195         }\r
196 \r
197         private void tooltip()\r
198         {\r
199             if (Properties.Settings.Default.tooltipEnable == "Checked")\r
200             {\r
201                 ToolTip.Active = true;\r
202             }\r
203         }\r
204 \r
205         private void hidePresetBar()\r
206         {\r
207             if (Properties.Settings.Default.hidePresets == "Checked")\r
208             {\r
209                 mnu_showPresets.Visible = true;\r
210                 this.Width = 591;\r
211             }\r
212 \r
213         }\r
214 \r
215         #endregion\r
216 \r
217         // -------------------------------------------------------------- \r
218         // The main Menu bar.\r
219         // -------------------------------------------------------------- \r
220 \r
221         #region File Menu\r
222         private void mnu_open_Click(object sender, EventArgs e)\r
223         {\r
224             string filename;\r
225             File_Open.ShowDialog();\r
226             filename = File_Open.FileName;\r
227 \r
228             if (filename != "")\r
229             {\r
230                 try\r
231                 {\r
232                     // Some things that need to be done to reset some gui components:\r
233                     CheckPixelRatio.CheckState = CheckState.Unchecked;\r
234 \r
235                     //---------------------------\r
236                     // Get the Preset\r
237                     // ---------------------------\r
238 \r
239                     // Create StreamReader & open file\r
240                     StreamReader line = new StreamReader(filename);\r
241 \r
242                     // Send the query from the file to the Query Parser class\r
243                     Functions.QueryParser presetQuery = Functions.QueryParser.Parse(line.ReadLine());\r
244 \r
245                     // Now load the preset\r
246                     presetLoader(presetQuery, filename);\r
247 \r
248                     // Close the stream\r
249                     line.Close();\r
250                 }\r
251                 catch (Exception exc)\r
252                 {\r
253                     MessageBox.Show("Unable to load profile.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
254                     MessageBox.Show(exc.ToString());\r
255                 }\r
256             }\r
257         }\r
258 \r
259         private void mnu_save_Click(object sender, EventArgs e)\r
260         {\r
261             string filename;\r
262             File_Save.ShowDialog();\r
263             filename = File_Save.FileName;\r
264             if (filename != "")\r
265             {\r
266                 try\r
267                 {\r
268                     // Create a StreamWriter and open the file\r
269                     StreamWriter line = new StreamWriter(filename);\r
270 \r
271                     // Generate and write the query string to the file\r
272                     String query = GenerateTheQuery();\r
273                     line.WriteLine(query);\r
274 \r
275                     // close the stream\r
276                     line.Close();\r
277                     MessageBox.Show("Your profile has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
278                 }\r
279                 catch (Exception)\r
280                 {\r
281                     MessageBox.Show("Unable to write to the file. Please make sure the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
282                 }\r
283 \r
284             }\r
285         }\r
286 \r
287         private void mnu_exit_Click(object sender, EventArgs e)\r
288         {\r
289             Application.Exit();\r
290         }\r
291 \r
292         #endregion\r
293 \r
294         #region Tools Menu\r
295 \r
296         private void mnu_encode_Click(object sender, EventArgs e)\r
297         {\r
298             showQueue();\r
299         }\r
300 \r
301         private void mnu_viewDVDdata_Click(object sender, EventArgs e)\r
302         {\r
303             frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
304             dvdInfoWindow.Show();\r
305         }\r
306 \r
307         private void mnu_options_Click(object sender, EventArgs e)\r
308         {\r
309             Form Options = new frmOptions();\r
310             Options.ShowDialog();\r
311         }\r
312 \r
313         #endregion\r
314 \r
315         #region Presets Menu\r
316         Boolean presetStatus;\r
317 \r
318         private void mnu_showPresets_Click(object sender, EventArgs e)\r
319         {\r
320             if (presetStatus == false)\r
321             {\r
322                 this.Width = 881;\r
323                 presetStatus = true;\r
324                 mnu_showPresets.Text = "Hide Presets";\r
325             }\r
326             else\r
327             {\r
328                 this.Width = 590;\r
329                 presetStatus = false;\r
330                 mnu_showPresets.Text = "Show Presets";\r
331             }\r
332         }\r
333 \r
334         private void mnu_presetReset_Click(object sender, EventArgs e)\r
335         {\r
336             listview_presets.Items.Clear();\r
337             updatePresets();\r
338             MessageBox.Show("Presets have been updated", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
339         }\r
340 \r
341         private void mnu_SelectDefault_Click(object sender, EventArgs e)\r
342         {\r
343             ListViewItem item = listview_presets.FindItemWithText("Normal");\r
344 \r
345             if (item != null)\r
346             {\r
347                 listview_presets.SelectedItems.Clear();\r
348                 item.Selected = true;\r
349             }\r
350         }\r
351 \r
352         #endregion\r
353 \r
354         #region Help Menu\r
355 \r
356 \r
357         private void mnu_quickStart_Click(object sender, EventArgs e)\r
358         {\r
359             Form QuickStart = new frmQuickStart();\r
360             QuickStart.ShowDialog();\r
361         }\r
362 \r
363         private void mnu_wiki_Click(object sender, EventArgs e)\r
364         {\r
365             Process.Start("http://handbrake.m0k.org/trac");\r
366         }\r
367 \r
368         private void mnu_faq_Click(object sender, EventArgs e)\r
369         {\r
370             Process.Start("http://handbrake.m0k.org/trac/wiki/WindowsGuiFaq");\r
371         }\r
372 \r
373         private void mnu_onlineDocs_Click(object sender, EventArgs e)\r
374         {\r
375             Process.Start("http://handbrake.m0k.org/?page_id=11");\r
376         }\r
377 \r
378         private void mnu_homepage_Click(object sender, EventArgs e)\r
379         {\r
380             Process.Start("http://handbrake.m0k.org");\r
381         }\r
382 \r
383         private void mnu_forum_Click(object sender, EventArgs e)\r
384         {\r
385             Process.Start("http://handbrake.m0k.org/forum");\r
386         }\r
387 \r
388         private void mnu_UpdateCheck_Click(object sender, EventArgs e)\r
389         {\r
390             Boolean update = updateCheck();\r
391             if (update == true)\r
392             {\r
393                 MessageBox.Show("There is a new update available. Please visit http://handbrake.m0k.org for details!", "Update Check", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
394             }\r
395             else\r
396             {\r
397                 MessageBox.Show("There are no new updates at this time.", "Update Check", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
398             }\r
399         }\r
400 \r
401         private void mnu_about_Click(object sender, EventArgs e)\r
402         {\r
403             Form About = new frmAbout();\r
404             About.ShowDialog();\r
405         }\r
406 \r
407         #endregion\r
408 \r
409         // -------------------------------------------------------------- \r
410         // Buttons on the main Window\r
411         // --------------------------------------------------------------\r
412 \r
413         #region Buttons\r
414 \r
415         private void btn_Browse_Click(object sender, EventArgs e)\r
416         {\r
417             String filename = "";\r
418             text_source.Text = "";\r
419             frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
420 \r
421             if (RadioDVD.Checked)\r
422             {\r
423                 DVD_Open.ShowDialog();\r
424                 filename = DVD_Open.SelectedPath;\r
425                 if (filename != "")\r
426                 {\r
427                     Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow);\r
428                     text_source.Text = filename;\r
429                     frmRD.ShowDialog();\r
430                 }\r
431             }\r
432             else\r
433             {\r
434                 ISO_Open.ShowDialog();\r
435                 filename = ISO_Open.FileName;\r
436                 if (filename != "")\r
437                 {\r
438                     Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow);\r
439                     text_source.Text = filename;\r
440                     frmRD.ShowDialog();\r
441                 }\r
442             }\r
443 \r
444             // Check if there was titles in the dvd title dropdown \r
445             if (filename == "")\r
446             {\r
447                 text_source.Text = "Click 'Browse' to continue";\r
448             }\r
449 \r
450             // If there are no titles in the dropdown menu then the scan has obviously failed. Display an error message explaining to the user.\r
451             if (drp_dvdtitle.Items.Count == 0)\r
452             {\r
453                 MessageBox.Show("No Title(s) found. Please make sure you have selected a valid, non-copy protected source. Please refer to the FAQ (see Help Menu).", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
454             }\r
455         }\r
456 \r
457         private void btn_destBrowse_Click(object sender, EventArgs e)\r
458         {\r
459             // This removes the file extension from the filename box on the save file dialog.\r
460             // It's daft but some users don't realise that typing an extension overrides the dropdown extension selected.\r
461             // Should be a nicer way to do this.\r
462             DVD_Save.FileName = DVD_Save.FileName.Replace(".mp4", "").Replace(".m4v", "").Replace(".mkv", "").Replace(".ogm", "").Replace(".avi", "");\r
463 \r
464             // Show the dialog and set the main form file path\r
465             DVD_Save.ShowDialog();\r
466             text_destination.Text = DVD_Save.FileName;\r
467 \r
468             // Quicktime requires .m4v file for chapter markers to work. If checked, change the extension to .m4v (mp4 and m4v are the same thing)\r
469             if (Check_ChapterMarkers.Checked)\r
470             {\r
471                 string destination = text_destination.Text;\r
472                 destination = destination.Replace(".mp4", ".m4v");\r
473                 text_destination.Text = destination;\r
474             }\r
475         }\r
476 \r
477         private void btn_h264Clear_Click(object sender, EventArgs e)\r
478         {\r
479             rtf_h264advanced.Text = "";\r
480         }\r
481 \r
482         private void GenerateQuery_Click(object sender, EventArgs e)\r
483         {\r
484             String query = GenerateTheQuery();\r
485             QueryEditorText.Text = query;\r
486         }\r
487 \r
488         private void btn_ClearQuery_Click(object sender, EventArgs e)\r
489         {\r
490             QueryEditorText.Text = "";\r
491         }\r
492 \r
493         private void btn_queue_Click(object sender, EventArgs e)\r
494         {\r
495 \r
496             if (text_source.Text == "" || text_source.Text == "Click 'Browse' to continue" || text_destination.Text == "")\r
497                 MessageBox.Show("No source OR destination selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
498             else\r
499             {\r
500                 string query;\r
501                 if (QueryEditorText.Text == "")\r
502                 {\r
503                     query = GenerateTheQuery();\r
504                 }\r
505                 else\r
506                 {\r
507                     query = QueryEditorText.Text;\r
508                 }\r
509                 queueWindow.list_queue.Items.Add(query);\r
510                 queueWindow.Show();\r
511             }\r
512         }\r
513 \r
514         private void btn_copy_Click(object sender, EventArgs e)\r
515         {\r
516             if (QueryEditorText.Text != "")\r
517                 Clipboard.SetText(QueryEditorText.Text, TextDataFormat.Text);\r
518         }\r
519 \r
520         private void showQueue()\r
521         {\r
522             queueWindow.Show();\r
523         }\r
524 \r
525         #endregion\r
526 \r
527         // -------------------------------------------------------------- \r
528         // Main Window Preset System\r
529         // --------------------------------------------------------------\r
530 \r
531         #region Preset System\r
532 \r
533         // Import Current Presets\r
534         private void updatePresets()\r
535         {\r
536             string[] presets = new string[17];\r
537             presets[0] = "Animation";\r
538             presets[1] = "AppleTV";\r
539             presets[2] = "Bedlam";\r
540             presets[3] = "Blind";\r
541             presets[4] = "Broke";\r
542             presets[5] = "Classic";\r
543             presets[6] = "Constant Quality Rate";\r
544             presets[7] = "Deux Six Quatre";\r
545             presets[8] = "Film";\r
546             presets[9] = "iPhone / iPod Touch";\r
547             presets[10] = "iPod High-Rez";\r
548             presets[11] = "iPod Low-Rez";\r
549             presets[12] = "Normal";\r
550             presets[13] = "PS3";\r
551             presets[14] = "PSP";\r
552             presets[15] = "QuickTime";\r
553             presets[16] = "Television";\r
554 \r
555             ListViewItem preset_listview = new ListViewItem();\r
556             string[] presetList = new string[1];\r
557 \r
558             foreach (string preset in presets)\r
559             {\r
560                 presetList[0] = preset;\r
561                 preset_listview = new ListViewItem(presetList);\r
562 \r
563                 // Now Fill Out List View with Items\r
564                 listview_presets.Items.Add(preset_listview);\r
565             }\r
566 \r
567             string appPath = Application.StartupPath.ToString() + "\\";\r
568             string strCmdLine = "cmd /c " + '"' + '"' + appPath + "HandBrakeCLI.exe" + '"' + " --preset-list >" + '"' + appPath + "presets.dat" + '"' + " 2>&1" + '"';\r
569             Process hbproc = Process.Start("CMD.exe", strCmdLine);\r
570             hbproc.WaitForExit();\r
571             hbproc.Dispose();\r
572             hbproc.Close();\r
573 \r
574         }\r
575 \r
576         // Buttons\r
577         private void btn_setDefault_Click(object sender, EventArgs e)\r
578         {\r
579             String query = GenerateTheQuery();\r
580             Properties.Settings.Default.defaultUserSettings = query;\r
581             // Save the new default Settings\r
582             Properties.Settings.Default.Save();\r
583             MessageBox.Show("New default settings saved.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
584         }\r
585 \r
586         // Preset Selection\r
587         private void listview_presets_SelectedIndexChanged(object sender, EventArgs e)\r
588         {\r
589 \r
590             string selectedPreset = null;\r
591             ListView.SelectedListViewItemCollection name = null;\r
592             name = listview_presets.SelectedItems;\r
593 \r
594             if (listview_presets.SelectedItems.Count != 0)\r
595                 selectedPreset = name[0].SubItems[0].Text;\r
596 \r
597             try\r
598             {\r
599                 string appPath = Application.StartupPath.ToString() + "\\";\r
600                 StreamReader presetInput = new StreamReader(appPath + "presets.dat");\r
601 \r
602                 while (!presetInput.EndOfStream)\r
603                 {\r
604                     if ((char)presetInput.Peek() == '+')\r
605                     {\r
606                         string preset = presetInput.ReadLine().Replace("+ ", "");\r
607                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
608                         string[] presetName = r.Split(preset);\r
609 \r
610                         if (selectedPreset == "iPhone / iPod Touch")\r
611                         {\r
612                             selectedPreset = "iPhone";\r
613                         }\r
614 \r
615                         if (selectedPreset == presetName[0])\r
616                         {\r
617                             // Need to disable anamorphic now, otherwise it may overide the width / height values later.\r
618                             CheckPixelRatio.CheckState = CheckState.Unchecked;\r
619 \r
620                             // Send the query from the file to the Query Parser class\r
621                             Functions.QueryParser presetQuery = Functions.QueryParser.Parse(preset);\r
622 \r
623                             // Now load the preset\r
624                             presetLoader(presetQuery, selectedPreset);\r
625                         }\r
626                         \r
627                     }\r
628                     else\r
629                     {\r
630                         presetInput.ReadLine();\r
631                     }\r
632                 }\r
633             }\r
634             catch (Exception exc)\r
635             {\r
636                 MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
637             }\r
638 \r
639            \r
640         }\r
641 \r
642         #endregion\r
643 \r
644         //---------------------------------------------------\r
645         // Encode / Cancel Buttons\r
646         // Encode Progress Text Handler\r
647         //---------------------------------------------------\r
648 \r
649         #region Encode/CLI\r
650 \r
651         Functions.CLI process = new Functions.CLI();\r
652 \r
653         private void btn_encode_Click(object sender, EventArgs e)\r
654         {\r
655             if (text_source.Text == "" || text_source.Text == "Click 'Browse' to continue" || text_destination.Text == "")\r
656                 MessageBox.Show("No source OR destination selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
657             else\r
658             {\r
659                 btn_eCancel.Enabled = true;\r
660                 String query = "";\r
661                 if (QueryEditorText.Text == "")\r
662                 {\r
663                     query = GenerateTheQuery();\r
664                 }\r
665                 else\r
666                 {\r
667                     query = QueryEditorText.Text;\r
668                 }\r
669 \r
670                 ThreadPool.QueueUserWorkItem(procMonitor, query);\r
671                 lbl_encode.Visible = true;\r
672                 lbl_encode.Text = "Encoding in Progress";\r
673             }\r
674         }\r
675 \r
676         private void btn_eCancel_Click(object sender, EventArgs e)\r
677         {\r
678             process.killCLI();\r
679             process.setNull();\r
680             lbl_encode.Text = "Encoding Canceled";\r
681         }\r
682 \r
683         [DllImport("user32.dll")]\r
684         public static extern void LockWorkStation();\r
685         [DllImport("user32.dll")]\r
686         public static extern int ExitWindowsEx(int uFlags, int dwReason);\r
687 \r
688         private void procMonitor(object state)\r
689         {\r
690             // Make sure we are not already encoding and if we are then display an error.\r
691             if (hbProc != null)\r
692             {\r
693                 MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
694             }\r
695             else\r
696             {\r
697                 hbProc = process.runCli(this, (string)state, false, false, false, false);\r
698                 hbProc.WaitForExit();\r
699 \r
700                 try\r
701                 {\r
702                     /*\r
703                     //*****************************************************************************************\r
704                     // BUG!\r
705                     // When the below code is used and standard error is set to true, hbcli is outputing a\r
706                     // video stream which has mild corruption issues every few seconds.\r
707                     // Maybe an issue with the Parser cauing the CLI to hickup/pause?\r
708                     //*****************************************************************************************\r
709 \r
710                     \r
711                     Parsing.Parser encode = new Parsing.Parser(hbProc.StandardOutput.BaseStream);\r
712                     encode.OnEncodeProgress += encode_OnEncodeProgress;\r
713                     while (!encode.EndOfStream)\r
714                     {\r
715                         encode.ReadLine();\r
716                     }\r
717 \r
718                     hbProc.WaitForExit();\r
719                     process.closeCLI();\r
720                      */\r
721 \r
722                 }\r
723                 catch (Exception exc)\r
724                 {\r
725                     // Do nothing\r
726                     MessageBox.Show(exc.ToString());\r
727                 }\r
728 \r
729 \r
730                 setEncodeLabel();\r
731                 hbProc = null;\r
732 \r
733                 // Do something whent he encode ends.\r
734                 switch (Properties.Settings.Default.CompletionOption)\r
735                 {\r
736                     case "Shutdown":\r
737                         System.Diagnostics.Process.Start("Shutdown", "-s -t 60");\r
738                         break;\r
739                     case "Log Off":\r
740                         ExitWindowsEx(0, 0);\r
741                         break;\r
742                     case "Suspend":\r
743                         Application.SetSuspendState(PowerState.Suspend, true, true);\r
744                         break;\r
745                     case "Hibernate":\r
746                         Application.SetSuspendState(PowerState.Hibernate, true, true);\r
747                         break;\r
748                     case "Lock System":\r
749                         LockWorkStation();\r
750                         break;\r
751                     case "Quit HandBrake":\r
752                         Application.Exit();\r
753                         break;\r
754                     default:\r
755                         break;\r
756                 }\r
757             }\r
758         }\r
759 \r
760         private delegate void UpdateUIHandler();\r
761 \r
762         private void setEncodeLabel()\r
763         {\r
764             if (this.InvokeRequired)\r
765             {\r
766                 this.BeginInvoke(new UpdateUIHandler(setEncodeLabel));\r
767                 return;\r
768             }\r
769             lbl_encode.Text = "Encoding Finished";\r
770         }\r
771 \r
772         private void encode_OnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)\r
773         {\r
774 \r
775             if (this.InvokeRequired)\r
776             {\r
777                 this.BeginInvoke(new Parsing.EncodeProgressEventHandler(encode_OnEncodeProgress),\r
778                     new object[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });\r
779                 return;\r
780             }\r
781             lbl_encode.Text = string.Format("Encode Progress: {0}%,       FPS: {1},       Avg FPS: {2},       Time Remaining: {3} ", PercentComplete, CurrentFps, AverageFps, TimeRemaining);\r
782         }\r
783 \r
784         #endregion\r
785 \r
786         //---------------------------------------------------\r
787         //  Items that require actions on frmMain\r
788         //---------------------------------------------------\r
789 \r
790         #region frmMain Actions\r
791 \r
792         private void drop_chapterStart_SelectedIndexChanged(object sender, EventArgs e)\r
793         {\r
794             drop_chapterStart.BackColor = Color.White;\r
795             QueryEditorText.Text = "";\r
796             if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
797             {\r
798                 try\r
799                 {\r
800                     int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
801                     int chapterStart = int.Parse(drop_chapterStart.Text);\r
802 \r
803                     if (chapterFinish < chapterStart)\r
804                     {\r
805                         drop_chapterStart.BackColor = Color.LightCoral;\r
806                     }\r
807                 }\r
808                 catch (Exception)\r
809                 {\r
810                     drop_chapterStart.BackColor = Color.LightCoral;\r
811                 }\r
812             }\r
813 \r
814 \r
815         }\r
816 \r
817         private void drop_chapterFinish_SelectedIndexChanged(object sender, EventArgs e)\r
818         {\r
819             drop_chapterFinish.BackColor = Color.White;\r
820             QueryEditorText.Text = "";\r
821             if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
822             {\r
823                 try\r
824                 {\r
825                     int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
826                     int chapterStart = int.Parse(drop_chapterStart.Text);\r
827 \r
828                     if (chapterFinish < chapterStart)\r
829                     {\r
830                         drop_chapterFinish.BackColor = Color.LightCoral;\r
831                     }\r
832                 }\r
833                 catch (Exception)\r
834                 {\r
835                     drop_chapterFinish.BackColor = Color.LightCoral;\r
836                 }\r
837             }\r
838         }\r
839 \r
840         private void text_bitrate_TextChanged(object sender, EventArgs e)\r
841         {\r
842             text_filesize.Text = "";\r
843             slider_videoQuality.Value = 0;\r
844             SliderValue.Text = "0%";\r
845             CheckCRF.CheckState = CheckState.Unchecked;\r
846             CheckCRF.Enabled = false;\r
847         }\r
848 \r
849         private void text_filesize_TextChanged(object sender, EventArgs e)\r
850         {\r
851             text_bitrate.Text = "";\r
852             slider_videoQuality.Value = 0;\r
853             SliderValue.Text = "0%";\r
854             CheckCRF.CheckState = CheckState.Unchecked;\r
855             CheckCRF.Enabled = false;\r
856         }\r
857 \r
858         private void slider_videoQuality_Scroll(object sender, EventArgs e)\r
859         {\r
860             SliderValue.Text = slider_videoQuality.Value.ToString() + "%";\r
861             text_bitrate.Text = "";\r
862             text_filesize.Text = "";\r
863             CheckCRF.Enabled = true;\r
864         }\r
865 \r
866         private void label_h264_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
867         {\r
868             Process.Start("http://handbrake.m0k.org/trac/wiki/x264Options");\r
869         }\r
870 \r
871         private void text_width_TextChanged(object sender, EventArgs e)\r
872         {\r
873             try\r
874             {\r
875                 if (CheckPixelRatio.Checked)\r
876                 {\r
877                     text_width.Text = "";\r
878                     text_width.BackColor = Color.LightCoral;\r
879                     CheckPixelRatio.BackColor = Color.LightCoral;\r
880                 }\r
881                 else\r
882                 {\r
883                     if ((int.Parse(text_width.Text) % 16) != 0)\r
884                     {\r
885                         text_width.BackColor = Color.LightCoral;\r
886                     }\r
887                     else\r
888                     {\r
889                         text_width.BackColor = Color.LightGreen;\r
890                     }\r
891                 }\r
892 \r
893                 if (lbl_Aspect.Text != "Select a Title")\r
894                 {\r
895                     double height = int.Parse(text_width.Text) / double.Parse(lbl_Aspect.Text);\r
896                     double mod16 = height % 16;\r
897                     height = height - mod16;\r
898 \r
899                     if (text_width.Text == "")\r
900                     {\r
901                         text_height.Text = "";\r
902                         text_width.BackColor = Color.White;\r
903                     }\r
904                     else\r
905                     {\r
906                         text_height.Text = height.ToString();\r
907                         text_width.BackColor = Color.LightGreen;\r
908                     }\r
909                 }\r
910             }\r
911             catch (Exception)\r
912             {\r
913                 // No need to throw an error here.\r
914                 // Note on non english systems, this will throw an error because of double.Parse(lbl_Aspect.Text); not working.\r
915             }\r
916 \r
917 \r
918         }\r
919 \r
920         private void text_height_TextChanged(object sender, EventArgs e)\r
921         {\r
922             try\r
923             {\r
924                 if (CheckPixelRatio.Checked)\r
925                 {\r
926                     text_height.Text = "";\r
927                     text_height.BackColor = Color.LightCoral;\r
928                     CheckPixelRatio.BackColor = Color.LightCoral;\r
929                 }\r
930                 else\r
931                 {\r
932                     if ((int.Parse(text_height.Text) % 16) != 0)\r
933                     {\r
934                         text_height.BackColor = Color.LightCoral;\r
935                     }\r
936                     else\r
937                     {\r
938                         text_height.BackColor = Color.LightGreen;\r
939                     }\r
940                 }\r
941 \r
942             }\r
943             catch (Exception)\r
944             {\r
945                 // No need to alert the user.\r
946             }\r
947         }\r
948 \r
949         private void drp_crop_SelectedIndexChanged(object sender, EventArgs e)\r
950         {\r
951             if ((string)drp_crop.SelectedItem == "Manual")\r
952             {\r
953                 text_left.Enabled = true;\r
954                 text_right.Enabled = true;\r
955                 text_top.Enabled = true;\r
956                 text_bottom.Enabled = true;\r
957             }\r
958 \r
959             if ((string)drp_crop.SelectedItem == "Auto Crop")\r
960             {\r
961                 text_left.Enabled = false;\r
962                 text_right.Enabled = false;\r
963                 text_top.Enabled = false;\r
964                 text_bottom.Enabled = false;\r
965                 text_left.Text = "";\r
966                 text_right.Text = "";\r
967                 text_top.Text = "";\r
968                 text_bottom.Text = "";\r
969 \r
970                 if (lbl_RecomendedCrop.Text != "Select a Title")\r
971                 {\r
972                     string[] temp = new string[4];\r
973                     temp = lbl_RecomendedCrop.Text.Split('/');\r
974                     text_left.Text = temp[2];\r
975                     text_right.Text = temp[3];\r
976                     text_top.Text = temp[0];\r
977                     text_bottom.Text = temp[1];\r
978                 }\r
979             }\r
980 \r
981             if ((string)drp_crop.SelectedItem == "No Crop")\r
982             {\r
983                 text_left.Enabled = false;\r
984                 text_right.Enabled = false;\r
985                 text_top.Enabled = false;\r
986                 text_bottom.Enabled = false;\r
987                 text_left.Text = "0";\r
988                 text_right.Text = "0";\r
989                 text_top.Text = "0";\r
990                 text_bottom.Text = "0";\r
991 \r
992             }\r
993         }\r
994 \r
995         private void CheckPixelRatio_CheckedChanged(object sender, EventArgs e)\r
996         {\r
997             text_width.Text = "";\r
998             text_height.Text = "";\r
999             text_width.BackColor = Color.White;\r
1000             text_height.BackColor = Color.White;\r
1001             CheckPixelRatio.BackColor = TabPage1.BackColor;\r
1002         }\r
1003 \r
1004         private void check_2PassEncode_CheckedChanged(object sender, EventArgs e)\r
1005         {\r
1006             if (check_2PassEncode.CheckState.ToString() == "Checked")\r
1007             {\r
1008                 if (drp_videoEncoder.Text.Contains("H.264"))\r
1009                 {\r
1010                     check_turbo.Enabled = true;\r
1011                 }\r
1012             }\r
1013             else\r
1014             {\r
1015                 check_turbo.Enabled = false;\r
1016                 check_turbo.CheckState = CheckState.Unchecked;\r
1017             }\r
1018         }\r
1019 \r
1020         private void check_largeFile_CheckedChanged(object sender, EventArgs e)\r
1021         {\r
1022             if (!text_destination.Text.Contains(".mp4"))\r
1023             {\r
1024                 lbl_largeMp4Warning.Text = "Warning: Only mp4 files are supported";\r
1025                 lbl_largeMp4Warning.ForeColor = Color.Red;\r
1026                 check_largeFile.CheckState = CheckState.Unchecked;\r
1027             }\r
1028             else\r
1029             {\r
1030                 lbl_largeMp4Warning.Text = "Warning: Breaks iPod, @TV, PS3 compatibility.";\r
1031                 lbl_largeMp4Warning.ForeColor = Color.Black;\r
1032             }\r
1033         }\r
1034 \r
1035         private void drp_dvdtitle_Click(object sender, EventArgs e)\r
1036         {\r
1037             if (drp_dvdtitle.Items.Count == 0)\r
1038             {\r
1039                 MessageBox.Show("There are no titles to select. Please scan the DVD by clicking the 'browse' button above before trying to select a title.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
1040             }\r
1041         }\r
1042 \r
1043         private void drp_audioCodec_SelectedIndexChanged(object sender, EventArgs e)\r
1044         {\r
1045 \r
1046             //CLI Audio mixdown Names: mono stereo dpl1 dpl2 6ch\r
1047 \r
1048             drp_audioMixDown.Items.Clear();\r
1049 \r
1050             if (drp_audioCodec.Text == "AAC")\r
1051             {\r
1052                 drp_audioMixDown.Items.Clear();\r
1053                 drp_audioMixDown.Items.Add("Mono");\r
1054                 drp_audioMixDown.Items.Add("Stereo");\r
1055                 drp_audioMixDown.Items.Add("Dolby Surround");\r
1056                 drp_audioMixDown.Items.Add("Dolby Pro Logic II");\r
1057                 drp_audioMixDown.Items.Add("6 Channel Discrete");\r
1058 \r
1059                 drp_audioBitrate.Items.Clear();\r
1060                 drp_audioBitrate.Items.Add("32");\r
1061                 drp_audioBitrate.Items.Add("40");\r
1062                 drp_audioBitrate.Items.Add("48");\r
1063                 drp_audioBitrate.Items.Add("56");\r
1064                 drp_audioBitrate.Items.Add("64");\r
1065                 drp_audioBitrate.Items.Add("80");\r
1066                 drp_audioBitrate.Items.Add("86");\r
1067                 drp_audioBitrate.Items.Add("112");\r
1068                 drp_audioBitrate.Items.Add("128");\r
1069                 drp_audioBitrate.Items.Add("160");\r
1070 \r
1071             }\r
1072             else\r
1073             {\r
1074                 drp_audioMixDown.Items.Clear();\r
1075                 drp_audioMixDown.Items.Add("Stereo");\r
1076                 drp_audioMixDown.Items.Add("Dolby Surround");\r
1077                 drp_audioMixDown.Items.Add("Dolby Pro Logic II");\r
1078 \r
1079                 drp_audioBitrate.Items.Clear();\r
1080                 drp_audioBitrate.Items.Add("32");\r
1081                 drp_audioBitrate.Items.Add("40");\r
1082                 drp_audioBitrate.Items.Add("48");\r
1083                 drp_audioBitrate.Items.Add("56");\r
1084                 drp_audioBitrate.Items.Add("64");\r
1085                 drp_audioBitrate.Items.Add("80");\r
1086                 drp_audioBitrate.Items.Add("86");\r
1087                 drp_audioBitrate.Items.Add("112");\r
1088                 drp_audioBitrate.Items.Add("128");\r
1089                 drp_audioBitrate.Items.Add("160");\r
1090                 drp_audioBitrate.Items.Add("192");\r
1091                 drp_audioBitrate.Items.Add("224");\r
1092                 drp_audioBitrate.Items.Add("256");\r
1093                 drp_audioBitrate.Items.Add("320");\r
1094             }\r
1095         }\r
1096 \r
1097         private void drp_audioMixDown_SelectedIndexChanged(object sender, EventArgs e)\r
1098         {\r
1099             if (drp_audioCodec.Text == "AAC")\r
1100             {\r
1101                 if (drp_audioMixDown.Text == "6 Channel Discrete")\r
1102                 {\r
1103 \r
1104                     drp_audioBitrate.Items.Clear();\r
1105                     drp_audioBitrate.Items.Add("32");\r
1106                     drp_audioBitrate.Items.Add("40");\r
1107                     drp_audioBitrate.Items.Add("48");\r
1108                     drp_audioBitrate.Items.Add("56");\r
1109                     drp_audioBitrate.Items.Add("64");\r
1110                     drp_audioBitrate.Items.Add("80");\r
1111                     drp_audioBitrate.Items.Add("86");\r
1112                     drp_audioBitrate.Items.Add("112");\r
1113                     drp_audioBitrate.Items.Add("128");\r
1114                     drp_audioBitrate.Items.Add("160");\r
1115                     drp_audioBitrate.Items.Add("192");\r
1116                     drp_audioBitrate.Items.Add("224");\r
1117                     drp_audioBitrate.Items.Add("256");\r
1118                     drp_audioBitrate.Items.Add("320");\r
1119                     drp_audioBitrate.Items.Add("384");\r
1120                 }\r
1121             }\r
1122         }\r
1123 \r
1124         private void Check_ChapterMarkers_CheckedChanged(object sender, EventArgs e)\r
1125         {\r
1126             if (Check_ChapterMarkers.Checked)\r
1127             {\r
1128                 string destination = text_destination.Text;\r
1129                 destination = destination.Replace(".mp4", ".m4v");\r
1130                 text_destination.Text = destination;\r
1131             }\r
1132             else\r
1133             {\r
1134                 string destination = text_destination.Text;\r
1135                 destination = destination.Replace(".m4v", ".mp4");\r
1136                 text_destination.Text = destination;\r
1137             }\r
1138         }\r
1139 \r
1140         private void drp_videoEncoder_SelectedIndexChanged(object sender, EventArgs e)\r
1141         {\r
1142             //Turn off some options which are H.264 only when the user selects a non h.264 encoder\r
1143             if (!drp_videoEncoder.Text.Contains("H.264"))\r
1144             {\r
1145                 check_turbo.CheckState = CheckState.Unchecked;\r
1146                 CheckCRF.CheckState = CheckState.Unchecked;\r
1147                 CheckCRF.Enabled = false;\r
1148                 check_turbo.Enabled = false;\r
1149                 h264Tab.Enabled = false;\r
1150                 rtf_h264advanced.Text = "";\r
1151             }\r
1152             else\r
1153             {\r
1154                 CheckCRF.Enabled = true;\r
1155                 if (check_2PassEncode.CheckState.ToString() == "Checked")\r
1156                 {\r
1157                     check_turbo.Enabled = true;\r
1158                 }\r
1159                 h264Tab.Enabled = true;\r
1160             }\r
1161 \r
1162         }\r
1163 \r
1164         public void setStreamReader(Parsing.DVD dvd)\r
1165         {\r
1166             this.thisDVD = dvd;\r
1167         }\r
1168 \r
1169         private void drp_dvdtitle_SelectedIndexChanged(object sender, EventArgs e)\r
1170         {\r
1171             // Reset some values on the form\r
1172             lbl_Aspect.Text = "Select a Title";\r
1173             lbl_RecomendedCrop.Text = "Select a Title";\r
1174             drop_chapterStart.Items.Clear();\r
1175             drop_chapterFinish.Items.Clear();\r
1176             QueryEditorText.Text = "";\r
1177 \r
1178             // If the dropdown is set to automatic nothing else needs to be done.\r
1179             // Otheriwse if its not, title data has to be loased from parsing.\r
1180             if (drp_dvdtitle.Text != "Automatic")\r
1181             {\r
1182                 Parsing.Title selectedTitle = drp_dvdtitle.SelectedItem as Parsing.Title;\r
1183 \r
1184                 // Set the Aspect Ratio\r
1185                 lbl_Aspect.Text = selectedTitle.AspectRatio.ToString();\r
1186 \r
1187                 // Set the Recommended Cropping values\r
1188                 lbl_RecomendedCrop.Text = string.Format("{0}/{1}/{2}/{3}", selectedTitle.AutoCropDimensions[0], selectedTitle.AutoCropDimensions[1], selectedTitle.AutoCropDimensions[2], selectedTitle.AutoCropDimensions[3]);\r
1189 \r
1190                 // Populate the Start chapter Dropdown\r
1191                 drop_chapterStart.Items.Clear();\r
1192                 drop_chapterStart.Items.AddRange(selectedTitle.Chapters.ToArray());\r
1193                 if (drop_chapterStart.Items.Count > 0)\r
1194                 {\r
1195                     drop_chapterStart.Text = drop_chapterStart.Items[0].ToString();\r
1196                 }\r
1197 \r
1198                 // Populate the Final Chapter Dropdown\r
1199                 drop_chapterFinish.Items.Clear();\r
1200                 drop_chapterFinish.Items.AddRange(selectedTitle.Chapters.ToArray());\r
1201                 if (drop_chapterFinish.Items.Count > 0)\r
1202                 {\r
1203                     drop_chapterFinish.Text = drop_chapterFinish.Items[drop_chapterFinish.Items.Count - 1].ToString();\r
1204                 }\r
1205 \r
1206                 // Populate the Audio Channels Dropdown\r
1207                 drp_audioChannels.Items.Clear();\r
1208                 drp_audioChannels.Items.Add("Automatic");\r
1209                 drp_audioChannels.Items.AddRange(selectedTitle.AudioTracks.ToArray());\r
1210                 if (drp_audioChannels.Items.Count > 0)\r
1211                 {\r
1212                     drp_audioChannels.Text = drp_audioChannels.Items[0].ToString();\r
1213                 }\r
1214 \r
1215                 // Populate the Subtitles dropdown\r
1216                 drp_subtitle.Items.Clear();\r
1217                 drp_subtitle.Items.Add("None");\r
1218                 drp_subtitle.Items.AddRange(selectedTitle.Subtitles.ToArray());\r
1219                 if (drp_subtitle.Items.Count > 0)\r
1220                 {\r
1221                     drp_subtitle.Text = drp_subtitle.Items[0].ToString();\r
1222                 }\r
1223             }\r
1224         }\r
1225 \r
1226         #endregion\r
1227 \r
1228         //---------------------------------------------------\r
1229         //  Some Functions\r
1230         //  - Query Generation\r
1231         //---------------------------------------------------\r
1232 \r
1233         #region Program Functions\r
1234 \r
1235         // Generate a CLI Query String\r
1236         public string GenerateTheQuery()\r
1237         {\r
1238             string source = text_source.Text;\r
1239             string dvdTitle = drp_dvdtitle.Text;\r
1240             string chapterStart = drop_chapterStart.Text;\r
1241             string chapterFinish = drop_chapterFinish.Text;\r
1242             int totalChapters = drop_chapterFinish.Items.Count - 1;\r
1243             string dvdChapter = "";\r
1244 \r
1245             source = " -i " + '"' + source + '"';\r
1246 \r
1247             if (dvdTitle == "Automatic")\r
1248                 dvdTitle = "";\r
1249             else\r
1250             {\r
1251                 string[] titleInfo = dvdTitle.Split(' ');\r
1252                 dvdTitle = " -t " + titleInfo[0];\r
1253             }\r
1254 \r
1255             if (chapterFinish.Equals("Auto") && chapterStart.Equals("Auto"))\r
1256                 dvdChapter = "";\r
1257             else if (chapterFinish == chapterStart)\r
1258                 dvdChapter = " -c " + chapterStart;\r
1259             else\r
1260                 dvdChapter = " -c " + chapterStart + "-" + chapterFinish;\r
1261 \r
1262             string querySource = source + dvdTitle + dvdChapter;\r
1263             // ----------------------------------------------------------------------\r
1264 \r
1265             // Destination\r
1266 \r
1267             string destination = text_destination.Text;\r
1268             string videoEncoder = drp_videoEncoder.Text;\r
1269             string audioEncoder = drp_audioCodec.Text;\r
1270             string width = text_width.Text;\r
1271             string height = text_height.Text;\r
1272 \r
1273             if (destination == "")\r
1274                 MessageBox.Show("No destination has been selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
1275             else\r
1276                 destination = " -o " + '"' + destination + '"'; //'"'+ \r
1277 \r
1278 \r
1279             switch (videoEncoder)\r
1280             {\r
1281                 case "Mpeg 4":\r
1282                     videoEncoder = " -e ffmpeg";\r
1283                     break;\r
1284                 case "Xvid":\r
1285                     videoEncoder = " -e xvid";\r
1286                     break;\r
1287                 case "H.264":\r
1288                     videoEncoder = " -e x264";\r
1289                     break;\r
1290                 case "H.264 Baseline 1.3":\r
1291                     videoEncoder = " -e x264b13";\r
1292                     break;\r
1293                 case "H.264 (iPod)":\r
1294                     videoEncoder = " -e x264b30";\r
1295                     break;\r
1296                 default:\r
1297                     videoEncoder = " -e x264";\r
1298                     break;\r
1299             }\r
1300 \r
1301             switch (audioEncoder)\r
1302             {\r
1303                 case "AAC":\r
1304                     audioEncoder = " -E faac";\r
1305                     break;\r
1306                 case "MP3":\r
1307                     audioEncoder = " -E lame";\r
1308                     break;\r
1309                 case "Vorbis":\r
1310                     audioEncoder = " -E vorbis";\r
1311                     break;\r
1312                 case "AC3":\r
1313                     audioEncoder = " -E ac3";\r
1314                     break;\r
1315                 default:\r
1316                     audioEncoder = " -E faac";\r
1317                     break;\r
1318             }\r
1319 \r
1320             if (width != "")\r
1321                 width = " -w " + width;\r
1322 \r
1323 \r
1324             if (height == "Auto")\r
1325             {\r
1326                 height = "";\r
1327             }\r
1328             else if (height != "")\r
1329             {\r
1330                 height = " -l " + height;\r
1331             }\r
1332 \r
1333 \r
1334             string queryDestination = destination + videoEncoder + audioEncoder + width + height;\r
1335             // ----------------------------------------------------------------------\r
1336 \r
1337             // Picture Settings Tab\r
1338 \r
1339             string cropSetting = drp_crop.Text;\r
1340             string cropTop = text_top.Text;\r
1341             string cropBottom = text_bottom.Text;\r
1342             string cropLeft = text_left.Text;\r
1343             string cropRight = text_right.Text;\r
1344             string subtitles = drp_subtitle.Text;\r
1345             string cropOut = "";\r
1346             string deInterlace_Option = drp_deInterlace_option.Text;\r
1347             string deinterlace = "";\r
1348             string grayscale = "";\r
1349             string pixelRatio = "";\r
1350             string ChapterMarkers = "";\r
1351             // Returns Crop Query\r
1352 \r
1353             if (cropSetting == "Auto Crop")\r
1354                 cropOut = "";\r
1355             else if (cropSetting == "No Crop")\r
1356                 cropOut = " --crop 0:0:0:0 ";\r
1357             else\r
1358             {\r
1359                 if (text_top.Text == "")\r
1360                     cropTop = "0";\r
1361                 if (text_bottom.Text == "")\r
1362                     cropBottom = "0";\r
1363                 if (text_left.Text == "")\r
1364                     cropLeft = "0";\r
1365                 if (text_right.Text == "")\r
1366                     cropRight = "0";\r
1367 \r
1368                 cropOut = " --crop " + cropTop + ":" + cropBottom + ":" + cropLeft + ":" + cropRight;\r
1369             }\r
1370 \r
1371             if (subtitles == "None")\r
1372                 subtitles = "";\r
1373             else if (subtitles == "")\r
1374                 subtitles = "";\r
1375             else\r
1376             {\r
1377                 string[] tempSub;\r
1378                 tempSub = subtitles.Split(' ');\r
1379                 subtitles = " -s " + tempSub[0];\r
1380             }\r
1381 \r
1382             switch (deInterlace_Option)\r
1383             {\r
1384                 case "None":\r
1385                     deinterlace = "";\r
1386                     break;\r
1387                 case "Original (Fast)":\r
1388                     deinterlace = " --deinterlace=fast";\r
1389                     break;\r
1390                 case "yadif (Slow)":\r
1391                     deinterlace = " --deinterlace=slow";\r
1392                     break;\r
1393                 case "yadif + mcdeint (Slower)":\r
1394                     deinterlace = " --deinterlace=slower";\r
1395                     break;\r
1396                 case "yadif + mcdeint (Slowest)":\r
1397                     deinterlace = " --deinterlace=slowest";\r
1398                     break;\r
1399                 default:\r
1400                     deinterlace = "";\r
1401                     break;\r
1402             }\r
1403 \r
1404             if (check_grayscale.Checked)\r
1405                 grayscale = " -g ";\r
1406 \r
1407             if (CheckPixelRatio.Checked)\r
1408                 pixelRatio = " -p ";\r
1409 \r
1410             if (Check_ChapterMarkers.Checked)\r
1411                 ChapterMarkers = " -m ";\r
1412 \r
1413             string queryPictureSettings = cropOut + subtitles + deinterlace + grayscale + pixelRatio + ChapterMarkers;\r
1414             // ----------------------------------------------------------------------\r
1415 \r
1416             // Video Settings Tab\r
1417 \r
1418             string videoBitrate = text_bitrate.Text;\r
1419             string videoFilesize = text_filesize.Text;\r
1420             double videoQuality = slider_videoQuality.Value;\r
1421             string vidQSetting = "";\r
1422             string twoPassEncoding = "";\r
1423             string videoFramerate = drp_videoFramerate.Text;\r
1424             string turboH264 = "";\r
1425             string largeFile = "";\r
1426             string deblock = "";\r
1427             string detelecine = "";\r
1428             string denoise = "";\r
1429             string CRF = CheckCRF.CheckState.ToString();\r
1430 \r
1431             if (CRF == "Checked")\r
1432                 CRF = " -Q ";\r
1433             else\r
1434                 CRF = "";\r
1435 \r
1436             if (videoBitrate != "")\r
1437                 videoBitrate = " -b " + videoBitrate;\r
1438 \r
1439             if (videoFilesize != "")\r
1440                 videoFilesize = " -S " + videoFilesize;\r
1441 \r
1442             // Video Quality Setting\r
1443 \r
1444             if ((videoQuality == 0))\r
1445                 vidQSetting = "";\r
1446             else\r
1447             {\r
1448                 videoQuality = videoQuality / 100;\r
1449                 if (videoQuality == 1)\r
1450                 {\r
1451                     vidQSetting = "1.0";\r
1452                 }\r
1453                 vidQSetting = " -q " + videoQuality.ToString(new CultureInfo("en-US")); \r
1454             }\r
1455 \r
1456             if (check_2PassEncode.Checked)\r
1457                 twoPassEncoding = " -2 ";\r
1458 \r
1459             if (videoFramerate == "Automatic")\r
1460                 videoFramerate = "";\r
1461             else\r
1462                 videoFramerate = " -r " + videoFramerate;\r
1463 \r
1464             if (check_turbo.Checked)\r
1465                 turboH264 = " -T ";\r
1466 \r
1467             if (check_largeFile.Checked)\r
1468                 largeFile = " -4 ";\r
1469 \r
1470             if (check_deblock.Checked)\r
1471                 deblock = " --deblock";\r
1472 \r
1473             if (check_detelecine.Checked)\r
1474                 detelecine = " --detelecine";\r
1475 \r
1476             switch (drp_deNoise.Text)\r
1477             {\r
1478                 case "None":\r
1479                     denoise = "";\r
1480                     break;\r
1481                 case "Weak":\r
1482                     denoise = " --denoise=weak";\r
1483                     break;\r
1484                 case "Medium":\r
1485                     denoise = " --denoise=medium";\r
1486                     break;\r
1487                 case "Strong":\r
1488                     denoise = " --denoise=strong";\r
1489                     break;\r
1490                 default:\r
1491                     denoise = "";\r
1492                     break;\r
1493             }\r
1494 \r
1495             string queryVideoSettings = videoBitrate + videoFilesize + vidQSetting + CRF + twoPassEncoding + videoFramerate + turboH264 + largeFile + deblock + detelecine + denoise;\r
1496             // ----------------------------------------------------------------------\r
1497 \r
1498             // Audio Settings Tab\r
1499 \r
1500             string audioBitrate = drp_audioBitrate.Text;\r
1501             string audioSampleRate = drp_audioSampleRate.Text;\r
1502             string audioChannels = drp_audioChannels.Text;\r
1503             string Mixdown = drp_audioMixDown.Text;\r
1504             string SixChannelAudio = "";\r
1505 \r
1506             if (audioBitrate != "")\r
1507                 audioBitrate = " -B " + audioBitrate;\r
1508 \r
1509             if (audioSampleRate != "")\r
1510                 audioSampleRate = " -R " + audioSampleRate;\r
1511 \r
1512             if (audioChannels == "Automatic")\r
1513                 audioChannels = "";\r
1514             else if (audioChannels == "")\r
1515                 audioChannels = "";\r
1516             else\r
1517             {\r
1518                 string[] tempSub;\r
1519                 tempSub = audioChannels.Split(' ');\r
1520                 audioChannels = " -a " + tempSub[0];\r
1521             }\r
1522 \r
1523             switch (Mixdown)\r
1524             {\r
1525                 case "Automatic":\r
1526                     Mixdown = "";\r
1527                     break;\r
1528                 case "Mono":\r
1529                     Mixdown = "mono";\r
1530                     break;\r
1531                 case "Stereo":\r
1532                     Mixdown = "stereo";\r
1533                     break;\r
1534                 case "Dolby Surround":\r
1535                     Mixdown = "dpl1";\r
1536                     break;\r
1537                 case "Dolby Pro Logic II":\r
1538                     Mixdown = "dpl2";\r
1539                     break;\r
1540                 case "6 Channel Discrete":\r
1541                     Mixdown = "6ch";\r
1542                     break;\r
1543                 default:\r
1544                     Mixdown = "";\r
1545                     break;\r
1546             }\r
1547 \r
1548             if (Mixdown != "")\r
1549                 SixChannelAudio = " -6 " + Mixdown;\r
1550             else\r
1551                 SixChannelAudio = "";\r
1552 \r
1553             string queryAudioSettings = audioBitrate + audioSampleRate + audioChannels + SixChannelAudio;\r
1554             // ----------------------------------------------------------------------\r
1555 \r
1556             //  H.264 Tab\r
1557 \r
1558 \r
1559             string h264Advanced = rtf_h264advanced.Text;\r
1560 \r
1561             if ((h264Advanced == ""))\r
1562                 h264Advanced = "";\r
1563             else\r
1564                 h264Advanced = " -x " + h264Advanced;\r
1565 \r
1566 \r
1567             string h264Settings = h264Advanced;\r
1568             // ----------------------------------------------------------------------\r
1569 \r
1570             // Processors (Program Settings)\r
1571 \r
1572             string processors = Properties.Settings.Default.Processors;\r
1573             //  Number of Processors Handler\r
1574 \r
1575             if (processors == "Automatic")\r
1576                 processors = "";\r
1577             else\r
1578                 processors = " -C " + processors + " ";\r
1579 \r
1580 \r
1581             string queryAdvancedSettings = processors;\r
1582             // ----------------------------------------------------------------------\r
1583 \r
1584             //  Verbose option (Program Settings)\r
1585 \r
1586             string verbose = "";\r
1587             if (Properties.Settings.Default.verbose == "Checked")\r
1588                 verbose = " -v ";\r
1589 \r
1590             // ----------------------------------------------------------------------\r
1591 \r
1592             return querySource + queryDestination + queryPictureSettings + queryVideoSettings + h264Settings + queryAudioSettings + queryAdvancedSettings + verbose;\r
1593         }\r
1594 \r
1595         // Load a Preset\r
1596         private void presetLoader(Functions.QueryParser presetQuery, string name)\r
1597         {\r
1598             // ---------------------------\r
1599             // Setup the GUI\r
1600             // ---------------------------\r
1601 \r
1602             // Source tab\r
1603             #region source\r
1604             if (presetQuery.Source != "")\r
1605                 text_source.Text = presetQuery.Source;\r
1606 \r
1607             if (presetQuery.DVDTitle != 0)\r
1608                 drp_dvdtitle.Text = presetQuery.DVDTitle.ToString();\r
1609 \r
1610             if (presetQuery.DVDChapterStart != 0)\r
1611                 drop_chapterStart.Text = presetQuery.DVDChapterStart.ToString();\r
1612 \r
1613             if (presetQuery.DVDChapterFinish != 0)\r
1614                 drop_chapterFinish.Text = presetQuery.DVDChapterFinish.ToString();\r
1615 \r
1616             if (presetQuery.Format != "")\r
1617             {\r
1618                 string destination = text_destination.Text;\r
1619                 destination = destination.Replace(".mp4", "." + presetQuery.Format);\r
1620                 destination = destination.Replace(".m4v", "." + presetQuery.Format);\r
1621                 destination = destination.Replace(".avi", "." + presetQuery.Format);\r
1622                 destination = destination.Replace(".mkv", "." + presetQuery.Format);\r
1623                 destination = destination.Replace(".ogm", "." + presetQuery.Format);\r
1624                 text_destination.Text = destination;\r
1625             }\r
1626 \r
1627             #endregion\r
1628 \r
1629             // Destination tab\r
1630             #region destination\r
1631 \r
1632             if (presetQuery.Destination != "")\r
1633                 text_destination.Text = presetQuery.Destination;\r
1634 \r
1635             drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
1636             drp_audioCodec.Text = presetQuery.AudioEncoder;\r
1637             if (presetQuery.Width != 0)\r
1638             {\r
1639                 text_width.Text = presetQuery.Width.ToString();\r
1640            \r
1641             }\r
1642             else\r
1643             {\r
1644                 text_width.Text = "";\r
1645                 text_width.BackColor = Color.White;\r
1646             }\r
1647 \r
1648             if (presetQuery.Height != 0)\r
1649             {\r
1650                 text_height.Text = presetQuery.Height.ToString();\r
1651             }\r
1652             else\r
1653             {\r
1654                 text_height.Text = "";\r
1655                 text_height.BackColor = Color.White;\r
1656             }\r
1657             #endregion\r
1658 \r
1659             // Picture Settings Tab\r
1660             #region Picture\r
1661             drp_crop.Text = "Manual";\r
1662             text_top.Text = presetQuery.CropTop;\r
1663             text_bottom.Text = presetQuery.CropBottom;\r
1664             text_left.Text = presetQuery.CropLeft;\r
1665             text_right.Text = presetQuery.CropRight;\r
1666 \r
1667             drp_deInterlace_option.Text = presetQuery.DeInterlace;\r
1668             drp_deNoise.Text = presetQuery.DeNoise;\r
1669 \r
1670             if (presetQuery.DeTelecine == true)\r
1671             {\r
1672                 check_detelecine.CheckState = CheckState.Checked;\r
1673             }\r
1674             else\r
1675             {\r
1676                 check_detelecine.CheckState = CheckState.Unchecked;\r
1677             }\r
1678 \r
1679 \r
1680             if (presetQuery.DeBlock == true)\r
1681             {\r
1682                 check_deblock.CheckState = CheckState.Checked;\r
1683             }\r
1684             else\r
1685             {\r
1686                 check_deblock.CheckState = CheckState.Unchecked;\r
1687             }\r
1688 \r
1689             if (presetQuery.ChapterMarkers == true)\r
1690             {\r
1691                 Check_ChapterMarkers.CheckState = CheckState.Checked;\r
1692             }\r
1693             else\r
1694             {\r
1695                 Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
1696             }\r
1697 \r
1698             if (presetQuery.Anamorphic == true)\r
1699             {\r
1700                 CheckPixelRatio.CheckState = CheckState.Checked;\r
1701             }\r
1702             else\r
1703             {\r
1704                 CheckPixelRatio.CheckState = CheckState.Unchecked;\r
1705             }\r
1706             #endregion\r
1707 \r
1708             // Video Settings Tab\r
1709             #region video\r
1710             text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
1711             text_filesize.Text = presetQuery.VideoTargetSize;\r
1712             slider_videoQuality.Value = presetQuery.VideoQuality;\r
1713             if (slider_videoQuality.Value != 0)\r
1714             {\r
1715                 CheckCRF.Enabled = true;\r
1716                 int ql = presetQuery.VideoQuality;\r
1717                 SliderValue.Text = ql.ToString() + "%";\r
1718             }\r
1719 \r
1720             if (presetQuery.TwoPass == true)\r
1721             {\r
1722                 check_2PassEncode.CheckState = CheckState.Checked;\r
1723             }\r
1724             else\r
1725             {\r
1726                 check_2PassEncode.CheckState = CheckState.Unchecked;\r
1727             }\r
1728 \r
1729             if (presetQuery.Grayscale == true)\r
1730             {\r
1731                 check_grayscale.CheckState = CheckState.Checked;\r
1732             }\r
1733             else\r
1734             {\r
1735                 check_grayscale.CheckState = CheckState.Unchecked;\r
1736             }\r
1737 \r
1738             drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
1739 \r
1740             if (presetQuery.TurboFirstPass == true)\r
1741             {\r
1742                 check_turbo.CheckState = CheckState.Checked;\r
1743             }\r
1744             else\r
1745             {\r
1746                 check_turbo.CheckState = CheckState.Unchecked;\r
1747             }\r
1748 \r
1749             if (presetQuery.LargeMP4 == true)\r
1750             {\r
1751                 check_largeFile.CheckState = CheckState.Checked;\r
1752             }\r
1753             else\r
1754             {\r
1755                 check_largeFile.CheckState = CheckState.Unchecked;\r
1756             }\r
1757             if (presetQuery.CRF == true)\r
1758             {\r
1759                 CheckCRF.CheckState = CheckState.Checked;\r
1760             }\r
1761             else\r
1762             {\r
1763                 CheckCRF.CheckState = CheckState.Unchecked;\r
1764             }\r
1765             #endregion\r
1766 \r
1767             // Audio Settings Tab\r
1768             #region audio\r
1769             drp_audioBitrate.Text = presetQuery.AudioBitrate;\r
1770             drp_audioSampleRate.Text = presetQuery.AudioSampleBitrate;\r
1771             drp_audioChannels.Text = presetQuery.AudioTrack1;\r
1772             drp_audioMixDown.Text = presetQuery.AudioTrackMix;\r
1773             drp_subtitle.Text = presetQuery.Subtitles;\r
1774             #endregion\r
1775 \r
1776             // H264 Tab & Preset Name\r
1777             #region other\r
1778             rtf_h264advanced.Text = presetQuery.H264Query;\r
1779 \r
1780             // Set the preset name\r
1781             groupBox_output.Text = "Output Settings (Preset: " + name + ")";\r
1782             #endregion\r
1783         }\r
1784 \r
1785         #endregion\r
1786         // This is the END of the road ------------------------------------------------------------------------------\r
1787     }\r
1788 }