FFmpegValidator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "mpeg4",
  78. "msmpeg4",
  79. "libvpx",
  80. //"libvpx-vp9",
  81. "aac",
  82. "ac3",
  83. "libmp3lame",
  84. "libvorbis",
  85. "srt"
  86. };
  87. foreach (var encoder in required)
  88. {
  89. var srch = " " + encoder + " ";
  90. if (output.IndexOf(srch, StringComparison.OrdinalIgnoreCase) == -1)
  91. {
  92. _logger.Error("ffmpeg is missing encoder " + encoder);
  93. //throw new ArgumentException("ffmpeg is missing encoder " + encoder);
  94. }
  95. }
  96. }
  97. private string GetFFMpegOutput(string path, string arguments)
  98. {
  99. var process = new Process
  100. {
  101. StartInfo = new ProcessStartInfo
  102. {
  103. CreateNoWindow = true,
  104. UseShellExecute = false,
  105. FileName = path,
  106. Arguments = arguments,
  107. WindowStyle = ProcessWindowStyle.Hidden,
  108. ErrorDialog = false,
  109. RedirectStandardOutput = true,
  110. RedirectStandardError = true
  111. }
  112. };
  113. using (process)
  114. {
  115. process.Start();
  116. try
  117. {
  118. process.BeginErrorReadLine();
  119. using (var reader = new StreamReader(process.StandardOutput.BaseStream))
  120. {
  121. return reader.ReadToEnd();
  122. }
  123. }
  124. catch
  125. {
  126. // Hate having to do this
  127. try
  128. {
  129. process.Kill();
  130. }
  131. catch (Exception ex1)
  132. {
  133. _logger.ErrorException("Error killing ffmpeg", ex1);
  134. }
  135. throw;
  136. }
  137. }
  138. }
  139. }
  140. }