EncoderValidator.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 ffmpegPath)
  24. {
  25. string output = string.Empty;
  26. try
  27. {
  28. output = GetFFMpegOutput(ffmpegPath, "-decoders");
  29. }
  30. catch
  31. {
  32. }
  33. //_logger.Debug("ffmpeg decoder query result: {0}", output ?? string.Empty);
  34. var found = new List<string>();
  35. var required = new[]
  36. {
  37. "h264_qsv",
  38. "mpeg2_qsv",
  39. "vc1_qsv"
  40. };
  41. foreach (var codec in required)
  42. {
  43. var srch = " " + codec + " ";
  44. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
  45. {
  46. _logger.Warn("ffmpeg is missing decoder " + codec);
  47. }
  48. else
  49. {
  50. found.Add(codec);
  51. }
  52. }
  53. return found;
  54. }
  55. private List<string> GetEncoders(string ffmpegPath)
  56. {
  57. string output = null;
  58. try
  59. {
  60. output = GetFFMpegOutput(ffmpegPath, "-encoders");
  61. }
  62. catch
  63. {
  64. }
  65. //_logger.Debug("ffmpeg encoder query result: {0}", output ?? string.Empty);
  66. var found = new List<string>();
  67. var required = new[]
  68. {
  69. "libx264",
  70. "libx265",
  71. "mpeg4",
  72. "msmpeg4",
  73. //"libvpx",
  74. //"libvpx-vp9",
  75. "aac",
  76. "libmp3lame",
  77. "libopus",
  78. //"libvorbis",
  79. "srt"
  80. };
  81. foreach (var codec in required)
  82. {
  83. var srch = " " + codec + " ";
  84. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
  85. {
  86. _logger.Warn("ffmpeg is missing encoder " + codec);
  87. }
  88. else
  89. {
  90. found.Add(codec);
  91. }
  92. }
  93. return found;
  94. }
  95. private string GetFFMpegOutput(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. RedirectStandardError = true
  109. }
  110. };
  111. using (process)
  112. {
  113. process.Start();
  114. try
  115. {
  116. process.BeginErrorReadLine();
  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 ffmpeg", ex1);
  130. }
  131. throw;
  132. }
  133. }
  134. }
  135. }
  136. }