EncoderValidator.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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
  34. {
  35. }
  36. output = output ?? string.Empty;
  37. if (logOutput)
  38. {
  39. _logger.Info("ffmpeg info: {0}", output);
  40. }
  41. if (string.IsNullOrWhiteSpace(output))
  42. {
  43. return false;
  44. }
  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
  68. {
  69. }
  70. var found = new List<string>();
  71. var required = new[]
  72. {
  73. "h264_qsv",
  74. "mpeg2_qsv",
  75. "vc1_qsv"
  76. };
  77. foreach (var codec in required)
  78. {
  79. var srch = " " + codec + " ";
  80. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  81. {
  82. _logger.Info("Decoder available: " + codec);
  83. found.Add(codec);
  84. }
  85. }
  86. return found;
  87. }
  88. private List<string> GetEncoders(string encoderAppPath)
  89. {
  90. string output = null;
  91. try
  92. {
  93. output = GetProcessOutput(encoderAppPath, "-encoders");
  94. }
  95. catch
  96. {
  97. }
  98. var found = new List<string>();
  99. var required = new[]
  100. {
  101. "libx264",
  102. "libx265",
  103. "mpeg4",
  104. "msmpeg4",
  105. "libvpx",
  106. "libvpx-vp9",
  107. "aac",
  108. "libmp3lame",
  109. "libopus",
  110. "libvorbis",
  111. "srt",
  112. "h264_nvenc",
  113. "h264_qsv",
  114. "h264_omx",
  115. "h264_vaapi",
  116. "ac3"
  117. };
  118. output = output ?? string.Empty;
  119. var index = 0;
  120. foreach (var codec in required)
  121. {
  122. var srch = " " + codec + " ";
  123. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  124. {
  125. if (index < required.Length - 1)
  126. {
  127. _logger.Info("Encoder available: " + codec);
  128. }
  129. found.Add(codec);
  130. }
  131. index++;
  132. }
  133. return found;
  134. }
  135. private string GetProcessOutput(string path, string arguments)
  136. {
  137. var process = _processFactory.Create(new ProcessOptions
  138. {
  139. CreateNoWindow = true,
  140. UseShellExecute = false,
  141. FileName = path,
  142. Arguments = arguments,
  143. IsHidden = true,
  144. ErrorDialog = false,
  145. RedirectStandardOutput = true
  146. });
  147. _logger.Info("Running {0} {1}", path, arguments);
  148. using (process)
  149. {
  150. process.Start();
  151. try
  152. {
  153. return process.StandardOutput.ReadToEnd();
  154. }
  155. catch
  156. {
  157. _logger.Info("Killing process {0} {1}", path, arguments);
  158. // Hate having to do this
  159. try
  160. {
  161. process.Kill();
  162. }
  163. catch (Exception ex1)
  164. {
  165. _logger.ErrorException("Error killing process", ex1);
  166. }
  167. throw;
  168. }
  169. }
  170. }
  171. }
  172. }