FFmpegValidator.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. namespace MediaBrowser.Server.Startup.Common.FFMpeg
  10. {
  11. public class FFmpegValidator
  12. {
  13. private readonly ILogger _logger;
  14. private readonly IApplicationPaths _appPaths;
  15. public FFmpegValidator(ILogger logger, IApplicationPaths appPaths)
  16. {
  17. _logger = logger;
  18. _appPaths = appPaths;
  19. }
  20. public void Validate(FFMpegInfo info)
  21. {
  22. _logger.Info("FFMpeg: {0}", info.EncoderPath);
  23. _logger.Info("FFProbe: {0}", info.ProbePath);
  24. string cacheKey;
  25. try
  26. {
  27. cacheKey = new FileInfo(info.EncoderPath).Length.ToString(CultureInfo.InvariantCulture).GetMD5().ToString("N");
  28. }
  29. catch (IOException)
  30. {
  31. // This could happen if ffmpeg is coming from a Path variable and we don't have the full path
  32. cacheKey = Guid.NewGuid().ToString("N");
  33. }
  34. catch
  35. {
  36. cacheKey = Guid.NewGuid().ToString("N");
  37. }
  38. var cachePath = Path.Combine(_appPaths.CachePath, "1" + cacheKey);
  39. ValidateCodecs(info.EncoderPath, cachePath);
  40. }
  41. private void ValidateCodecs(string ffmpegPath, string cachePath)
  42. {
  43. string output = null;
  44. try
  45. {
  46. output = File.ReadAllText(cachePath, Encoding.UTF8);
  47. }
  48. catch
  49. {
  50. }
  51. if (string.IsNullOrWhiteSpace(output))
  52. {
  53. try
  54. {
  55. output = GetFFMpegOutput(ffmpegPath, "-encoders");
  56. }
  57. catch
  58. {
  59. return;
  60. }
  61. try
  62. {
  63. Directory.CreateDirectory(Path.GetDirectoryName(cachePath));
  64. File.WriteAllText(cachePath, output, Encoding.UTF8);
  65. }
  66. catch
  67. {
  68. }
  69. }
  70. ValidateCodecsFromOutput(output);
  71. }
  72. private void ValidateCodecsFromOutput(string output)
  73. {
  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 encoder in required)
  89. {
  90. var srch = " " + encoder + " ";
  91. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
  92. {
  93. _logger.Error("ffmpeg is missing encoder " + encoder);
  94. //throw new ArgumentException("ffmpeg is missing encoder " + encoder);
  95. }
  96. }
  97. }
  98. private string GetFFMpegOutput(string path, string arguments)
  99. {
  100. var process = new Process
  101. {
  102. StartInfo = new ProcessStartInfo
  103. {
  104. CreateNoWindow = true,
  105. UseShellExecute = false,
  106. FileName = path,
  107. Arguments = arguments,
  108. WindowStyle = ProcessWindowStyle.Hidden,
  109. ErrorDialog = false,
  110. RedirectStandardOutput = true,
  111. RedirectStandardError = true
  112. }
  113. };
  114. using (process)
  115. {
  116. process.Start();
  117. try
  118. {
  119. process.BeginErrorReadLine();
  120. using (var reader = new StreamReader(process.StandardOutput.BaseStream))
  121. {
  122. return reader.ReadToEnd();
  123. }
  124. }
  125. catch
  126. {
  127. // Hate having to do this
  128. try
  129. {
  130. process.Kill();
  131. }
  132. catch (Exception ex1)
  133. {
  134. _logger.ErrorException("Error killing ffmpeg", ex1);
  135. }
  136. throw;
  137. }
  138. }
  139. }
  140. }
  141. }