ImageEncoder.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.MediaEncoding;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.MediaEncoding.Encoder
  11. {
  12. public class ImageEncoder
  13. {
  14. private readonly string _ffmpegPath;
  15. private readonly ILogger _logger;
  16. private readonly IFileSystem _fileSystem;
  17. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  18. private static readonly SemaphoreSlim ResourcePool = new SemaphoreSlim(10, 10);
  19. public ImageEncoder(string ffmpegPath, ILogger logger, IFileSystem fileSystem)
  20. {
  21. _ffmpegPath = ffmpegPath;
  22. _logger = logger;
  23. _fileSystem = fileSystem;
  24. }
  25. public async Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
  26. {
  27. ValidateInput(options);
  28. await ResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  29. try
  30. {
  31. return await EncodeImageInternal(options, cancellationToken).ConfigureAwait(false);
  32. }
  33. finally
  34. {
  35. ResourcePool.Release();
  36. }
  37. }
  38. private async Task<Stream> EncodeImageInternal(ImageEncodingOptions options, CancellationToken cancellationToken)
  39. {
  40. ValidateInput(options);
  41. var process = new Process
  42. {
  43. StartInfo = new ProcessStartInfo
  44. {
  45. CreateNoWindow = true,
  46. UseShellExecute = false,
  47. FileName = _ffmpegPath,
  48. Arguments = GetArguments(options),
  49. WindowStyle = ProcessWindowStyle.Hidden,
  50. ErrorDialog = false,
  51. RedirectStandardOutput = true,
  52. RedirectStandardError = true,
  53. WorkingDirectory = Path.GetDirectoryName(options.InputPath)
  54. }
  55. };
  56. _logger.Debug("ffmpeg " + process.StartInfo.Arguments);
  57. process.Start();
  58. var memoryStream = new MemoryStream();
  59. #pragma warning disable 4014
  60. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  61. process.StandardOutput.BaseStream.CopyToAsync(memoryStream);
  62. #pragma warning restore 4014
  63. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  64. process.BeginErrorReadLine();
  65. var ranToCompletion = process.WaitForExit(5000);
  66. if (!ranToCompletion)
  67. {
  68. try
  69. {
  70. _logger.Info("Killing ffmpeg process");
  71. process.Kill();
  72. process.WaitForExit(1000);
  73. }
  74. catch (Exception ex)
  75. {
  76. _logger.ErrorException("Error killing process", ex);
  77. }
  78. }
  79. var exitCode = ranToCompletion ? process.ExitCode : -1;
  80. process.Dispose();
  81. if (exitCode == -1 || memoryStream.Length == 0)
  82. {
  83. memoryStream.Dispose();
  84. var msg = string.Format("ffmpeg image encoding failed for {0}", options.InputPath);
  85. _logger.Error(msg);
  86. throw new ApplicationException(msg);
  87. }
  88. memoryStream.Position = 0;
  89. return memoryStream;
  90. }
  91. private string GetArguments(ImageEncodingOptions options)
  92. {
  93. var vfScale = GetFilterGraph(options);
  94. var outputFormat = GetOutputFormat(options.Format);
  95. var quality = (options.Quality ?? 100) * .3;
  96. quality = 31 - quality;
  97. var qualityValue = Convert.ToInt32(Math.Max(quality, 1));
  98. return string.Format("-f image2 -i file:\"{3}\" -q:v {0} {1} -f image2pipe -vcodec {2} -",
  99. qualityValue.ToString(_usCulture),
  100. vfScale,
  101. outputFormat,
  102. Path.GetFileName(options.InputPath));
  103. }
  104. private string GetFilterGraph(ImageEncodingOptions options)
  105. {
  106. if (!options.Width.HasValue &&
  107. !options.Height.HasValue &&
  108. !options.MaxHeight.HasValue &&
  109. !options.MaxWidth.HasValue)
  110. {
  111. return string.Empty;
  112. }
  113. var widthScale = "-1";
  114. var heightScale = "-1";
  115. if (options.MaxWidth.HasValue)
  116. {
  117. widthScale = "min(iw\\," + options.MaxWidth.Value.ToString(_usCulture) + ")";
  118. }
  119. else if (options.Width.HasValue)
  120. {
  121. widthScale = options.Width.Value.ToString(_usCulture);
  122. }
  123. if (options.MaxHeight.HasValue)
  124. {
  125. heightScale = "min(ih\\," + options.MaxHeight.Value.ToString(_usCulture) + ")";
  126. }
  127. else if (options.Height.HasValue)
  128. {
  129. heightScale = options.Height.Value.ToString(_usCulture);
  130. }
  131. var scaleMethod = "lanczos";
  132. return string.Format("-vf scale=\"{0}:{1}\" -sws_flags {2}",
  133. widthScale,
  134. heightScale,
  135. scaleMethod);
  136. }
  137. private string GetOutputFormat(string format)
  138. {
  139. if (string.Equals(format, "jpeg", StringComparison.OrdinalIgnoreCase) ||
  140. string.Equals(format, "jpg", StringComparison.OrdinalIgnoreCase))
  141. {
  142. return "mjpeg";
  143. }
  144. return format;
  145. }
  146. private void ValidateInput(ImageEncodingOptions options)
  147. {
  148. }
  149. }
  150. }