jellyfin/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs

160 lines
4.3 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;
using System.IO;
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-06-30 06:23:52 +02:00
public EncoderValidator(ILogger logger)
2014-11-15 03:31:03 +01:00
{
_logger = logger;
}
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
}
private List<string> GetDecoders(string ffmpegPath)
2014-11-18 03:48:22 +01:00
{
string output = string.Empty;
2014-11-18 03:48:22 +01:00
try
{
output = GetFFMpegOutput(ffmpegPath, "-decoders");
2014-11-18 03:48:22 +01:00
}
catch
2014-11-15 03:31:03 +01:00
{
}
//_logger.Debug("ffmpeg decoder query result: {0}", output ?? string.Empty);
2014-11-18 03:48:22 +01:00
var found = new List<string>();
var required = new[]
2014-11-18 03:48:22 +01:00
{
"h264_qsv",
"mpeg2_qsv",
"vc1_qsv"
};
foreach (var codec in required)
{
var srch = " " + codec + " ";
2014-11-18 03:48:22 +01:00
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
2014-11-18 03:48:22 +01:00
{
_logger.Warn("ffmpeg is missing decoder " + codec);
2014-11-18 03:48:22 +01:00
}
else
2014-11-18 03:48:22 +01:00
{
found.Add(codec);
2014-11-18 03:48:22 +01:00
}
}
return found;
2014-11-15 03:31:03 +01:00
}
private List<string> GetEncoders(string ffmpegPath)
2014-11-15 03:31:03 +01:00
{
string output = null;
try
{
output = GetFFMpegOutput(ffmpegPath, "-encoders");
}
catch
{
}
//_logger.Debug("ffmpeg encoder query result: {0}", output ?? string.Empty);
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",
2015-12-21 17:41:32 +01:00
//"libvpx",
2014-11-15 03:31:03 +01:00
//"libvpx-vp9",
"aac",
"libmp3lame",
"libopus",
2015-12-21 17:41:32 +01:00
//"libvorbis",
2014-11-15 03:31:03 +01:00
"srt"
};
foreach (var codec in required)
2014-11-15 03:31:03 +01:00
{
var srch = " " + codec + " ";
2014-11-15 03:31:03 +01:00
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
{
_logger.Warn("ffmpeg is missing encoder " + codec);
}
else
{
found.Add(codec);
2014-11-15 03:31:03 +01:00
}
}
return found;
2014-11-15 03:31:03 +01:00
}
2014-11-18 03:48:22 +01:00
private string GetFFMpegOutput(string path, string arguments)
2014-11-15 03:31:03 +01:00
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
FileName = path,
Arguments = arguments,
WindowStyle = ProcessWindowStyle.Hidden,
ErrorDialog = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
using (process)
{
process.Start();
try
{
process.BeginErrorReadLine();
using (var reader = new StreamReader(process.StandardOutput.BaseStream))
{
return reader.ReadToEnd();
}
}
catch
{
// Hate having to do this
try
{
process.Kill();
}
catch (Exception ex1)
{
_logger.ErrorException("Error killing ffmpeg", ex1);
}
throw;
}
}
}
}
}