2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr/>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace Handbrake.EncodeQueue
\r
9 using System.Diagnostics;
\r
11 using System.Windows.Forms;
\r
16 /// Class which handles the CLI
\r
21 /// Fires when a new CLI Job starts
\r
23 public event EventHandler EncodeStarted;
\r
26 /// Fires when a CLI job finishes.
\r
28 public event EventHandler EncodeEnded;
\r
31 /// Gets or sets The HB Process
\r
33 public Process HbProcess { get; set; }
\r
36 /// Gets or sets The Process Handle
\r
38 public IntPtr ProcessHandle { get; set; }
\r
41 /// Gets or sets a value indicating whether HandBrakeCLI.exe is running
\r
43 public bool IsEncoding { get; set; }
\r
46 /// Gets or sets the Process ID
\r
48 private int ProcessID { get; set; }
\r
51 /// Create a preview sample video
\r
53 /// <param name="query">
\r
56 public void CreatePreviewSample(string query)
\r
62 /// Kill the CLI process
\r
66 if (this.HbProcess != null)
\r
67 this.HbProcess.Kill();
\r
69 Process[] list = Process.GetProcessesByName("HandBrakeCLI");
\r
70 foreach (Process process in list)
\r
73 this.IsEncoding = false;
\r
75 if (this.EncodeEnded != null)
\r
76 this.EncodeEnded(this, new EventArgs());
\r
80 /// Attempt to Safely kill a DirectRun() CLI
\r
81 /// NOTE: This will not work with a MinGW CLI
\r
82 /// Note: http://www.cygwin.com/ml/cygwin/2006-03/msg00330.html
\r
84 public void SafelyClose()
\r
86 if ((int)this.ProcessHandle == 0)
\r
89 // Allow the CLI to exit cleanly
\r
90 Win32.SetForegroundWindow((int)this.ProcessHandle);
\r
91 SendKeys.Send("^C");
\r
93 // HbProcess.StandardInput.AutoFlush = true;
\r
94 // HbProcess.StandardInput.WriteLine("^C");
\r
98 /// Execute a HandBrakeCLI process.
\r
100 /// <param name="query">
\r
103 protected void Run(string query)
\r
107 if (this.EncodeStarted != null)
\r
108 this.EncodeStarted(this, new EventArgs());
\r
110 this.IsEncoding = true;
\r
112 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
115 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs",
\r
116 "last_encode_log.txt");
\r
117 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath);
\r
118 var cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
120 if (Settings.Default.enocdeStatusInGui)
\r
122 cliStart.RedirectStandardOutput = true;
\r
123 cliStart.UseShellExecute = false;
\r
124 if (!Settings.Default.showCliForInGuiEncodeStatus)
\r
125 cliStart.CreateNoWindow = true;
\r
127 if (Settings.Default.cli_minimized)
\r
128 cliStart.WindowStyle = ProcessWindowStyle.Minimized;
\r
130 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.
\r
131 this.HbProcess = Process.Start(cliStart);
\r
132 this.ProcessID = Main.GetCliProcess(before);
\r
134 if (this.HbProcess != null)
\r
135 this.ProcessHandle = this.HbProcess.MainWindowHandle; // Set the process Handle
\r
137 // Set the process Priority
\r
138 Process hbCliProcess = null;
\r
139 if (this.ProcessID != -1)
\r
140 hbCliProcess = Process.GetProcessById(this.ProcessID);
\r
142 if (hbCliProcess != null)
\r
143 switch (Settings.Default.processPriority)
\r
146 hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
149 hbCliProcess.PriorityClass = ProcessPriorityClass.High;
\r
151 case "Above Normal":
\r
152 hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
155 hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
158 hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
161 hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
165 catch (Exception exc)
\r
168 "It would appear that HandBrakeCLI has not started correctly. You should take a look at the Activity log as it may indicate the reason why.\n\nDetailed Error Information: error occured in runCli()\n\n" +
\r
171 MessageBoxButtons.OK,
\r
172 MessageBoxIcon.Error);
\r
177 /// Function to run the CLI directly rather than via CMD
\r
178 /// TODO: Code to handle the Log data has yet to be written.
\r
179 /// TODO: Code to handle the % / ETA info has to be written.
\r
181 /// <param name="query">
\r
184 protected void DirectRun(string query)
\r
188 if (this.EncodeStarted != null)
\r
189 this.EncodeStarted(this, new EventArgs());
\r
191 this.IsEncoding = true;
\r
194 string handbrakeCLIPath = Path.Combine(Environment.CurrentDirectory, "HandBrakeCLI.exe");
\r
195 var hbProc = new Process
\r
199 FileName = handbrakeCLIPath,
\r
200 Arguments = query,
\r
201 UseShellExecute = false,
\r
202 RedirectStandardOutput = true,
\r
203 RedirectStandardError = true,
\r
204 RedirectStandardInput = true,
\r
205 CreateNoWindow = false,
\r
206 WindowStyle = ProcessWindowStyle.Minimized
\r
210 // Setup the redirects
\r
211 hbProc.ErrorDataReceived += new DataReceivedEventHandler(HbProcErrorDataReceived);
\r
212 hbProc.OutputDataReceived += new DataReceivedEventHandler(HbProcOutputDataReceived);
\r
214 // Start the process
\r
217 // Set the Process Priority
\r
218 switch (Settings.Default.processPriority)
\r
221 hbProc.PriorityClass = ProcessPriorityClass.RealTime;
\r
224 hbProc.PriorityClass = ProcessPriorityClass.High;
\r
226 case "Above Normal":
\r
227 hbProc.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
230 hbProc.PriorityClass = ProcessPriorityClass.Normal;
\r
233 hbProc.PriorityClass = ProcessPriorityClass.Idle;
\r
236 hbProc.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
240 // Set the class items
\r
241 this.HbProcess = hbProc;
\r
242 this.ProcessID = hbProc.Id;
\r
243 this.ProcessHandle = hbProc.Handle;
\r
245 catch (Exception exc)
\r
247 Console.WriteLine(exc);
\r
252 /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
\r
254 protected void Finish()
\r
256 if (this.EncodeEnded != null)
\r
257 this.EncodeEnded(this, new EventArgs());
\r
259 this.IsEncoding = false;
\r
262 if (Settings.Default.growlQueue)
\r
263 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");
\r
265 // Do something whent he encode ends.
\r
266 switch (Settings.Default.CompletionOption)
\r
269 Process.Start("Shutdown", "-s -t 60");
\r
272 Win32.ExitWindowsEx(0, 0);
\r
275 Application.SetSuspendState(PowerState.Suspend, true, true);
\r
278 Application.SetSuspendState(PowerState.Hibernate, true, true);
\r
280 case "Lock System":
\r
281 Win32.LockWorkStation();
\r
283 case "Quit HandBrake":
\r
284 Application.Exit();
\r
292 /// Add the CLI Query to the Log File.
\r
294 /// <param name="encJob">
\r
295 /// The Encode Job Object
\r
297 protected void AddCLIQueryToLog(Job encJob)
\r
301 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
302 "\\HandBrake\\logs";
\r
303 string logPath = Path.Combine(logDir, "last_encode_log.txt");
\r
306 new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
\r
307 string log = reader.ReadToEnd();
\r
310 var writer = new StreamWriter(File.Create(logPath));
\r
312 writer.Write("### CLI Query: " + encJob.Query + "\n\n");
\r
313 writer.Write("### User Query: " + encJob.CustomQuery + "\n\n");
\r
314 writer.Write("#########################################\n\n");
\r
315 writer.WriteLine(log);
\r
326 /// Save a copy of the log to the users desired location or a default location
\r
327 /// if this feature is enabled in options.
\r
329 /// <param name="destination">
\r
330 /// The Destination File Path
\r
332 protected void CopyLog(string destination)
\r
336 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
337 "\\HandBrake\\logs";
\r
338 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");
\r
340 string encodeDestinationPath = Path.GetDirectoryName(destination);
\r
341 string destinationFile = Path.GetFileName(destination);
\r
342 string encodeLogFile = destinationFile + " " +
\r
343 DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";
\r
345 // Make sure the log directory exists.
\r
346 if (!Directory.Exists(logDir))
\r
347 Directory.CreateDirectory(logDir);
\r
349 // Copy the Log to HandBrakes log folder in the users applciation data folder.
\r
350 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
\r
352 // Save a copy of the log file in the same location as the enocde.
\r
353 if (Settings.Default.saveLogWithVideo)
\r
354 File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
\r
356 // Save a copy of the log file to a user specified location
\r
357 if (Directory.Exists(Settings.Default.saveLogPath))
\r
358 if (Settings.Default.saveLogPath != String.Empty && Settings.Default.saveLogToSpecifiedPath)
\r
359 File.Copy(tempLogFile, Path.Combine(Settings.Default.saveLogPath, encodeLogFile));
\r
361 catch (Exception exc)
\r
364 "Something went a bit wrong trying to copy your log file.\nError Information:\n\n" + exc,
\r
366 MessageBoxButtons.OK,
\r
367 MessageBoxIcon.Error);
\r
372 /// Recieve the Standard Error information and process it
\r
374 /// <param name="sender">
\r
375 /// The Sender Object
\r
377 /// <param name="e">
\r
378 /// DataReceived EventArgs
\r
380 private static void HbProcErrorDataReceived(object sender, DataReceivedEventArgs e)
\r
382 // TODO: Recieve the Log data and process it
\r
383 throw new NotImplementedException();
\r
387 /// Standard Input Data Recieved from the CLI
\r
389 /// <param name="sender">
\r
390 /// The Sender Object
\r
392 /// <param name="e">
\r
393 /// DataReceived EventArgs
\r
395 private static void HbProcOutputDataReceived(object sender, DataReceivedEventArgs e)
\r
397 // TODO: Recieve the %, ETA, FPS etc and process it
\r
398 throw new NotImplementedException();
\r