diff options
| author | LukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com> | 2012-08-10 11:17:52 -0400 |
|---|---|---|
| committer | LukePulverenti Luke Pulverenti luke pulverenti <LukePulverenti Luke Pulverenti luke.pulverenti@gmail.com> | 2012-08-10 11:17:52 -0400 |
| commit | 92056c4d3d409590b23996eef2795419d4b9617b (patch) | |
| tree | a86fe4b43f7da09c376d56f13512b4073afd6cb7 /MediaBrowser.Api/Transcoding/TranscodingJob.cs | |
| parent | 7303c6be3213891330da1dc44e87d44206dd653d (diff) | |
Added audio transcoding
Diffstat (limited to 'MediaBrowser.Api/Transcoding/TranscodingJob.cs')
| -rw-r--r-- | MediaBrowser.Api/Transcoding/TranscodingJob.cs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/MediaBrowser.Api/Transcoding/TranscodingJob.cs b/MediaBrowser.Api/Transcoding/TranscodingJob.cs new file mode 100644 index 000000000..e504fec09 --- /dev/null +++ b/MediaBrowser.Api/Transcoding/TranscodingJob.cs @@ -0,0 +1,102 @@ +using System;
+using System.Diagnostics;
+using System.IO;
+using System.Threading;
+using MediaBrowser.Common.Logging;
+
+namespace MediaBrowser.Api.Transcoding
+{
+ /// <summary>
+ /// Represents an active transcoding job
+ /// </summary>
+ public class TranscodingJob
+ {
+ public string InputFile { get; set; }
+ public string OutputFile { get; set; }
+ public string TranscoderPath { get; set; }
+ public string Arguments { get; set; }
+
+ public TranscoderJobStatus Status { get; private set; }
+
+ /// <summary>
+ /// Starts the job
+ /// </summary>
+ public void Start()
+ {
+ ApiService.AddTranscodingJob(this);
+
+ ProcessStartInfo startInfo = new ProcessStartInfo();
+
+ startInfo.CreateNoWindow = true;
+
+ startInfo.UseShellExecute = false;
+
+ startInfo.FileName = TranscoderPath;
+ startInfo.WorkingDirectory = Path.GetDirectoryName(TranscoderPath);
+ startInfo.Arguments = Arguments;
+
+ Logger.LogInfo("TranscodingJob.Start: " + TranscoderPath + " " + Arguments);
+
+ Process process = new Process();
+
+ process.StartInfo = startInfo;
+
+ process.EnableRaisingEvents = true;
+
+ process.Start();
+
+ process.Exited += process_Exited;
+ }
+
+ void process_Exited(object sender, EventArgs e)
+ {
+ ApiService.RemoveTranscodingJob(this);
+
+ Process process = sender as Process;
+
+ // If it terminated with an error
+ if (process.ExitCode != 0)
+ {
+ Status = TranscoderJobStatus.Error;
+
+ // Delete this since it won't be valid
+ if (File.Exists(OutputFile))
+ {
+ File.Delete(OutputFile);
+ }
+ }
+ else
+ {
+ Status = TranscoderJobStatus.Completed;
+ }
+
+ process.Dispose();
+ }
+
+ /// <summary>
+ /// Provides a helper to wait for the job to exit
+ /// </summary>
+ public void WaitForExit()
+ {
+ while (true)
+ {
+ TranscoderJobStatus status = Status;
+
+ if (status == TranscoderJobStatus.Completed || status == TranscoderJobStatus.Error)
+ {
+ break;
+ }
+
+ Thread.Sleep(500);
+ }
+ }
+ }
+
+ public enum TranscoderJobStatus
+ {
+ Queued,
+ Started,
+ Completed,
+ Error
+ }
+}
|
