FFmpegValidator.cs 4.5 KB

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