EncoderValidator.cs 4.0 KB

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