jellyfin-server/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs

160 lines
4.1 KiB
C#
Raw Normal View History

2016-06-30 04:23:52 +00:00
using System;
using System.Collections.Generic;
2014-11-15 02:31:03 +00:00
using System.Diagnostics;
using System.IO;
2016-06-30 04:23:52 +00:00
using MediaBrowser.Model.Logging;
2014-11-15 02:31:03 +00:00
2016-06-30 04:23:52 +00:00
namespace MediaBrowser.MediaEncoding.Encoder
2014-11-15 02:31:03 +00:00
{
2016-06-30 04:23:52 +00:00
public class EncoderValidator
2014-11-15 02:31:03 +00:00
{
private readonly ILogger _logger;
2016-06-30 04:23:52 +00:00
public EncoderValidator(ILogger logger)
2014-11-15 02:31:03 +00:00
{
_logger = logger;
}
2016-06-30 04:23:52 +00:00
public Tuple<List<string>, List<string>> Validate(string encoderPath)
2014-11-15 02:31:03 +00:00
{
2016-06-30 04:23:52 +00:00
_logger.Info("Validating media encoder at {0}", encoderPath);
2016-06-20 06:19:28 +00:00
var decoders = GetDecoders(encoderPath);
var encoders = GetEncoders(encoderPath);
2014-11-29 02:40:46 +00:00
2016-06-30 04:23:52 +00:00
_logger.Info("Encoder validation complete");
return new Tuple<List<string>, List<string>>(decoders, encoders);
2014-11-18 02:48:22 +00:00
}
2016-06-30 18:59:18 +00:00
private List<string> GetDecoders(string encoderAppPath)
2014-11-18 02:48:22 +00:00
{
string output = string.Empty;
2014-11-18 02:48:22 +00:00
try
{
2016-06-30 18:59:18 +00:00
output = GetProcessOutput(encoderAppPath, "-decoders");
2014-11-18 02:48:22 +00:00
}
catch
2014-11-15 02:31:03 +00:00
{
}
2014-11-18 02:48:22 +00:00
var found = new List<string>();
var required = new[]
2014-11-18 02:48:22 +00:00
{
"h264_qsv",
"mpeg2_qsv",
"vc1_qsv"
};
foreach (var codec in required)
{
var srch = " " + codec + " ";
2014-11-18 02:48:22 +00:00
2016-06-30 18:59:18 +00:00
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
2014-11-18 02:48:22 +00:00
{
2016-06-30 18:59:18 +00:00
_logger.Info("Decoder available: " + codec);
found.Add(codec);
2014-11-18 02:48:22 +00:00
}
}
return found;
2014-11-15 02:31:03 +00:00
}
2016-06-30 18:59:18 +00:00
private List<string> GetEncoders(string encoderAppPath)
2014-11-15 02:31:03 +00:00
{
string output = null;
try
{
2016-06-30 18:59:18 +00:00
output = GetProcessOutput(encoderAppPath, "-encoders");
}
catch
{
}
var found = new List<string>();
2014-11-15 02:31:03 +00:00
var required = new[]
{
"libx264",
2015-01-11 03:06:16 +00:00
"libx265",
2014-11-15 02:31:03 +00:00
"mpeg4",
"msmpeg4",
2016-07-01 01:06:18 +00:00
"libvpx",
"libvpx-vp9",
2014-11-15 02:31:03 +00:00
"aac",
"libmp3lame",
"libopus",
2016-07-01 01:06:18 +00:00
"libvorbis",
2016-06-30 18:59:18 +00:00
"srt",
"h264_nvenc",
2016-07-08 03:21:06 +00:00
"h264_qsv",
"ac3"
2014-11-15 02:31:03 +00:00
};
2016-07-01 01:06:18 +00:00
output = output ?? string.Empty;
2016-07-08 03:21:06 +00:00
var index = 0;
foreach (var codec in required)
2014-11-15 02:31:03 +00:00
{
var srch = " " + codec + " ";
2014-11-15 02:31:03 +00:00
2016-06-30 18:59:18 +00:00
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
{
2016-07-08 03:21:06 +00:00
if (index < required.Length - 1)
{
_logger.Info("Encoder available: " + codec);
}
found.Add(codec);
2014-11-15 02:31:03 +00:00
}
2016-07-08 03:21:06 +00:00
index++;
2014-11-15 02:31:03 +00:00
}
return found;
2014-11-15 02:31:03 +00:00
}
2016-06-30 18:59:18 +00:00
private string GetProcessOutput(string path, string arguments)
2014-11-15 02:31:03 +00:00
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
FileName = path,
Arguments = arguments,
WindowStyle = ProcessWindowStyle.Hidden,
ErrorDialog = false,
RedirectStandardOutput = true
2014-11-15 02:31:03 +00:00
}
};
using (process)
{
process.Start();
try
{
2016-06-30 04:41:15 +00:00
return process.StandardOutput.ReadToEnd();
2014-11-15 02:31:03 +00:00
}
catch
{
2016-06-30 04:41:15 +00:00
_logger.Info("Killing process {0} {1}", path, arguments);
2014-11-15 02:31:03 +00:00
// Hate having to do this
try
{
process.Kill();
}
catch (Exception ex1)
{
2016-06-30 18:59:18 +00:00
_logger.ErrorException("Error killing process", ex1);
2014-11-15 02:31:03 +00:00
}
throw;
}
}
}
}
}