EncoderValidator.cs 4.1 KB

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