EncoderValidator.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "ac3"
  78. };
  79. output = output ?? string.Empty;
  80. var index = 0;
  81. foreach (var codec in required)
  82. {
  83. var srch = " " + codec + " ";
  84. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  85. {
  86. if (index < required.Length - 1)
  87. {
  88. _logger.Info("Encoder available: " + codec);
  89. }
  90. found.Add(codec);
  91. }
  92. index++;
  93. }
  94. return found;
  95. }
  96. private string GetProcessOutput(string path, string arguments)
  97. {
  98. var process = new Process
  99. {
  100. StartInfo = new ProcessStartInfo
  101. {
  102. CreateNoWindow = true,
  103. UseShellExecute = false,
  104. FileName = path,
  105. Arguments = arguments,
  106. WindowStyle = ProcessWindowStyle.Hidden,
  107. ErrorDialog = false,
  108. RedirectStandardOutput = true
  109. }
  110. };
  111. using (process)
  112. {
  113. process.Start();
  114. try
  115. {
  116. return process.StandardOutput.ReadToEnd();
  117. }
  118. catch
  119. {
  120. _logger.Info("Killing process {0} {1}", path, arguments);
  121. // Hate having to do this
  122. try
  123. {
  124. process.Kill();
  125. }
  126. catch (Exception ex1)
  127. {
  128. _logger.ErrorException("Error killing process", ex1);
  129. }
  130. throw;
  131. }
  132. }
  133. }
  134. }
  135. }