Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
- namespace VideoTranscoderLibrary
- {
- public class TranscodeJob
- {
- public TranscodeJob(TranscodeInfo transcodeInfo)
- {
- ErrorMessages = new Dictionary<string, Exception>();
- TranscodeInfo = transcodeInfo;
- InputFile = TranscodeInfo.InputFile;
- OutputFile = new FileInfo(GenerateOutputFileName());
- HandbrakeLog = new StringBuilder();
- }
- public IDictionary<string, Exception> ErrorMessages { get; set; }
- public string TranscodeLog { get { return HandbrakeLog.ToString(); } }
- private TranscodeInfo TranscodeInfo { get; set; }
- private FileInfo InputFile { get; set; }
- private FileInfo OutputFile { get; set; }
- private StringBuilder HandbrakeLog { get; set; }
- public void Start()
- {
- try
- {
- var handbrake = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- FileName = GetExecutablePath() + @"Handbrake\HandBrakeCLI.exe",
- WorkingDirectory = InputFile.DirectoryName,
- Arguments = GenerateArguments(),
- CreateNoWindow = true,
- UseShellExecute = false,
- RedirectStandardError = true
- }
- };
- handbrake.ErrorDataReceived += (sender, args) => HandbrakeLog.AppendLine(args.Data);
- handbrake.Start();
- handbrake.BeginErrorReadLine();
- handbrake.WaitForExit();
- }
- catch (Exception e)
- {
- LogErrors(e);
- }
- }
- private string GenerateOutputFileName()
- {
- var fileName = Path.GetFileNameWithoutExtension(InputFile.FullName);
- if (InputFile.Extension.Equals(".mp4", StringComparison.InvariantCultureIgnoreCase))
- {
- fileName += "-converted";
- }
- return InputFile.DirectoryName + @"\" + fileName + ".mp4";
- }
- private static string GetExecutablePath()
- {
- return Path.Combine(Environment.CurrentDirectory, "Executables") + @"\";
- }
- private string GenerateArguments()
- {
- return String.Format("-i {0} -o {1} {2}", InputFile.FullName, OutputFile.FullName, GetQuality());
- }
- private string GetQuality()
- {
- string quality;
- switch (TranscodeInfo.OutputQuality)
- {
- case VideoQuality.Low:
- quality = "--preset=\"iPhone & iPod Touch\"";
- break;
- case VideoQuality.High:
- quality = "--preset=\"High Profile\"";
- break;
- case VideoQuality.Same:
- quality = String.Empty;
- break;
- default:
- quality = String.Empty;
- break;
- }
- return quality;
- }
- private void LogErrors(Exception exception)
- {
- var currentException = exception;
- while (currentException != null)
- {
- ErrorMessages.Add(currentException.Message, currentException);
- currentException = currentException.InnerException;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement