jellyfin/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs

215 lines
5.8 KiB
C#
Raw Normal View History

2016-06-30 06:23:52 +02:00
using System;
using System.Collections.Generic;
2014-11-15 03:31:03 +01:00
using System.Diagnostics;
2016-11-06 18:30:44 +01:00
using System.Globalization;
2016-11-01 05:07:12 +01:00
using MediaBrowser.Model.Diagnostics;
2016-06-30 06:23:52 +02:00
using MediaBrowser.Model.Logging;
2014-11-15 03:31:03 +01:00
2016-06-30 06:23:52 +02:00
namespace MediaBrowser.MediaEncoding.Encoder
2014-11-15 03:31:03 +01:00
{
2016-06-30 06:23:52 +02:00
public class EncoderValidator
2014-11-15 03:31:03 +01:00
{
private readonly ILogger _logger;
2016-11-01 05:07:12 +01:00
private readonly IProcessFactory _processFactory;
2014-11-15 03:31:03 +01:00
2016-11-01 05:07:12 +01:00
public EncoderValidator(ILogger logger, IProcessFactory processFactory)
2014-11-15 03:31:03 +01:00
{
_logger = logger;
2016-11-01 05:07:12 +01:00
_processFactory = processFactory;
2014-11-15 03:31:03 +01:00
}
2016-06-30 06:23:52 +02:00
public Tuple<List<string>, List<string>> Validate(string encoderPath)
2014-11-15 03:31:03 +01:00
{
2016-06-30 06:23:52 +02:00
_logger.Info("Validating media encoder at {0}", encoderPath);
2016-06-20 08:19:28 +02:00
var decoders = GetDecoders(encoderPath);
var encoders = GetEncoders(encoderPath);
2014-11-29 03:40:46 +01:00
2016-06-30 06:23:52 +02:00
_logger.Info("Encoder validation complete");
return new Tuple<List<string>, List<string>>(decoders, encoders);
2014-11-18 03:48:22 +01:00
}
2016-10-28 20:35:17 +02:00
public bool ValidateVersion(string encoderAppPath, bool logOutput)
2016-09-22 08:57:31 +02:00
{
string output = string.Empty;
try
{
output = GetProcessOutput(encoderAppPath, "-version");
}
2016-11-13 22:04:21 +01:00
catch (Exception ex)
2016-09-22 08:57:31 +02:00
{
2016-11-13 22:04:21 +01:00
if (logOutput)
{
_logger.ErrorException("Error validating encoder", ex);
}
2016-09-22 08:57:31 +02:00
}
2016-11-13 22:04:21 +01:00
if (string.IsNullOrWhiteSpace(output))
2016-10-28 20:35:17 +02:00
{
2016-11-13 22:04:21 +01:00
return false;
2016-10-28 20:35:17 +02:00
}
2016-11-13 22:04:21 +01:00
if (logOutput)
2016-09-22 08:57:31 +02:00
{
2016-11-13 22:04:21 +01:00
_logger.Info("ffmpeg info: {0}", output);
2016-09-22 08:57:31 +02:00
}
if (output.IndexOf("Libav developers", StringComparison.OrdinalIgnoreCase) != -1)
{
return false;
}
2016-11-06 18:30:44 +01:00
output = " " + output + " ";
for (var i = 2013; i <= 2015; i++)
{
var yearString = i.ToString(CultureInfo.InvariantCulture);
if (output.IndexOf(" " + yearString + " ", StringComparison.OrdinalIgnoreCase) != -1)
{
return false;
}
}
2016-09-22 08:57:31 +02:00
return true;
}
2016-06-30 20:59:18 +02:00
private List<string> GetDecoders(string encoderAppPath)
2014-11-18 03:48:22 +01:00
{
string output = string.Empty;
2014-11-18 03:48:22 +01:00
try
{
2016-06-30 20:59:18 +02:00
output = GetProcessOutput(encoderAppPath, "-decoders");
2014-11-18 03:48:22 +01:00
}
2016-11-19 08:51:07 +01:00
catch (Exception )
2014-11-15 03:31:03 +01:00
{
2016-11-19 08:51:07 +01:00
//_logger.ErrorException("Error detecting available decoders", ex);
2014-11-15 03:31:03 +01:00
}
2014-11-18 03:48:22 +01:00
var found = new List<string>();
var required = new[]
2014-11-18 03:48:22 +01:00
{
2017-03-16 18:21:24 +01:00
"mpeg2video",
"h264_qsv",
"hevc_qsv",
"mpeg2_qsv",
"vc1_qsv"
};
foreach (var codec in required)
{
var srch = " " + codec + " ";
2014-11-18 03:48:22 +01:00
2016-06-30 20:59:18 +02:00
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
2014-11-18 03:48:22 +01:00
{
2016-06-30 20:59:18 +02:00
_logger.Info("Decoder available: " + codec);
found.Add(codec);
2014-11-18 03:48:22 +01:00
}
}
return found;
2014-11-15 03:31:03 +01:00
}
2016-06-30 20:59:18 +02:00
private List<string> GetEncoders(string encoderAppPath)
2014-11-15 03:31:03 +01:00
{
string output = null;
try
{
2016-06-30 20:59:18 +02:00
output = GetProcessOutput(encoderAppPath, "-encoders");
}
catch
{
}
var found = new List<string>();
2014-11-15 03:31:03 +01:00
var required = new[]
{
"libx264",
2015-01-11 04:06:16 +01:00
"libx265",
2014-11-15 03:31:03 +01:00
"mpeg4",
"msmpeg4",
2016-07-01 03:06:18 +02:00
"libvpx",
"libvpx-vp9",
2014-11-15 03:31:03 +01:00
"aac",
"libmp3lame",
"libopus",
2016-07-01 03:06:18 +02:00
"libvorbis",
2016-06-30 20:59:18 +02:00
"srt",
"h264_nvenc",
"hevc_nvenc",
2016-07-08 05:21:06 +02:00
"h264_qsv",
"hevc_qsv",
2016-08-05 07:12:25 +02:00
"h264_omx",
"hevc_omx",
2016-08-07 22:13:30 +02:00
"h264_vaapi",
"hevc_vaapi",
2016-07-08 05:21:06 +02:00
"ac3"
2014-11-15 03:31:03 +01:00
};
2016-07-01 03:06:18 +02:00
output = output ?? string.Empty;
2016-07-08 05:21:06 +02:00
var index = 0;
foreach (var codec in required)
2014-11-15 03:31:03 +01:00
{
var srch = " " + codec + " ";
2014-11-15 03:31:03 +01:00
2016-06-30 20:59:18 +02:00
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
{
2016-07-08 05:21:06 +02:00
if (index < required.Length - 1)
{
_logger.Info("Encoder available: " + codec);
}
found.Add(codec);
2014-11-15 03:31:03 +01:00
}
2016-07-08 05:21:06 +02:00
index++;
2014-11-15 03:31:03 +01:00
}
return found;
2014-11-15 03:31:03 +01:00
}
2016-06-30 20:59:18 +02:00
private string GetProcessOutput(string path, string arguments)
2014-11-15 03:31:03 +01:00
{
2016-11-01 05:07:12 +01:00
var process = _processFactory.Create(new ProcessOptions
2014-11-15 03:31:03 +01:00
{
2016-11-01 05:07:12 +01:00
CreateNoWindow = true,
UseShellExecute = false,
FileName = path,
Arguments = arguments,
IsHidden = true,
ErrorDialog = false,
RedirectStandardOutput = true
});
2014-11-15 03:31:03 +01:00
2016-10-16 19:11:32 +02:00
_logger.Info("Running {0} {1}", path, arguments);
2014-11-15 03:31:03 +01:00
using (process)
{
process.Start();
try
{
2016-06-30 06:41:15 +02:00
return process.StandardOutput.ReadToEnd();
2014-11-15 03:31:03 +01:00
}
catch
{
2016-06-30 06:41:15 +02:00
_logger.Info("Killing process {0} {1}", path, arguments);
2014-11-15 03:31:03 +01:00
// Hate having to do this
try
{
process.Kill();
}
catch (Exception ex1)
{
2016-06-30 20:59:18 +02:00
_logger.ErrorException("Error killing process", ex1);
2014-11-15 03:31:03 +01:00
}
throw;
}
}
}
}
}