ImageEncoder.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.MediaEncoding;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.MediaEncoding.Encoder
  14. {
  15. public class ImageEncoder
  16. {
  17. private readonly string _ffmpegPath;
  18. private readonly ILogger _logger;
  19. private readonly IFileSystem _fileSystem;
  20. private readonly IApplicationPaths _appPaths;
  21. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  22. private static readonly SemaphoreSlim ResourcePool = new SemaphoreSlim(10, 10);
  23. public ImageEncoder(string ffmpegPath, ILogger logger, IFileSystem fileSystem, IApplicationPaths appPaths)
  24. {
  25. _ffmpegPath = ffmpegPath;
  26. _logger = logger;
  27. _fileSystem = fileSystem;
  28. _appPaths = appPaths;
  29. }
  30. public async Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
  31. {
  32. ValidateInput(options);
  33. await ResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  34. try
  35. {
  36. return await EncodeImageInternal(options, cancellationToken).ConfigureAwait(false);
  37. }
  38. finally
  39. {
  40. ResourcePool.Release();
  41. }
  42. }
  43. private async Task<Stream> EncodeImageInternal(ImageEncodingOptions options, CancellationToken cancellationToken)
  44. {
  45. ValidateInput(options);
  46. var inputPath = options.InputPath;
  47. var filename = Path.GetFileName(inputPath);
  48. if (HasDiacritics(filename))
  49. {
  50. inputPath = GetTempFile(inputPath);
  51. filename = Path.GetFileName(inputPath);
  52. }
  53. var process = new Process
  54. {
  55. StartInfo = new ProcessStartInfo
  56. {
  57. CreateNoWindow = true,
  58. UseShellExecute = false,
  59. FileName = _ffmpegPath,
  60. Arguments = GetArguments(options, filename),
  61. WindowStyle = ProcessWindowStyle.Hidden,
  62. ErrorDialog = false,
  63. RedirectStandardOutput = true,
  64. RedirectStandardError = true,
  65. WorkingDirectory = Path.GetDirectoryName(inputPath)
  66. }
  67. };
  68. _logger.Debug("ffmpeg " + process.StartInfo.Arguments);
  69. process.Start();
  70. var memoryStream = new MemoryStream();
  71. #pragma warning disable 4014
  72. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  73. process.StandardOutput.BaseStream.CopyToAsync(memoryStream);
  74. #pragma warning restore 4014
  75. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  76. process.BeginErrorReadLine();
  77. var ranToCompletion = process.WaitForExit(5000);
  78. if (!ranToCompletion)
  79. {
  80. try
  81. {
  82. _logger.Info("Killing ffmpeg process");
  83. process.Kill();
  84. process.WaitForExit(1000);
  85. }
  86. catch (Exception ex)
  87. {
  88. _logger.ErrorException("Error killing process", ex);
  89. }
  90. }
  91. var exitCode = ranToCompletion ? process.ExitCode : -1;
  92. process.Dispose();
  93. if (exitCode == -1 || memoryStream.Length == 0)
  94. {
  95. memoryStream.Dispose();
  96. var msg = string.Format("ffmpeg image encoding failed for {0}", options.InputPath);
  97. _logger.Error(msg);
  98. throw new ApplicationException(msg);
  99. }
  100. memoryStream.Position = 0;
  101. return memoryStream;
  102. }
  103. private string GetTempFile(string path)
  104. {
  105. var extension = Path.GetExtension(path) ?? string.Empty;
  106. var tempPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N") + extension);
  107. File.Copy(path, tempPath);
  108. return tempPath;
  109. }
  110. private string GetArguments(ImageEncodingOptions options, string inputFilename)
  111. {
  112. var vfScale = GetFilterGraph(options);
  113. var outputFormat = GetOutputFormat(options.Format);
  114. var quality = (options.Quality ?? 100) * .3;
  115. quality = 31 - quality;
  116. var qualityValue = Convert.ToInt32(Math.Max(quality, 1));
  117. return string.Format("-f image2 -i file:\"{3}\" -q:v {0} {1} -f image2pipe -vcodec {2} -",
  118. qualityValue.ToString(_usCulture),
  119. vfScale,
  120. outputFormat,
  121. inputFilename);
  122. }
  123. private string GetFilterGraph(ImageEncodingOptions options)
  124. {
  125. if (!options.Width.HasValue &&
  126. !options.Height.HasValue &&
  127. !options.MaxHeight.HasValue &&
  128. !options.MaxWidth.HasValue)
  129. {
  130. return string.Empty;
  131. }
  132. var widthScale = "-1";
  133. var heightScale = "-1";
  134. if (options.MaxWidth.HasValue)
  135. {
  136. widthScale = "min(iw\\," + options.MaxWidth.Value.ToString(_usCulture) + ")";
  137. }
  138. else if (options.Width.HasValue)
  139. {
  140. widthScale = options.Width.Value.ToString(_usCulture);
  141. }
  142. if (options.MaxHeight.HasValue)
  143. {
  144. heightScale = "min(ih\\," + options.MaxHeight.Value.ToString(_usCulture) + ")";
  145. }
  146. else if (options.Height.HasValue)
  147. {
  148. heightScale = options.Height.Value.ToString(_usCulture);
  149. }
  150. var scaleMethod = "lanczos";
  151. return string.Format("-vf scale=\"{0}:{1}\"",
  152. widthScale,
  153. heightScale);
  154. }
  155. private string GetOutputFormat(string format)
  156. {
  157. if (string.Equals(format, "jpeg", StringComparison.OrdinalIgnoreCase) ||
  158. string.Equals(format, "jpg", StringComparison.OrdinalIgnoreCase))
  159. {
  160. return "mjpeg";
  161. }
  162. return format;
  163. }
  164. private void ValidateInput(ImageEncodingOptions options)
  165. {
  166. }
  167. /// <summary>
  168. /// Determines whether the specified text has diacritics.
  169. /// </summary>
  170. /// <param name="text">The text.</param>
  171. /// <returns><c>true</c> if the specified text has diacritics; otherwise, <c>false</c>.</returns>
  172. private bool HasDiacritics(string text)
  173. {
  174. return !String.Equals(text, RemoveDiacritics(text), StringComparison.Ordinal);
  175. }
  176. /// <summary>
  177. /// Removes the diacritics.
  178. /// </summary>
  179. /// <param name="text">The text.</param>
  180. /// <returns>System.String.</returns>
  181. private string RemoveDiacritics(string text)
  182. {
  183. return String.Concat(
  184. text.Normalize(NormalizationForm.FormD)
  185. .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
  186. UnicodeCategory.NonSpacingMark)
  187. ).Normalize(NormalizationForm.FormC);
  188. }
  189. }
  190. }