EncoderValidator.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, bool logOutput)
  23. {
  24. string output = string.Empty;
  25. try
  26. {
  27. output = GetProcessOutput(encoderAppPath, "-version");
  28. }
  29. catch
  30. {
  31. }
  32. if (logOutput)
  33. {
  34. _logger.Info("ffmpeg info: {0}", output ?? string.Empty);
  35. }
  36. if (string.IsNullOrWhiteSpace(output))
  37. {
  38. return false;
  39. }
  40. if (output.IndexOf("Libav developers", StringComparison.OrdinalIgnoreCase) != -1)
  41. {
  42. return false;
  43. }
  44. return true;
  45. }
  46. private List<string> GetDecoders(string encoderAppPath)
  47. {
  48. string output = string.Empty;
  49. try
  50. {
  51. output = GetProcessOutput(encoderAppPath, "-decoders");
  52. }
  53. catch
  54. {
  55. }
  56. var found = new List<string>();
  57. var required = new[]
  58. {
  59. "h264_qsv",
  60. "mpeg2_qsv",
  61. "vc1_qsv"
  62. };
  63. foreach (var codec in required)
  64. {
  65. var srch = " " + codec + " ";
  66. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  67. {
  68. _logger.Info("Decoder available: " + codec);
  69. found.Add(codec);
  70. }
  71. }
  72. return found;
  73. }
  74. private List<string> GetEncoders(string encoderAppPath)
  75. {
  76. string output = null;
  77. try
  78. {
  79. output = GetProcessOutput(encoderAppPath, "-encoders");
  80. }
  81. catch
  82. {
  83. }
  84. var found = new List<string>();
  85. var required = new[]
  86. {
  87. "libx264",
  88. "libx265",
  89. "mpeg4",
  90. "msmpeg4",
  91. "libvpx",
  92. "libvpx-vp9",
  93. "aac",
  94. "libmp3lame",
  95. "libopus",
  96. "libvorbis",
  97. "srt",
  98. "h264_nvenc",
  99. "h264_qsv",
  100. "h264_omx",
  101. "h264_vaapi",
  102. "ac3"
  103. };
  104. output = output ?? string.Empty;
  105. var index = 0;
  106. foreach (var codec in required)
  107. {
  108. var srch = " " + codec + " ";
  109. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  110. {
  111. if (index < required.Length - 1)
  112. {
  113. _logger.Info("Encoder available: " + codec);
  114. }
  115. found.Add(codec);
  116. }
  117. index++;
  118. }
  119. return found;
  120. }
  121. private string GetProcessOutput(string path, string arguments)
  122. {
  123. var process = new Process
  124. {
  125. StartInfo = new ProcessStartInfo
  126. {
  127. CreateNoWindow = true,
  128. UseShellExecute = false,
  129. FileName = path,
  130. Arguments = arguments,
  131. WindowStyle = ProcessWindowStyle.Hidden,
  132. ErrorDialog = false,
  133. RedirectStandardOutput = true
  134. }
  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. }