EncoderValidator.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using MediaBrowser.Model.Logging;
  6. namespace MediaBrowser.MediaEncoding.Encoder
  7. {
  8. public class EncoderValidator
  9. {
  10. private readonly ILogger _logger;
  11. public EncoderValidator(ILogger logger)
  12. {
  13. _logger = logger;
  14. }
  15. public Tuple<List<string>, List<string>> Validate(string encoderPath)
  16. {
  17. _logger.Info("Validating media encoder at {0}", encoderPath);
  18. var decoders = GetDecoders(encoderPath);
  19. var encoders = GetEncoders(encoderPath);
  20. _logger.Info("Encoder validation complete");
  21. return new Tuple<List<string>, List<string>>(decoders, encoders);
  22. }
  23. private List<string> GetDecoders(string encoderAppPath)
  24. {
  25. string output = string.Empty;
  26. try
  27. {
  28. output = GetProcessOutput(encoderAppPath, "-decoders");
  29. }
  30. catch
  31. {
  32. }
  33. var found = new List<string>();
  34. var required = new[]
  35. {
  36. "h264_qsv",
  37. "mpeg2_qsv",
  38. "vc1_qsv"
  39. };
  40. foreach (var codec in required)
  41. {
  42. var srch = " " + codec + " ";
  43. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) != -1)
  44. {
  45. _logger.Info("Decoder available: " + codec);
  46. found.Add(codec);
  47. }
  48. }
  49. return found;
  50. }
  51. private List<string> GetEncoders(string encoderAppPath)
  52. {
  53. string output = null;
  54. try
  55. {
  56. output = GetProcessOutput(encoderAppPath, "-encoders");
  57. }
  58. catch
  59. {
  60. }
  61. var found = new List<string>();
  62. var required = new[]
  63. {
  64. "libx264",
  65. "libx265",
  66. "mpeg4",
  67. "msmpeg4",
  68. "libvpx",
  69. "libvpx-vp9",
  70. "aac",
  71. "libmp3lame",
  72. "libopus",
  73. "libvorbis",
  74. "srt",
  75. "h264_nvenc",
  76. "h264_qsv",
  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. }