/// <summary> /// Scan a Source Path. /// Title 0: scan all /// </summary> /// <param name="sourcePath"> /// Path to the file to scan /// </param> /// <param name="title"> /// int title number. 0 for scan all /// </param> /// <param name="postAction"> /// The post Action. /// </param> public void Scan(string sourcePath, int title, Action <bool, Source> postAction) { // Try to cleanup any previous scan instances. if (this.instance != null) { try { this.instance.Dispose(); } catch (Exception) { // Do Nothing } } this.isCancelled = false; // Reset the log this.logInstanceManager.ResetApplicationLog(); // Handle the post scan operation. this.postScanOperation = postAction; // Create a new HandBrake Instance. this.instance = HandBrakeInstanceManager.GetScanInstance(this.userSettingService.GetUserSetting <int>(UserSettingConstants.Verbosity)); this.instance.ScanProgress += this.InstanceScanProgress; this.instance.ScanCompleted += this.InstanceScanCompleted; // Start the scan on a back this.ScanSource(sourcePath, title, this.userSettingService.GetUserSetting <int>(UserSettingConstants.PreviewScanCount)); }
/// <summary> /// Kill the scan /// </summary> public void Stop() { try { this.ServiceLogMessage("Manually Stopping Scan ..."); this.IsScanning = false; var handBrakeInstance = this.instance; if (handBrakeInstance != null) { handBrakeInstance.StopScan(); handBrakeInstance.ScanProgress -= this.InstanceScanProgress; handBrakeInstance.ScanCompleted -= this.InstanceScanCompleted; handBrakeInstance.Dispose(); this.instance = null; } } catch (Exception exc) { this.ServiceLogMessage(exc.ToString()); } finally { this.ScanCompleted?.Invoke(this, new ScanCompletedEventArgs(this.isCancelled, null, null, null)); this.instance = null; this.ServiceLogMessage("Scan Stopped ..."); } }
/// <summary> /// Scan a Source Path. /// Title 0: scan all /// </summary> /// <param name="sourcePath"> /// Path to the file to scan /// </param> /// <param name="title"> /// int title number. 0 for scan all /// </param> /// <param name="postAction"> /// The post Action. /// </param> /// <param name="configuraiton"> /// The configuraiton. /// </param> public void Scan(string sourcePath, int title, Action <bool, Source> postAction, HBConfiguration configuraiton) { // Try to cleanup any previous scan instances. if (this.instance != null) { try { this.instance.Dispose(); } catch (Exception) { // Do Nothing } } this.isCancelled = false; // Handle the post scan operation. this.postScanOperation = postAction; // Create a new HandBrake Instance. this.instance = HandBrakeInstanceManager.GetScanInstance(configuraiton.Verbosity); this.instance.ScanProgress += this.InstanceScanProgress; this.instance.ScanCompleted += this.InstanceScanCompleted; // Start the scan on a back this.ScanSource(sourcePath, title, configuraiton.PreviewScanCount, configuraiton); }
/// <summary> /// The is nightly. /// </summary> /// <returns> /// The <see cref="string"/>. /// </returns> public static bool IsNightly() { IHandBrakeInstance instance = HandBrakeInstanceManager.GetScanInstance(1); // 01 = Unofficial Builds. 00 = Official Tagged Releases. return(instance.Build.ToString().EndsWith("01")); }
/// <summary> /// The get build. /// </summary> /// <returns> /// The <see cref="int"/>. /// </returns> public static string GetVersion() { Version version = Assembly.GetEntryAssembly().GetName().Version; IHandBrakeInstance instance = HandBrakeInstanceManager.GetScanInstance(1); return(IsNightly() ? string.Format("Nightly {0} ({1})", instance.Version, instance.Build) : string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision)); }
/// <summary> /// Scan a Source Path. /// Title 0: scan all /// </summary> /// <param name="sourcePath"> /// Path to the file to scan /// </param> /// <param name="title"> /// int title number. 0 for scan all /// </param> /// <param name="postAction"> /// The post Action. /// </param> /// <param name="configuraiton"> /// The configuraiton. /// </param> public void Scan(string sourcePath, int title, Action <bool, Source> postAction, HBConfiguration configuraiton) { // Try to cleanup any previous scan instances. if (this.instance != null) { try { lock (LogLock) { this.scanLog.Close(); this.scanLog.Dispose(); this.scanLog = null; } this.instance.Dispose(); } catch (Exception) { // Do Nothing } } // Handle the post scan operation. this.postScanOperation = postAction; // Clear down the logging this.logging.Clear(); try { // Make we don't pick up a stale last_scan_log_xyz.txt (and that we have rights to the file) if (File.Exists(this.dvdInfoPath)) { File.Delete(this.dvdInfoPath); } } catch (Exception exc) { Debug.WriteLine(exc); } if (!Directory.Exists(Path.GetDirectoryName(this.dvdInfoPath))) { Directory.CreateDirectory(Path.GetDirectoryName(this.dvdInfoPath)); } // Create a new scan log. this.scanLog = new StreamWriter(this.dvdInfoPath); // Create a new HandBrake Instance. HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; this.instance = HandBrakeInstanceManager.GetScanInstance(configuraiton.Verbosity); this.instance.ScanProgress += this.InstanceScanProgress; this.instance.ScanCompleted += this.InstanceScanCompleted; // Start the scan on a back this.ScanSource(sourcePath, title, configuraiton.PreviewScanCount, configuraiton); }
/// <summary> /// Start with a LibHb EncodeJob Object /// </summary> /// <param name="job"> /// The job. /// </param> public void Start(QueueTask job) { // Setup this.startTime = DateTime.Now; this.loggingEnabled = job.Configuration.IsLoggingEnabled; this.currentTask = job; // Create a new HandBrake instance // Setup the HandBrake Instance instance = new HandBrakeInstance(); instance.Initialize(1); instance.EncodeCompleted += this.InstanceEncodeCompleted; instance.EncodeProgress += this.InstanceEncodeProgress; try { // Sanity Checking and Setup if (this.IsEncoding) { throw new Exception("HandBrake is already encoding."); } this.IsEncoding = true; // Enable logging if required. if (job.Configuration.IsLoggingEnabled) { try { this.SetupLogging(job); } catch (Exception) { this.IsEncoding = false; throw; } } // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(job); // We have to scan the source again but only the title so the HandBrake instance is initialised correctly. // Since the UI sends the crop params down, we don't have to do all the previews. instance.StartScan(job.Task.Source, 1, job.Task.Title); instance.ScanCompleted += delegate { ScanCompleted(job, instance); }; } catch (Exception exc) { this.InvokeEncodeCompleted(new EncodeCompletedEventArgs(false, exc, "An Error has occured.", this.currentTask.Task.Destination)); } }
/// <summary> /// Initializes a new instance of the <see cref="LibEncode"/> class. /// </summary> /// <param name="userSettingService"> /// The user Setting Service. /// </param> /// <param name="handBrakeInstance"> /// The hand Brake Instance. /// </param> public LibEncode(IUserSettingService userSettingService, IHandBrakeInstance handBrakeInstance) : base(userSettingService) { this.userSettingService = userSettingService; // Setup the HandBrake Instance this.instance = handBrakeInstance; this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; }
/// <summary> /// Initializes a new instance of the <see cref="LibScan"/> class. /// </summary> /// <param name="handBrakeInstance"> /// The hand Brake Instance. /// </param> public LibScan(IHandBrakeInstance handBrakeInstance) { logging = new StringBuilder(); header = GeneralUtilities.CreateCliLogHeader(); instance = handBrakeInstance; instance.Initialize(1); instance.ScanProgress += this.InstanceScanProgress; instance.ScanCompleted += this.InstanceScanCompleted; HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; }
/// <summary> /// Scan a Source Path. /// Title 0: scan all /// </summary> /// <param name="sourcePath"> /// Path to the file to scan /// </param> /// <param name="title"> /// int title number. 0 for scan all /// </param> /// <param name="previewCount"> /// The preview Count. /// </param> /// <param name="postAction"> /// The post Action. /// </param> public void Scan(string sourcePath, int title, int previewCount, Action <bool> postAction) { // Try to cleanup any previous scan instances. if (instance != null) { try { this.scanLog.Close(); this.scanLog.Dispose(); instance.Dispose(); } catch (Exception exc) { // Do Nothing } } // Clear down the logging this.logging.Clear(); try { // Make we don't pick up a stale last_scan_log_xyz.txt (and that we have rights to the file) if (File.Exists(dvdInfoPath)) { File.Delete(dvdInfoPath); } } catch (Exception) { // Do nothing. } if (!Directory.Exists(Path.GetDirectoryName(dvdInfoPath))) { Directory.CreateDirectory(Path.GetDirectoryName(dvdInfoPath)); } // Create a new scan log. scanLog = new StreamWriter(dvdInfoPath); // Create a new HandBrake Instance. instance = new HandBrakeInstance(); instance.Initialize(1); instance.ScanProgress += this.InstanceScanProgress; instance.ScanCompleted += this.InstanceScanCompleted; // Start the scan on a back this.ScanSource(sourcePath, title, previewCount); }
public void Dispose() { if (this.instance != null) { try { this.instance.Dispose(); this.instance = null; } catch (Exception e) { this.ServiceLogMessage("Unable to Dispose of LibScan: " + e); } } }
/// <summary> /// Initializes a new instance of the <see cref="LibScan"/> class. /// </summary> /// <param name="userSettingService"> /// The user Setting Service. /// </param> /// <param name="handBrakeInstance"> /// The hand Brake Instance. /// </param> public LibScan(IUserSettingService userSettingService, IHandBrakeInstance handBrakeInstance) { logging = new StringBuilder(); header = GeneralUtilities.CreateCliLogHeader( userSettingService.GetUserSetting<string>(ASUserSettingConstants.HandBrakeVersion), userSettingService.GetUserSetting<int>(ASUserSettingConstants.HandBrakeBuild)); instance = handBrakeInstance; instance.Initialize(1); instance.ScanProgress += this.InstanceScanProgress; instance.ScanCompleted += this.InstanceScanCompleted; HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; }
/// <summary> /// Start with a LibHb EncodeJob Object /// </summary> /// <param name="task"> /// The task. /// </param> /// <param name="configuration"> /// The configuration. /// </param> public void Start(EncodeTask task, HBConfiguration configuration) { try { // Sanity Checking and Setup if (this.IsEncoding) { throw new GeneralApplicationException("HandBrake is already encoding a file.", "Please stop the current encode. If the problem persists, please restart HandBrake.", null); } // Setup this.startTime = DateTime.Now; this.currentTask = task; this.currentConfiguration = configuration; // Create a new HandBrake instance // Setup the HandBrake Instance HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; this.IsEncoding = true; this.isPreviewInstance = task.IsPreviewEncode; this.SetupLogging(task.IsPreviewEncode); // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(task); this.ServiceLogMessage("Starting Encode ..."); // Get an EncodeJob object for the Interop Library this.instance.StartEncode(EncodeFactory.Create(task, configuration)); // Fire the Encode Started Event this.InvokeEncodeStarted(System.EventArgs.Empty); } catch (Exception exc) { this.IsEncoding = false; this.ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc); this.InvokeEncodeCompleted(new HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source)); } }
/// <summary> /// Start with a LibHb EncodeJob Object /// </summary> /// <param name="task"> /// The task. /// </param> /// <param name="configuration"> /// The configuration. /// </param> public void Start(EncodeTask task, HBConfiguration configuration) { try { // Sanity Checking and Setup if (this.IsEncoding) { throw new GeneralApplicationException(Resources.Queue_AlreadyEncoding, Resources.Queue_AlreadyEncodingSolution, null); } // Setup this.startTime = DateTime.Now; this.currentTask = task; this.currentConfiguration = configuration; // Create a new HandBrake instance // Setup the HandBrake Instance this.log.Reset(); // Reset so we have a clean log for the start of the encode. this.ServiceLogMessage("Starting Encode ..."); HandBrakeUtils.SetDvdNav(!configuration.IsDvdNavDisabled); this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; this.IsEncoding = true; this.isPreviewInstance = task.IsPreviewEncode; // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(task); // Get an EncodeJob object for the Interop Library this.instance.StartEncode(EncodeFactory.Create(task, configuration)); // Fire the Encode Started Event this.InvokeEncodeStarted(System.EventArgs.Empty); } catch (Exception exc) { this.IsEncoding = false; this.ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc); this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source, null, 0)); } }
/// <summary> /// The scan completed. /// </summary> /// <param name="job"> /// The job. /// </param> /// <param name="instance"> /// The instance. /// </param> private void ScanCompleted(QueueTask job, IHandBrakeInstance instance) { // Get an EncodeJob object for the Interop Library EncodeJob encodeJob = InteropModelCreator.GetEncodeJob(job); // Start the Encode instance.StartEncode(encodeJob); // Fire the Encode Started Event this.InvokeEncodeStarted(EventArgs.Empty); // Set the Process Priority switch (job.Configuration.ProcessPriority) { case "Realtime": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; break; case "High": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; break; case "Above Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; break; case "Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal; break; case "Low": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle; break; default: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal; break; } }
/// <summary> /// Initializes a new instance of the <see cref="ScanServiceWrapper"/> class. /// </summary> /// <param name="userSettingService"> /// The user setting service. /// </param> public ScanServiceWrapper(IUserSettingService userSettingService) { var useLibHb = userSettingService.GetUserSetting<bool>(UserSettingConstants.EnableLibHb); var useProcessIsolation = userSettingService.GetUserSetting<bool>(UserSettingConstants.EnableProcessIsolation); string port = userSettingService.GetUserSetting<string>(UserSettingConstants.ServerPort); if (useLibHb) { try { if (useProcessIsolation) { this.scanService = new IsolatedScanService(port); } else { HandbrakeInstance = new HandBrakeInstance(); this.scanService = new LibScan(userSettingService, HandbrakeInstance); } } catch(Exception exc) { // Try to recover from errors. userSettingService.SetUserSetting(UserSettingConstants.EnableLibHb, false); throw new GeneralApplicationException("Unable to initialise LibHB or Background worker service", "Falling back to using HandBrakeCLI.exe. Setting has been reset", exc); } } else { this.scanService = new ScanService(userSettingService); } this.scanService.ScanCompleted += this.ScanServiceScanCompleted; this.scanService.ScanStared += this.ScanServiceScanStared; this.scanService.ScanStatusChanged += this.ScanServiceScanStatusChanged; }
/// <summary> /// Initializes a new instance of the <see cref="ScanServiceWrapper"/> class. /// </summary> /// <param name="userSettingService"> /// The user setting service. /// </param> public ScanServiceWrapper(IUserSettingService userSettingService) { var useLibHb = userSettingService.GetUserSetting <bool>(UserSettingConstants.EnableLibHb); var useProcessIsolation = userSettingService.GetUserSetting <bool>(UserSettingConstants.EnableProcessIsolation); string port = userSettingService.GetUserSetting <string>(UserSettingConstants.ServerPort); if (useLibHb) { try { if (useProcessIsolation) { this.scanService = new IsolatedScanService(port); } else { HandbrakeInstance = new HandBrakeInstance(); this.scanService = new LibScan(HandbrakeInstance); } } catch (Exception exc) { // Try to recover from errors. userSettingService.SetUserSetting(UserSettingConstants.EnableLibHb, false); throw new GeneralApplicationException("Unable to initialise LibHB or Background worker service", "Falling back to using HandBrakeCLI.exe. Setting has been reset", exc); } } else { this.scanService = new ScanService(userSettingService); } this.scanService.ScanCompleted += this.ScanServiceScanCompleted; this.scanService.ScanStared += this.ScanServiceScanStared; this.scanService.ScanStatusChanged += this.ScanServiceScanStatusChanged; }
/// <summary> /// Start with a LibHb EncodeJob Object /// </summary> /// <param name="task"> /// The task. /// </param> /// <param name="configuration"> /// The configuration. /// </param> public void Start(EncodeTask task, HBConfiguration configuration) { try { // Setup this.startTime = DateTime.Now; this.currentTask = task; this.currentConfiguration = configuration; // Create a new HandBrake instance // Setup the HandBrake Instance HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; this.instance = HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; // Sanity Checking and Setup if (this.IsEncoding) { throw new Exception("HandBrake is already encoding."); } this.IsEncoding = true; this.SetupLogging(); // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(task); this.ServiceLogMessage("Starting Encode ..."); // Get an EncodeJob object for the Interop Library this.instance.StartEncode(EncodeFactory.Create(task, configuration)); // Fire the Encode Started Event this.InvokeEncodeStarted(System.EventArgs.Empty); // Set the Process Priority switch (configuration.ProcessPriority) { case "Realtime": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; break; case "High": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; break; case "Above Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; break; case "Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal; break; case "Low": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle; break; default: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal; break; } } catch (Exception exc) { this.IsEncoding = false; this.ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc); this.InvokeEncodeCompleted(new HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source)); } }
/// <summary> /// The get build. /// </summary> /// <returns> /// The <see cref="int"/>. /// </returns> public static string GetVersion() { IHandBrakeInstance instance = HandBrakeInstanceManager.MasterInstance; return IsNightly() ? string.Format("Nightly {0} ({1})", instance.Version, instance.Build) : string.Format("{0} ({1})", instance.Version, instance.Build); }
public static string GetVersionShort() { IHandBrakeInstance instance = HandBrakeInstanceManager.GetScanInstance(1); return(string.Format("{0} {1}", instance.Version, instance.Build)); }
/// <summary> /// The get build. /// </summary> /// <returns> /// The <see cref="int"/>. /// </returns> public static string GetVersion() { IHandBrakeInstance instance = HandBrakeInstanceManager.GetScanInstance(1); return(IsNightly() ? string.Format("Nightly {0} ({1})", instance.Version, instance.Build) : string.Format("{0} ({1})", instance.Version, instance.Build)); }
public static string GetVersionShort() { IHandBrakeInstance instance = HandBrakeInstanceManager.MasterInstance; return string.Format("{0} {1}", instance.Version, instance.Build); }
/// <summary> /// Scan a Source Path. /// Title 0: scan all /// </summary> /// <param name="sourcePath"> /// Path to the file to scan /// </param> /// <param name="title"> /// int title number. 0 for scan all /// </param> /// <param name="previewCount"> /// The preview Count. /// </param> /// <param name="postAction"> /// The post Action. /// </param> public void Scan(string sourcePath, int title, int previewCount, Action<bool> postAction) { // Try to cleanup any previous scan instances. if (instance != null) { try { this.scanLog.Close(); this.scanLog.Dispose(); instance.Dispose(); } catch (Exception exc) { // Do Nothing } } // Clear down the logging this.logging.Clear(); try { // Make we don't pick up a stale last_scan_log_xyz.txt (and that we have rights to the file) if (File.Exists(dvdInfoPath)) { File.Delete(dvdInfoPath); } } catch (Exception) { // Do nothing. } if (!Directory.Exists(Path.GetDirectoryName(dvdInfoPath))) { Directory.CreateDirectory(Path.GetDirectoryName(dvdInfoPath)); } // Create a new scan log. scanLog = new StreamWriter(dvdInfoPath); // Create a new HandBrake Instance. instance = new HandBrakeInstance(); instance.Initialize(1); instance.ScanProgress += this.InstanceScanProgress; instance.ScanCompleted += this.InstanceScanCompleted; // Start the scan on a back this.ScanSource(sourcePath, title, previewCount); }
/// <summary> /// Start with a LibHb EncodeJob Object /// </summary> /// <param name="job"> /// The job. /// </param> public void Start(QueueTask job) { // Setup this.startTime = DateTime.Now; this.currentTask = job; // Create a new HandBrake instance // Setup the HandBrake Instance HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; this.instance = HandBrakeInstanceManager.GetEncodeInstance(job.Configuration.Verbosity); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; try { // Sanity Checking and Setup if (this.IsEncoding) { throw new Exception("HandBrake is already encoding."); } this.IsEncoding = true; // Enable logging if required. try { this.SetupLogging(job, true); } catch (Exception) { this.IsEncoding = false; throw; } // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(job); // We have to scan the source again but only the title so the HandBrake instance is initialised correctly. // Since the UI sends the crop params down, we don't have to do all the previews. this.instance.ScanCompleted += delegate { // Process into internal structures. this.scannedSource = new Source { Titles = LibScan.ConvertTitles(this.instance.Titles, this.instance.FeatureTitle) }; // TODO work around the bad Internal API. this.ScanCompleted(job, this.instance); }; HandBrakeUtils.SetDvdNav(!job.Configuration.IsDvdNavDisabled); ServiceLogMessage("Scanning title for encoding ... "); this.instance.StartScan(job.ScannedSourcePath, job.Configuration.PreviewScanCount, TimeSpan.FromSeconds(job.Configuration.MinScanDuration), job.Task.Title); } catch (Exception exc) { ServiceLogMessage("Scan Failed ... " + Environment.NewLine + exc); this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "An Error has occured.", this.currentTask.Task.Destination)); } }
/// <summary> /// The scan completed. /// </summary> /// <param name="job"> /// The job. /// </param> /// <param name="instance"> /// The instance. /// </param> private void ScanCompleted(QueueTask job, IHandBrakeInstance instance) { // Get an EncodeJob object for the Interop Library EncodeJob encodeJob = InteropModelCreator.GetEncodeJob(job); // Start the Encode instance.StartEncode(encodeJob, job.Configuration.PreviewScanCount); // Fire the Encode Started Event this.InvokeEncodeStarted(EventArgs.Empty); // Set the Process Priority switch (job.Configuration.ProcessPriority) { case "Realtime": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; break; case "High": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; break; case "Above Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; break; case "Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal; break; case "Low": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle; break; default: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal; break; } }
/// <summary> /// Start with a LibHb EncodeJob Object /// </summary> /// <param name="job"> /// The job. /// </param> public void Start(QueueTask job) { // Setup this.startTime = DateTime.Now; this.loggingEnabled = job.Configuration.IsLoggingEnabled; this.currentTask = job; // Create a new HandBrake instance // Setup the HandBrake Instance instance = new HandBrakeInstance(); instance.Initialize(1); instance.EncodeCompleted += this.InstanceEncodeCompleted; instance.EncodeProgress += this.InstanceEncodeProgress; try { // Sanity Checking and Setup if (this.IsEncoding) { throw new Exception("HandBrake is already encoding."); } this.IsEncoding = true; // Enable logging if required. if (job.Configuration.IsLoggingEnabled) { try { this.SetupLogging(job); } catch (Exception) { this.IsEncoding = false; throw; } } // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(job); // We have to scan the source again but only the title so the HandBrake instance is initialised correctly. // Since the UI sends the crop params down, we don't have to do all the previews. instance.StartScan(job.Task.Source, job.Configuration.PreviewScanCount, job.Task.Title); instance.ScanCompleted += delegate { ScanCompleted(job, instance); }; } catch (Exception exc) { this.InvokeEncodeCompleted(new EncodeCompletedEventArgs(false, exc, "An Error has occured.", this.currentTask.Task.Destination)); } }
/// <summary> /// Scan a Source Path. /// Title 0: scan all /// </summary> /// <param name="sourcePath"> /// Path to the file to scan /// </param> /// <param name="title"> /// int title number. 0 for scan all /// </param> /// <param name="postAction"> /// The post Action. /// </param> /// <param name="configuraiton"> /// The configuraiton. /// </param> public void Scan(string sourcePath, int title, Action<bool, Source> postAction, HBConfiguration configuraiton) { // Try to cleanup any previous scan instances. if (this.instance != null) { try { lock (LogLock) { this.scanLog.Close(); this.scanLog.Dispose(); this.scanLog = null; } this.instance.Dispose(); } catch (Exception) { // Do Nothing } } // Handle the post scan operation. this.postScanOperation = postAction; // Clear down the logging this.logging.Clear(); try { // Make we don't pick up a stale last_scan_log_xyz.txt (and that we have rights to the file) if (File.Exists(this.dvdInfoPath)) { File.Delete(this.dvdInfoPath); } } catch (Exception exc) { Debug.WriteLine(exc); } if (!Directory.Exists(Path.GetDirectoryName(this.dvdInfoPath))) { Directory.CreateDirectory(Path.GetDirectoryName(this.dvdInfoPath)); } // Create a new scan log. this.scanLog = new StreamWriter(this.dvdInfoPath); // Create a new HandBrake Instance. HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; this.instance = HandBrakeInstanceManager.GetScanInstance(configuraiton.Verbosity); this.instance.ScanProgress += this.InstanceScanProgress; this.instance.ScanCompleted += this.InstanceScanCompleted; // Start the scan on a back this.ScanSource(sourcePath, title, configuraiton.PreviewScanCount, configuraiton); }
/// <summary> /// Start with a LibHb EncodeJob Object /// </summary> /// <param name="task"> /// The task. /// </param> /// <param name="configuration"> /// The configuration. /// </param> public void Start(EncodeTask task, HBConfiguration configuration) { try { // Setup this.startTime = DateTime.Now; this.currentTask = task; this.currentConfiguration = configuration; // Create a new HandBrake instance // Setup the HandBrake Instance HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged; HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged; this.instance = HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; // Sanity Checking and Setup if (this.IsEncoding) { throw new Exception("HandBrake is already encoding."); } this.IsEncoding = true; this.SetupLogging(); // Verify the Destination Path Exists, and if not, create it. this.VerifyEncodeDestinationPath(task); ServiceLogMessage("Starting Encode ..."); // Get an EncodeJob object for the Interop Library instance.StartEncode(EncodeFactory.Create(task, configuration)); // Fire the Encode Started Event this.InvokeEncodeStarted(System.EventArgs.Empty); // Set the Process Priority switch (configuration.ProcessPriority) { case "Realtime": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; break; case "High": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; break; case "Above Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; break; case "Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal; break; case "Low": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle; break; default: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal; break; } } catch (Exception exc) { this.IsEncoding = false; ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc); this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source)); } }
/// <summary> /// The scan completed. /// </summary> /// <param name="job"> /// The job. /// </param> /// <param name="instance"> /// The instance. /// </param> private void ScanCompleted(QueueTask job, IHandBrakeInstance instance) { ServiceLogMessage("Scan Completed. Setting up the job for encoding ..."); // Get an EncodeJob object for the Interop Library EncodeJob encodeJob = InteropModelCreator.GetEncodeJob(job); // Start the Encode Title title = this.scannedSource.Titles.FirstOrDefault(t => t.TitleNumber == job.Task.Title); if (title == null) { ServiceLogMessage("Title not found."); throw new Exception("Unable to get title for encoding. Encode Failed."); } Interop.Model.Scan.Title scannedTitle = new Interop.Model.Scan.Title { Resolution = new Size(title.Resolution.Width, title.Resolution.Height), ParVal = new Size(title.ParVal.Width, title.ParVal.Height), FramerateDenominator = title.FramerateDenominator, FramerateNumerator = title.FramerateNumerator, }; // TODO fix this tempory hack to pass in the required title information into the factory. try { ServiceLogMessage("Starting Encode ..."); instance.StartEncode(encodeJob, scannedTitle); } catch (Exception exc) { ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc); this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", job.Task.Source)); } // Fire the Encode Started Event this.InvokeEncodeStarted(System.EventArgs.Empty); // Set the Process Priority switch (job.Configuration.ProcessPriority) { case "Realtime": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; break; case "High": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High; break; case "Above Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal; break; case "Normal": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Normal; break; case "Low": Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle; break; default: Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.BelowNormal; break; } }
/// <summary> /// Scan a Source Path. /// Title 0: scan all /// </summary> /// <param name="sourcePath"> /// Path to the file to scan /// </param> /// <param name="title"> /// int title number. 0 for scan all /// </param> /// <param name="postAction"> /// The post Action. /// </param> /// <param name="configuraiton"> /// The configuraiton. /// </param> public void Scan(string sourcePath, int title, Action<bool, Source> postAction, HBConfiguration configuraiton) { // Try to cleanup any previous scan instances. if (this.instance != null) { try { this.instance.Dispose(); } catch (Exception) { // Do Nothing } } // Handle the post scan operation. this.postScanOperation = postAction; // Create a new HandBrake Instance. this.instance = HandBrakeInstanceManager.GetScanInstance(configuraiton.Verbosity); this.instance.ScanProgress += this.InstanceScanProgress; this.instance.ScanCompleted += this.InstanceScanCompleted; // Start the scan on a back this.ScanSource(sourcePath, title, configuraiton.PreviewScanCount, configuraiton); }