EncoderValidator.cs 5.8 KB

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