EncoderValidator.cs 5.0 KB

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