Use ValueTuple and Linq

This commit is contained in:
Bond_009 2019-01-02 01:23:49 +01:00 committed by Vasily
parent 96ad22a009
commit 0042b96c80
2 changed files with 17 additions and 34 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq;
using MediaBrowser.Model.Diagnostics; using MediaBrowser.Model.Diagnostics;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -17,21 +18,21 @@ namespace MediaBrowser.MediaEncoding.Encoder
_processFactory = processFactory; _processFactory = processFactory;
} }
public Tuple<List<string>, List<string>> Validate(string encoderPath) public (IEnumerable<string> decoders, IEnumerable<string> encoders) Validate(string encoderPath)
{ {
_logger.LogInformation("Validating media encoder at {0}", encoderPath); _logger.LogInformation("Validating media encoder at {EncoderPath}", encoderPath);
var decoders = GetDecoders(encoderPath); var decoders = GetDecoders(encoderPath);
var encoders = GetEncoders(encoderPath); var encoders = GetEncoders(encoderPath);
_logger.LogInformation("Encoder validation complete"); _logger.LogInformation("Encoder validation complete");
return new Tuple<List<string>, List<string>>(decoders, encoders); return (decoders, encoders);
} }
public bool ValidateVersion(string encoderAppPath, bool logOutput) public bool ValidateVersion(string encoderAppPath, bool logOutput)
{ {
string output = string.Empty; string output = null;
try try
{ {
output = GetProcessOutput(encoderAppPath, "-version"); output = GetProcessOutput(encoderAppPath, "-version");
@ -70,7 +71,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
return true; return true;
} }
private List<string> GetDecoders(string encoderAppPath) private IEnumerable<string> GetDecoders(string encoderAppPath)
{ {
string output = null; string output = null;
try try
@ -84,7 +85,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
if (string.IsNullOrWhiteSpace(output)) if (string.IsNullOrWhiteSpace(output))
{ {
return new List<string>(); return Enumerable.Empty<string>();
} }
var required = new[] var required = new[]
@ -104,23 +105,14 @@ namespace MediaBrowser.MediaEncoding.Encoder
"hevc" "hevc"
}; };
var found = new List<string>(); var found = required.Where(x => output.IndexOf(x, StringComparison.OrdinalIgnoreCase) != -1);
foreach (var codec in required)
{
var srch = " " + codec + " ";
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
{
found.Add(codec);
}
}
_logger.LogInformation("Available decoders: {Codecs}", found); _logger.LogInformation("Available decoders: {Codecs}", found);
return found; return found;
} }
private List<string> GetEncoders(string encoderAppPath) private IEnumerable<string> GetEncoders(string encoderAppPath)
{ {
string output = null; string output = null;
try try
@ -134,7 +126,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
if (string.IsNullOrWhiteSpace(output)) if (string.IsNullOrWhiteSpace(output))
{ {
return new List<string>(); return Enumerable.Empty<string>();
} }
var required = new[] var required = new[]
@ -161,16 +153,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
"ac3" "ac3"
}; };
var found = new List<string>(); var found = required.Where(x => output.IndexOf(x, StringComparison.OrdinalIgnoreCase) != -1);
foreach (var codec in required)
{
var srch = " " + codec + " ";
if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
{
found.Add(codec);
}
}
_logger.LogInformation("Available encoders: {Codecs}", found); _logger.LogInformation("Available encoders: {Codecs}", found);
@ -179,7 +162,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
private string GetProcessOutput(string path, string arguments) private string GetProcessOutput(string path, string arguments)
{ {
var process = _processFactory.Create(new ProcessOptions IProcess process = _processFactory.Create(new ProcessOptions
{ {
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = false, UseShellExecute = false,
@ -190,7 +173,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
RedirectStandardOutput = true RedirectStandardOutput = true
}); });
_logger.LogInformation("Running {path} {arguments}", path, arguments); _logger.LogInformation("Running {Path} {Arguments}", path, arguments);
using (process) using (process)
{ {

View File

@ -175,8 +175,8 @@ namespace MediaBrowser.MediaEncoding.Encoder
{ {
var result = new EncoderValidator(_logger, _processFactory).Validate(FFMpegPath); var result = new EncoderValidator(_logger, _processFactory).Validate(FFMpegPath);
SetAvailableDecoders(result.Item1); SetAvailableDecoders(result.decoders);
SetAvailableEncoders(result.Item2); SetAvailableEncoders(result.encoders);
if (EnableEncoderFontFile) if (EnableEncoderFontFile)
{ {
@ -401,14 +401,14 @@ namespace MediaBrowser.MediaEncoding.Encoder
} }
private List<string> _encoders = new List<string>(); private List<string> _encoders = new List<string>();
public void SetAvailableEncoders(List<string> list) public void SetAvailableEncoders(IEnumerable<string> list)
{ {
_encoders = list.ToList(); _encoders = list.ToList();
//_logger.Info("Supported encoders: {0}", string.Join(",", list.ToArray())); //_logger.Info("Supported encoders: {0}", string.Join(",", list.ToArray()));
} }
private List<string> _decoders = new List<string>(); private List<string> _decoders = new List<string>();
public void SetAvailableDecoders(List<string> list) public void SetAvailableDecoders(IEnumerable<string> list)
{ {
_decoders = list.ToList(); _decoders = list.ToList();
//_logger.Info("Supported decoders: {0}", string.Join(",", list.ToArray())); //_logger.Info("Supported decoders: {0}", string.Join(",", list.ToArray()));