3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr/>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.Diagnostics;
\r
10 using System.Windows.Forms;
\r
11 using Handbrake.Functions;
\r
13 namespace Handbrake.EncodeQueue
\r
17 private int ProcessID { get; set; }
\r
22 public Process HbProcess { get; set; }
\r
25 /// The Process Handle
\r
27 public IntPtr ProcessHandle { get; set; }
\r
30 /// Returns true of HandBrakeCLI.exe is running
\r
32 public Boolean IsEncoding { get; set; }
\r
35 /// Fires when a new CLI Job starts
\r
37 public event EventHandler EncodeStarted;
\r
40 /// Fires when a CLI job finishes.
\r
42 public event EventHandler EncodeEnded;
\r
45 /// Create a preview sample video
\r
47 /// <param name="query"></param>
\r
48 public void CreatePreviewSample(string query)
\r
54 /// Execute a HandBrakeCLI process.
\r
56 /// <param name="query">The CLI Query</param>
\r
57 protected void Run(string query)
\r
61 if (EncodeStarted != null)
\r
62 EncodeStarted(this, new EventArgs());
\r
66 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
67 string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");
\r
68 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, query, logPath);
\r
69 ProcessStartInfo cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
71 if (Properties.Settings.Default.enocdeStatusInGui)
\r
73 cliStart.RedirectStandardOutput = true;
\r
74 cliStart.UseShellExecute = false;
\r
75 if (!Properties.Settings.Default.showCliForInGuiEncodeStatus)
\r
76 cliStart.CreateNoWindow = true;
\r
78 if (Properties.Settings.Default.cli_minimized)
\r
79 cliStart.WindowStyle = ProcessWindowStyle.Minimized;
\r
81 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.
\r
82 HbProcess = Process.Start(cliStart);
\r
83 ProcessID = Main.GetCliProcess(before);
\r
85 if (HbProcess != null)
\r
86 ProcessHandle = HbProcess.MainWindowHandle; // Set the process Handle
\r
88 // Set the process Priority
\r
89 Process hbCliProcess = null;
\r
90 if (ProcessID != -1)
\r
91 hbCliProcess = Process.GetProcessById(ProcessID);
\r
93 if (hbCliProcess != null)
\r
94 switch (Properties.Settings.Default.processPriority)
\r
97 hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;
\r
100 hbCliProcess.PriorityClass = ProcessPriorityClass.High;
\r
102 case "Above Normal":
\r
103 hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;
\r
106 hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;
\r
109 hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
112 hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;
\r
116 catch (Exception exc)
\r
118 MessageBox.Show("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\n Detailed Error Information: error occured in runCli()\n\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
123 /// Kill the CLI process
\r
127 if (HbProcess != null)
\r
130 Process[] list = Process.GetProcessesByName("HandBrakeCLI");
\r
131 foreach (Process process in list)
\r
134 IsEncoding = false;
\r
136 if (EncodeEnded != null)
\r
137 EncodeEnded(this, new EventArgs());
\r
141 /// Perform an action after an encode. e.g a shutdown, standby, restart etc.
\r
143 protected void Finish()
\r
145 IsEncoding = false;
\r
148 if (Properties.Settings.Default.growlQueue)
\r
149 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");
\r
151 // Do something whent he encode ends.
\r
152 switch (Properties.Settings.Default.CompletionOption)
\r
155 Process.Start("Shutdown", "-s -t 60");
\r
158 Win32.ExitWindowsEx(0, 0);
\r
161 Application.SetSuspendState(PowerState.Suspend, true, true);
\r
164 Application.SetSuspendState(PowerState.Hibernate, true, true);
\r
166 case "Lock System":
\r
167 Win32.LockWorkStation();
\r
169 case "Quit HandBrake":
\r
170 Application.Exit();
\r
178 /// Add the CLI Query to the Log File.
\r
180 /// <param name="encJob"></param>
\r
181 protected void AddCLIQueryToLog(Job encJob)
\r
185 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
186 "\\HandBrake\\logs";
\r
187 string logPath = Path.Combine(logDir, "last_encode_log.txt");
\r
189 StreamReader reader =
\r
190 new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));
\r
191 String log = reader.ReadToEnd();
\r
194 StreamWriter writer = new StreamWriter(File.Create(logPath));
\r
196 writer.Write("### CLI Query: " + encJob.Query + "\n\n");
\r
197 writer.Write("### User Query: " + encJob.CustomQuery + "\n\n");
\r
198 writer.Write("#########################################\n\n");
\r
199 writer.WriteLine(log);
\r
202 } catch (Exception)
\r
209 /// Save a copy of the log to the users desired location or a default location
\r
210 /// if this feature is enabled in options.
\r
212 /// <param name="destination"></param>
\r
213 protected void CopyLog(string destination)
\r
217 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
218 string tempLogFile = Path.Combine(logDir, "last_encode_log.txt");
\r
220 string encodeDestinationPath = Path.GetDirectoryName(destination);
\r
221 String destinationFile = Path.GetFileName(destination);
\r
222 string encodeLogFile = destinationFile + " " + DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + ".txt";
\r
224 // Make sure the log directory exists.
\r
225 if (!Directory.Exists(logDir))
\r
226 Directory.CreateDirectory(logDir);
\r
228 // Copy the Log to HandBrakes log folder in the users applciation data folder.
\r
229 File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
\r
231 // Save a copy of the log file in the same location as the enocde.
\r
232 if (Properties.Settings.Default.saveLogWithVideo)
\r
233 File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
\r
235 // Save a copy of the log file to a user specified location
\r
236 if (Directory.Exists(Properties.Settings.Default.saveLogPath))
\r
237 if (Properties.Settings.Default.saveLogPath != String.Empty && Properties.Settings.Default.saveLogToSpecifiedPath)
\r
238 File.Copy(tempLogFile, Path.Combine(Properties.Settings.Default.saveLogPath, encodeLogFile));
\r
240 catch (Exception exc)
\r
242 MessageBox.Show("Something went a bit wrong trying to copy your log file.\nError Information:\n\n" + exc, "Error",
\r
243 MessageBoxButtons.OK, MessageBoxIcon.Error);
\r