OSDN Git Service

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