EncoderValidator.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using MediaBrowser.Model.Logging;
  5. namespace MediaBrowser.MediaEncoding.Encoder
  6. {
  7. public class EncoderValidator
  8. {
  9. private readonly ILogger _logger;
  10. public EncoderValidator(ILogger logger)
  11. {
  12. _logger = logger;
  13. }
  14. public Tuple<List<string>, List<string>> Validate(string encoderPath)
  15. {
  16. _logger.Info("Validating media encoder at {0}", encoderPath);
  17. var decoders = GetDecoders(encoderPath);
  18. var encoders = GetEncoders(encoderPath);
  19. _logger.Info("Encoder validation complete");
  20. return new Tuple<List<string>, List<string>>(decoders, encoders);
  21. }
  22. public bool ValidateVersion(string encoderAppPath)
  23. {
  24. string output = string.Empty;
  25. try
  26. {
  27. output = GetProcessOutput(encoderAppPath, "-version");
  28. }
  29. catch
  30. {
  31. }
  32. if (string.IsNullOrWhiteSpace(output))
  33. {
  34. return false;
  35. }
  36. if (output.IndexOf("Libav developers", StringComparison.OrdinalIgnoreCase) != -1)
  37. {
  38. return false;
  39. }
  40. return true;
  41. }
  42. private List<string> GetDecoders(string encoderAppPath)
  43. {
  44. string output = string.Empty;
  45. try
  46. {
  47. output = GetProcessOutput(encoderAppPath, "-decoders");
  48. }
  49. catch
  50. {
  51. }
  52. var found = new List<string>();
  53. var required = new[]
  54. {
  55. "h264_qsv",
  56. "mpeg2_qsv",
  57. "vc1_qsv"
  58. };
  59. foreach (var codec in required)
  60. {
  61. var srch = " " + codec + " ";
  62. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  63. {
  64. _logger.Info("Decoder available: " + codec);
  65. found.Add(codec);
  66. }
  67. }
  68. return found;
  69. }
  70. private List<string> GetEncoders(string encoderAppPath)
  71. {
  72. string output = null;
  73. try
  74. {
  75. output = GetProcessOutput(encoderAppPath, "-encoders");
  76. }
  77. catch
  78. {
  79. }
  80. var found = new List<string>();
  81. var required = new[]
  82. {
  83. "libx264",
  84. "libx265",
  85. "mpeg4",
  86. "msmpeg4",
  87. "libvpx",
  88. "libvpx-vp9",
  89. "aac",
  90. "libmp3lame",
  91. "libopus",
  92. "libvorbis",
  93. "srt",
  94. "h264_nvenc",
  95. "h264_qsv",
  96. "h264_omx",
  97. "h264_vaapi",
  98. "ac3"
  99. };
  100. output = output ?? string.Empty;
  101. var index = 0;
  102. foreach (var codec in required)
  103. {
  104. var srch = " " + codec + " ";
  105. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  106. {
  107. if (index < required.Length - 1)
  108. {
  109. _logger.Info("Encoder available: " + codec);
  110. }
  111. found.Add(codec);
  112. }
  113. index++;
  114. }
  115. return found;
  116. }
  117. private string GetProcessOutput(string path, string arguments)
  118. {
  119. var process = new Process
  120. {
  121. StartInfo = new ProcessStartInfo
  122. {
  123. CreateNoWindow = true,
  124. UseShellExecute = false,
  125. FileName = path,
  126. Arguments = arguments,
  127. WindowStyle = ProcessWindowStyle.Hidden,
  128. ErrorDialog = false,
  129. RedirectStandardOutput = true
  130. }
  131. };
  132. _logger.Info("Running {0} {1}", path, arguments);
  133. using (process)
  134. {
  135. process.Start();
  136. try
  137. {
  138. return process.StandardOutput.ReadToEnd();
  139. }
  140. catch
  141. {
  142. _logger.Info("Killing process {0} {1}", path, arguments);
  143. // Hate having to do this
  144. try
  145. {
  146. process.Kill();
  147. }
  148. catch (Exception ex1)
  149. {
  150. _logger.ErrorException("Error killing process", ex1);
  151. }
  152. throw;
  153. }
  154. }
  155. }
  156. }
  157. }