FFmpegValidator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using CommonIO;
  8. namespace MediaBrowser.Server.Startup.Common.FFMpeg
  9. {
  10. public class FFmpegValidator
  11. {
  12. private readonly ILogger _logger;
  13. private readonly IApplicationPaths _appPaths;
  14. private readonly IFileSystem _fileSystem;
  15. public FFmpegValidator(ILogger logger, IApplicationPaths appPaths, IFileSystem fileSystem)
  16. {
  17. _logger = logger;
  18. _appPaths = appPaths;
  19. _fileSystem = fileSystem;
  20. }
  21. public Tuple<List<string>,List<string>> Validate(string encoderPath)
  22. {
  23. var decoders = GetDecoders(encoderPath);
  24. var encoders = GetEncoders(encoderPath);
  25. return new Tuple<List<string>, List<string>>(decoders, encoders);
  26. }
  27. private List<string> GetDecoders(string ffmpegPath)
  28. {
  29. string output = string.Empty;
  30. try
  31. {
  32. output = GetFFMpegOutput(ffmpegPath, "-decoders");
  33. }
  34. catch
  35. {
  36. }
  37. //_logger.Debug("ffmpeg decoder query result: {0}", output ?? string.Empty);
  38. var found = new List<string>();
  39. var required = new[]
  40. {
  41. "h264_qsv",
  42. "mpeg2_qsv",
  43. "vc1_qsv"
  44. };
  45. foreach (var codec in required)
  46. {
  47. var srch = " " + codec + " ";
  48. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
  49. {
  50. _logger.Warn("ffmpeg is missing decoder " + codec);
  51. }
  52. else
  53. {
  54. found.Add(codec);
  55. }
  56. }
  57. return found;
  58. }
  59. private List<string> GetEncoders(string ffmpegPath)
  60. {
  61. string output = null;
  62. try
  63. {
  64. output = GetFFMpegOutput(ffmpegPath, "-encoders");
  65. }
  66. catch
  67. {
  68. }
  69. //_logger.Debug("ffmpeg encoder query result: {0}", output ?? string.Empty);
  70. var found = new List<string>();
  71. var required = new[]
  72. {
  73. "libx264",
  74. "libx265",
  75. "mpeg4",
  76. "msmpeg4",
  77. //"libvpx",
  78. //"libvpx-vp9",
  79. "aac",
  80. "libmp3lame",
  81. "libopus",
  82. //"libvorbis",
  83. "srt"
  84. };
  85. foreach (var codec in required)
  86. {
  87. var srch = " " + codec + " ";
  88. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
  89. {
  90. _logger.Warn("ffmpeg is missing encoder " + codec);
  91. }
  92. else
  93. {
  94. found.Add(codec);
  95. }
  96. }
  97. return found;
  98. }
  99. private string GetFFMpegOutput(string path, string arguments)
  100. {
  101. var process = new Process
  102. {
  103. StartInfo = new ProcessStartInfo
  104. {
  105. CreateNoWindow = true,
  106. UseShellExecute = false,
  107. FileName = path,
  108. Arguments = arguments,
  109. WindowStyle = ProcessWindowStyle.Hidden,
  110. ErrorDialog = false,
  111. RedirectStandardOutput = true,
  112. RedirectStandardError = true
  113. }
  114. };
  115. using (process)
  116. {
  117. process.Start();
  118. try
  119. {
  120. process.BeginErrorReadLine();
  121. using (var reader = new StreamReader(process.StandardOutput.BaseStream))
  122. {
  123. return reader.ReadToEnd();
  124. }
  125. }
  126. catch
  127. {
  128. // Hate having to do this
  129. try
  130. {
  131. process.Kill();
  132. }
  133. catch (Exception ex1)
  134. {
  135. _logger.ErrorException("Error killing ffmpeg", ex1);
  136. }
  137. throw;
  138. }
  139. }
  140. }
  141. }
  142. }