EncoderValidator.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. _logger.Info("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 List<string> GetDecoders(string encoderAppPath)
  61. {
  62. string output = string.Empty;
  63. try
  64. {
  65. output = GetProcessOutput(encoderAppPath, "-decoders");
  66. }
  67. catch (Exception )
  68. {
  69. //_logger.ErrorException("Error detecting available decoders", ex);
  70. }
  71. var found = new List<string>();
  72. var required = new[]
  73. {
  74. "mpeg2video",
  75. "h264_qsv",
  76. "hevc_qsv",
  77. "mpeg2_qsv",
  78. "vc1_qsv",
  79. "h264_cuvid",
  80. "hevc_cuvid",
  81. "dts",
  82. "ac3",
  83. "aac",
  84. "mp3",
  85. "h264",
  86. "hevc"
  87. };
  88. foreach (var codec in required)
  89. {
  90. var srch = " " + codec + " ";
  91. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  92. {
  93. _logger.Info("Decoder available: " + codec);
  94. found.Add(codec);
  95. }
  96. }
  97. return found;
  98. }
  99. private List<string> GetEncoders(string encoderAppPath)
  100. {
  101. string output = null;
  102. try
  103. {
  104. output = GetProcessOutput(encoderAppPath, "-encoders");
  105. }
  106. catch
  107. {
  108. }
  109. var found = new List<string>();
  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. output = output ?? string.Empty;
  134. var index = 0;
  135. foreach (var codec in required)
  136. {
  137. var srch = " " + codec + " ";
  138. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  139. {
  140. if (index < required.Length - 1)
  141. {
  142. _logger.Info("Encoder available: " + codec);
  143. }
  144. found.Add(codec);
  145. }
  146. index++;
  147. }
  148. return found;
  149. }
  150. private string GetProcessOutput(string path, string arguments)
  151. {
  152. var process = _processFactory.Create(new ProcessOptions
  153. {
  154. CreateNoWindow = true,
  155. UseShellExecute = false,
  156. FileName = path,
  157. Arguments = arguments,
  158. IsHidden = true,
  159. ErrorDialog = false,
  160. RedirectStandardOutput = true
  161. });
  162. _logger.Info("Running {0} {1}", path, arguments);
  163. using (process)
  164. {
  165. process.Start();
  166. try
  167. {
  168. return process.StandardOutput.ReadToEnd();
  169. }
  170. catch
  171. {
  172. _logger.Info("Killing process {0} {1}", path, arguments);
  173. // Hate having to do this
  174. try
  175. {
  176. process.Kill();
  177. }
  178. catch (Exception ex1)
  179. {
  180. _logger.ErrorException("Error killing process", ex1);
  181. }
  182. throw;
  183. }
  184. }
  185. }
  186. }
  187. }