EncoderValidator.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using MediaBrowser.Model.Diagnostics;
  6. using Microsoft.Extensions.Logging;
  7. namespace MediaBrowser.MediaEncoding.Encoder
  8. {
  9. public class EncoderValidator
  10. {
  11. private readonly ILogger _logger;
  12. private readonly IProcessFactory _processFactory;
  13. public EncoderValidator(ILogger logger, IProcessFactory processFactory)
  14. {
  15. _logger = logger;
  16. _processFactory = processFactory;
  17. }
  18. public (IEnumerable<string> decoders, IEnumerable<string> encoders) Validate(string encoderPath)
  19. {
  20. _logger.LogInformation("Validating media encoder at {EncoderPath}", encoderPath);
  21. var decoders = GetDecoders(encoderPath);
  22. var encoders = GetEncoders(encoderPath);
  23. _logger.LogInformation("Encoder validation complete");
  24. return (decoders, encoders);
  25. }
  26. public bool ValidateVersion(string encoderAppPath, bool logOutput)
  27. {
  28. string output = null;
  29. try
  30. {
  31. output = GetProcessOutput(encoderAppPath, "-version");
  32. }
  33. catch (Exception ex)
  34. {
  35. if (logOutput)
  36. {
  37. _logger.LogError(ex, "Error validating encoder");
  38. }
  39. }
  40. if (string.IsNullOrWhiteSpace(output))
  41. {
  42. return false;
  43. }
  44. _logger.LogInformation("ffmpeg info: {0}", output);
  45. if (output.IndexOf("Libav developers", StringComparison.OrdinalIgnoreCase) != -1)
  46. {
  47. return false;
  48. }
  49. output = " " + output + " ";
  50. for (var i = 2013; i <= 2015; i++)
  51. {
  52. var yearString = i.ToString(CultureInfo.InvariantCulture);
  53. if (output.IndexOf(" " + yearString + " ", StringComparison.OrdinalIgnoreCase) != -1)
  54. {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. private IEnumerable<string> GetDecoders(string encoderAppPath)
  61. {
  62. string output = null;
  63. try
  64. {
  65. output = GetProcessOutput(encoderAppPath, "-decoders");
  66. }
  67. catch (Exception ex)
  68. {
  69. _logger.LogError(ex, "Error detecting available decoders");
  70. }
  71. if (string.IsNullOrWhiteSpace(output))
  72. {
  73. return Enumerable.Empty<string>();
  74. }
  75. var required = new[]
  76. {
  77. "mpeg2video",
  78. "h264_qsv",
  79. "hevc_qsv",
  80. "mpeg2_qsv",
  81. "vc1_qsv",
  82. "h264_cuvid",
  83. "hevc_cuvid",
  84. "dts",
  85. "ac3",
  86. "aac",
  87. "mp3",
  88. "h264",
  89. "hevc"
  90. };
  91. var found = required.Where(x => output.IndexOf(x, StringComparison.OrdinalIgnoreCase) != -1);
  92. _logger.LogInformation("Available decoders: {Codecs}", found);
  93. return found;
  94. }
  95. private IEnumerable<string> GetEncoders(string encoderAppPath)
  96. {
  97. string output = null;
  98. try
  99. {
  100. output = GetProcessOutput(encoderAppPath, "-encoders");
  101. }
  102. catch (Exception ex)
  103. {
  104. _logger.LogError(ex, "Error getting encoders");
  105. }
  106. if (string.IsNullOrWhiteSpace(output))
  107. {
  108. return Enumerable.Empty<string>();
  109. }
  110. var required = new[]
  111. {
  112. "libx264",
  113. "libx265",
  114. "mpeg4",
  115. "msmpeg4",
  116. "libvpx",
  117. "libvpx-vp9",
  118. "aac",
  119. "libmp3lame",
  120. "libopus",
  121. "libvorbis",
  122. "srt",
  123. "h264_nvenc",
  124. "hevc_nvenc",
  125. "h264_qsv",
  126. "hevc_qsv",
  127. "h264_omx",
  128. "hevc_omx",
  129. "h264_vaapi",
  130. "hevc_vaapi",
  131. "ac3"
  132. };
  133. var found = required.Where(x => output.IndexOf(x, StringComparison.OrdinalIgnoreCase) != -1);
  134. _logger.LogInformation("Available encoders: {Codecs}", found);
  135. return found;
  136. }
  137. private string GetProcessOutput(string path, string arguments)
  138. {
  139. IProcess process = _processFactory.Create(new ProcessOptions
  140. {
  141. CreateNoWindow = true,
  142. UseShellExecute = false,
  143. FileName = path,
  144. Arguments = arguments,
  145. IsHidden = true,
  146. ErrorDialog = false,
  147. RedirectStandardOutput = true
  148. });
  149. _logger.LogInformation("Running {Path} {Arguments}", path, arguments);
  150. using (process)
  151. {
  152. process.Start();
  153. try
  154. {
  155. return process.StandardOutput.ReadToEnd();
  156. }
  157. catch
  158. {
  159. _logger.LogInformation("Killing process {path} {arguments}", path, arguments);
  160. // Hate having to do this
  161. try
  162. {
  163. process.Kill();
  164. }
  165. catch (Exception ex)
  166. {
  167. _logger.LogError(ex, "Error killing process");
  168. }
  169. throw;
  170. }
  171. }
  172. }
  173. }
  174. }