FFmpegValidator.cs 4.5 KB

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