EncoderValidator.cs 4.2 KB

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