Advertisement
Guest User

Untitled

a guest
Dec 18th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6.  
  7. namespace VideoTranscoderLibrary
  8. {
  9.     public class TranscodeJob
  10.     {
  11.         public TranscodeJob(TranscodeInfo transcodeInfo)
  12.         {
  13.             ErrorMessages = new Dictionary<string, Exception>();
  14.             TranscodeInfo = transcodeInfo;
  15.             InputFile = TranscodeInfo.InputFile;
  16.             OutputFile = new FileInfo(GenerateOutputFileName());
  17.             HandbrakeLog = new StringBuilder();
  18.         }
  19.  
  20.         public IDictionary<string, Exception> ErrorMessages { get; set; }
  21.  
  22.         public string TranscodeLog { get { return HandbrakeLog.ToString(); } }
  23.  
  24.         private TranscodeInfo TranscodeInfo { get; set; }
  25.  
  26.         private FileInfo InputFile { get; set; }
  27.  
  28.         private FileInfo OutputFile { get; set; }
  29.  
  30.         private StringBuilder HandbrakeLog { get; set; }
  31.  
  32.         public void Start()
  33.         {
  34.             try
  35.             {
  36.                 var handbrake = new Process
  37.                 {
  38.                     StartInfo = new ProcessStartInfo
  39.                     {
  40.                         FileName = GetExecutablePath() + @"Handbrake\HandBrakeCLI.exe",
  41.                         WorkingDirectory = InputFile.DirectoryName,
  42.                         Arguments = GenerateArguments(),
  43.                         CreateNoWindow = true,
  44.                         UseShellExecute = false,
  45.                         RedirectStandardError = true
  46.                     }
  47.                 };
  48.  
  49.                 handbrake.ErrorDataReceived += (sender, args) => HandbrakeLog.AppendLine(args.Data);
  50.                 handbrake.Start();
  51.                 handbrake.BeginErrorReadLine();
  52.                 handbrake.WaitForExit();
  53.             }
  54.             catch (Exception e)
  55.             {
  56.                 LogErrors(e);
  57.             }
  58.         }
  59.  
  60.         private string GenerateOutputFileName()
  61.         {
  62.             var fileName = Path.GetFileNameWithoutExtension(InputFile.FullName);
  63.             if (InputFile.Extension.Equals(".mp4", StringComparison.InvariantCultureIgnoreCase))
  64.             {
  65.                 fileName += "-converted";
  66.             }
  67.             return InputFile.DirectoryName + @"\" + fileName + ".mp4";
  68.         }
  69.  
  70.         private static string GetExecutablePath()
  71.         {
  72.             return Path.Combine(Environment.CurrentDirectory, "Executables") + @"\";
  73.         }
  74.  
  75.         private string GenerateArguments()
  76.         {
  77.             return String.Format("-i {0} -o {1} {2}", InputFile.FullName, OutputFile.FullName, GetQuality());
  78.         }
  79.  
  80.         private string GetQuality()
  81.         {
  82.             string quality;
  83.             switch (TranscodeInfo.OutputQuality)
  84.             {
  85.                 case VideoQuality.Low:
  86.                     quality = "--preset=\"iPhone & iPod Touch\"";
  87.                     break;
  88.  
  89.                 case VideoQuality.High:
  90.                     quality = "--preset=\"High Profile\"";
  91.                     break;
  92.  
  93.                 case VideoQuality.Same:
  94.                     quality = String.Empty;
  95.                     break;
  96.  
  97.                 default:
  98.                     quality = String.Empty;
  99.                     break;
  100.             }
  101.             return quality;
  102.         }
  103.  
  104.         private void LogErrors(Exception exception)
  105.         {
  106.             var currentException = exception;
  107.             while (currentException != null)
  108.             {
  109.                 ErrorMessages.Add(currentException.Message, currentException);
  110.                 currentException = currentException.InnerException;
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement