/// <summary> /// Start an Encode /// </summary> public void StartEncode() { // Santiy Checking. if (this.ScannedSource == null || this.CurrentTask == null) { throw new GeneralApplicationException("You must first scan a source.", string.Empty, null); } if (string.IsNullOrEmpty(this.CurrentTask.Destination)) { throw new GeneralApplicationException("The Destination field was empty.", "You must first set a destination for the encoded file.", null); } if (this.queueProcessor.IsProcessing) { throw new GeneralApplicationException("HandBrake is already encoding.", string.Empty, null); } if (File.Exists(this.CurrentTask.Destination)) { // TODO: File Overwrite warning. } // Create the Queue Task and Start Processing QueueTask task = new QueueTask(null) { Destination = this.CurrentTask.Destination, Task = this.CurrentTask, Query = QueryGeneratorUtility.GenerateQuery(this.CurrentTask), CustomQuery = false }; this.queueProcessor.QueueManager.Add(task); this.queueProcessor.Start(); }
/// <summary> /// Setup the logging. /// </summary> /// <param name="encodeQueueTask"> /// The encode QueueTask. /// </param> /// <param name="isLibhb"> /// Indicates if this is libhb that is encoding or not. /// </param> protected void SetupLogging(QueueTask encodeQueueTask, bool isLibhb) { this.ShutdownFileWriter(); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId)); string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.ProcessId)); try { string query = QueryGeneratorUtility.GenerateQuery(new EncodeTask(encodeQueueTask.Task), encodeQueueTask.Configuration); this.logBuffer = new StringBuilder(); if (!isLibhb) { this.logBuffer.AppendLine(String.Format("CLI Query: {0}", query)); } this.logBuffer.AppendLine(); // Clear the current Encode Logs) if (File.Exists(logFile)) { File.Delete(logFile); } if (File.Exists(logFile2)) { File.Delete(logFile2); } lock (FileWriterLock) { this.fileWriter = new StreamWriter(logFile) { AutoFlush = true }; this.fileWriter.WriteLine(this.header); if (!isLibhb) { this.fileWriter.WriteLine("CLI Query: {0}", query); } this.fileWriter.WriteLine(); } } catch (Exception) { if (this.fileWriter != null) { lock (FileWriterLock) { this.fileWriter.Flush(); this.fileWriter.Close(); this.fileWriter.Dispose(); } } throw; } }
/// <summary> /// Prepare the Preset window to create a Preset Object later. /// </summary> /// <param name="task"> /// The Encode Task. /// </param> public void Setup(EncodeTask task) { task.UsesPictureFilters = this.Preset.UsePictureFilters; task.UsesMaxPictureSettings = false; // TODO task.UsesPictureSettings = false; // TODO this.Preset.Task = task; this.Preset.Query = QueryGeneratorUtility.GenerateQuery(task); }
/// <summary> /// Setup the logging. /// </summary> /// <param name="encodeQueueTask"> /// The encode QueueTask. /// </param> protected void SetupLogging(QueueTask encodeQueueTask) { ShutdownFileWriter(); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId)); string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.ProcessId)); try { string query = QueryGeneratorUtility.GenerateQuery(new EncodeTask(encodeQueueTask.Task), userSettingService.GetUserSetting <int>(ASUserSettingConstants.PreviewScanCount), userSettingService.GetUserSetting <int>(ASUserSettingConstants.Verbosity), userSettingService.GetUserSetting <bool>(ASUserSettingConstants.DisableLibDvdNav)); this.logBuffer = new StringBuilder(); this.logBuffer.AppendLine(String.Format("CLI Query: {0}", query)); this.logBuffer.AppendLine(String.Format("User Query: {0}", encodeQueueTask.CustomQuery)); this.logBuffer.AppendLine(); // Clear the current Encode Logs) if (File.Exists(logFile)) { File.Delete(logFile); } if (File.Exists(logFile2)) { File.Delete(logFile2); } this.fileWriter = new StreamWriter(logFile) { AutoFlush = true }; this.fileWriter.WriteLine(header); this.fileWriter.WriteLine(string.Format("CLI Query: {0}", query)); this.fileWriter.WriteLine(string.Format("User Query: {0}", encodeQueueTask.CustomQuery)); this.fileWriter.WriteLine(); } catch (Exception) { if (this.fileWriter != null) { this.fileWriter.Close(); this.fileWriter.Dispose(); } throw; } }
/// <summary> /// Setup the logging. /// </summary> /// <param name="encodeQueueTask"> /// The encode QueueTask. /// </param> protected void SetupLogging(QueueTask encodeQueueTask) { ShutdownFileWriter(); string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs"; string logFile = Path.Combine(logDir, string.Format("last_encode_log{0}.txt", GeneralUtilities.GetInstanceCount)); string logFile2 = Path.Combine(logDir, string.Format("tmp_appReadable_log{0}.txt", GeneralUtilities.GetInstanceCount)); try { string query = QueryGeneratorUtility.GenerateQuery(new EncodeTask(encodeQueueTask.Task)); this.logBuffer = new StringBuilder(); this.logBuffer.AppendLine(String.Format("CLI Query: {0}", query)); this.logBuffer.AppendLine(String.Format("User Query: {0}", encodeQueueTask.CustomQuery)); this.logBuffer.AppendLine(); // Clear the current Encode Logs) if (File.Exists(logFile)) { File.Delete(logFile); } if (File.Exists(logFile2)) { File.Delete(logFile2); } this.fileWriter = new StreamWriter(logFile) { AutoFlush = true }; this.fileWriter.WriteLine(GeneralUtilities.CreateCliLogHeader()); this.fileWriter.WriteLine(String.Format("CLI Query: {0}", query)); this.fileWriter.WriteLine(String.Format("User Query: {0}", encodeQueueTask.CustomQuery)); this.fileWriter.WriteLine(); } catch (Exception) { if (this.fileWriter != null) { this.fileWriter.Close(); this.fileWriter.Dispose(); } throw; } }
/// <summary> /// Start an Encode /// </summary> public void StartEncode() { // Santiy Checking. if (this.ScannedSource == null || this.CurrentTask == null) { this.errorService.ShowMessageBox("You must first scan a source.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (string.IsNullOrEmpty(this.CurrentTask.Destination)) { this.errorService.ShowMessageBox("The Destination field was empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (this.queueProcessor.IsProcessing) { this.errorService.ShowMessageBox("HandBrake is already encoding.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (File.Exists(this.CurrentTask.Destination)) { MessageBoxResult result = this.errorService.ShowMessageBox("The current file already exists, do you wish to overwrite it?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.No) { return; } } // Create the Queue Task and Start Processing QueueTask task = new QueueTask(null) { Destination = this.CurrentTask.Destination, Task = this.CurrentTask, Query = QueryGeneratorUtility.GenerateQuery(this.CurrentTask), CustomQuery = false }; this.queueProcessor.QueueManager.Add(task); this.queueProcessor.Start(); this.IsEncoding = true; }
/// <summary> /// Add the current task to the queue. /// </summary> public void AddToQueue() { if (this.ScannedSource == null || string.IsNullOrEmpty(this.ScannedSource.ScanPath) || this.ScannedSource.Titles.Count == 0) { this.errorService.ShowMessageBox("You must first scan a source and setup your job before adding to the queue.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } QueueTask task = new QueueTask { Task = this.CurrentTask, Query = QueryGeneratorUtility.GenerateQuery(this.CurrentTask) }; this.queueProcessor.QueueManager.Add(task); if (!this.IsEncoding) { this.ProgramStatusLabel = string.Format("{0} Encodes Pending", this.queueProcessor.QueueManager.Count); } }
/// <summary> /// Writes the current state of the queue in the form of a batch (.bat) file. /// </summary> /// <param name="file"> /// The location of the file to write the batch file to. /// </param> /// <returns> /// The write batch script to file. /// </returns> public bool WriteBatchScriptToFile(string file) { string queries = string.Empty; foreach (QueueTask queueItem in this.queue) { string qItem = QueryGeneratorUtility.GenerateQuery(new EncodeTask(queueItem.Task)); string fullQuery = '"' + Application.StartupPath + "\\HandBrakeCLI.exe" + '"' + qItem; if (queries == string.Empty) { queries = queries + fullQuery; } else { queries = queries + " && " + fullQuery; } } string strCmdLine = queries; if (file != string.Empty) { try { // Create a StreamWriter and open the file, Write the batch file query to the file and // Close the stream using (StreamWriter line = new StreamWriter(file)) { line.WriteLine(strCmdLine); } return(true); } catch (Exception exc) { throw new Exception("Unable to write to the file. Please make sure that the location has the correct permissions for file writing.", exc); } } return(false); }
/// <summary> /// Execute a HandBrakeCLI process. /// This should only be called from the UI thread. /// </summary> /// <param name="encodeQueueTask"> /// The encodeQueueTask. /// </param> public void Start(QueueTask encodeQueueTask) { try { if (this.IsEncoding) { throw new GeneralApplicationException("HandBrake is already encodeing.", "Please try again in a minute", null); } this.IsEncoding = true; this.currentTask = encodeQueueTask; if (encodeQueueTask.Configuration.IsLoggingEnabled) { try { this.SetupLogging(currentTask); } catch (Exception) { this.IsEncoding = false; throw; } } // Make sure the path exists, attempt to create it if it doesn't this.VerifyEncodeDestinationPath(currentTask); string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); // TODO tidy this code up, it's kinda messy. string query = this.currentTask.Task.IsPreviewEncode ? QueryGeneratorUtility.GeneratePreviewQuery( new EncodeTask(this.currentTask.Task), encodeQueueTask.Configuration, this.currentTask.Task.PreviewEncodeDuration, this.currentTask.Task.PreviewEncodeStartAt) : QueryGeneratorUtility.GenerateQuery(new EncodeTask(this.currentTask.Task), encodeQueueTask.Configuration); ProcessStartInfo cliStart = new ProcessStartInfo(handbrakeCLIPath, query) { RedirectStandardOutput = true, RedirectStandardError = encodeQueueTask.Configuration.IsLoggingEnabled, UseShellExecute = false, CreateNoWindow = true }; this.HbProcess = new Process { StartInfo = cliStart }; this.HbProcess.Start(); this.startTime = DateTime.Now; if (encodeQueueTask.Configuration.IsLoggingEnabled) { this.HbProcess.ErrorDataReceived += this.HbProcErrorDataReceived; this.HbProcess.BeginErrorReadLine(); } this.HbProcess.OutputDataReceived += HbProcess_OutputDataReceived; this.HbProcess.BeginOutputReadLine(); this.processId = this.HbProcess.Id; // Set the process Priority if (this.processId != -1) { this.HbProcess.EnableRaisingEvents = true; this.HbProcess.Exited += this.HbProcessExited; } // Set the Process Priority switch (encodeQueueTask.Configuration.ProcessPriority) { case "Realtime": this.HbProcess.PriorityClass = ProcessPriorityClass.RealTime; break; case "High": this.HbProcess.PriorityClass = ProcessPriorityClass.High; break; case "Above Normal": this.HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal; break; case "Normal": this.HbProcess.PriorityClass = ProcessPriorityClass.Normal; break; case "Low": this.HbProcess.PriorityClass = ProcessPriorityClass.Idle; break; default: this.HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal; break; } // Fire the Encode Started Event this.InvokeEncodeStarted(EventArgs.Empty); } catch (Exception exc) { encodeQueueTask.Status = QueueItemStatus.Error; this.IsEncoding = false; this.InvokeEncodeCompleted( new EncodeCompletedEventArgs( false, exc, "An Error occured when trying to encode this source. ", this.currentTask.Task.Destination)); throw; } }