FFMpegManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.MediaInfo;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Providers.MediaInfo;
  5. using MediaBrowser.Model.Entities;
  6. using System;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Controller.MediaInfo
  12. {
  13. /// <summary>
  14. /// Class FFMpegManager
  15. /// </summary>
  16. public class FFMpegManager
  17. {
  18. /// <summary>
  19. /// Gets or sets the video image cache.
  20. /// </summary>
  21. /// <value>The video image cache.</value>
  22. internal FileSystemRepository VideoImageCache { get; set; }
  23. /// <summary>
  24. /// Gets or sets the image cache.
  25. /// </summary>
  26. /// <value>The image cache.</value>
  27. internal FileSystemRepository AudioImageCache { get; set; }
  28. /// <summary>
  29. /// Gets or sets the subtitle cache.
  30. /// </summary>
  31. /// <value>The subtitle cache.</value>
  32. internal FileSystemRepository SubtitleCache { get; set; }
  33. /// <summary>
  34. /// The _logger
  35. /// </summary>
  36. private readonly Kernel _kernel;
  37. private readonly IServerApplicationPaths _appPaths;
  38. private readonly IMediaEncoder _encoder;
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="FFMpegManager" /> class.
  41. /// </summary>
  42. /// <param name="kernel">The kernel.</param>
  43. /// <param name="appPaths">The app paths.</param>
  44. /// <param name="encoder">The encoder.</param>
  45. /// <exception cref="System.ArgumentNullException">zipClient</exception>
  46. public FFMpegManager(Kernel kernel, IServerApplicationPaths appPaths, IMediaEncoder encoder)
  47. {
  48. if (kernel == null)
  49. {
  50. throw new ArgumentNullException("kernel");
  51. }
  52. _kernel = kernel;
  53. _appPaths = appPaths;
  54. _encoder = encoder;
  55. VideoImageCache = new FileSystemRepository(VideoImagesDataPath);
  56. AudioImageCache = new FileSystemRepository(AudioImagesDataPath);
  57. SubtitleCache = new FileSystemRepository(SubtitleCachePath);
  58. }
  59. /// <summary>
  60. /// The _video images data path
  61. /// </summary>
  62. private string _videoImagesDataPath;
  63. /// <summary>
  64. /// Gets the video images data path.
  65. /// </summary>
  66. /// <value>The video images data path.</value>
  67. public string VideoImagesDataPath
  68. {
  69. get
  70. {
  71. if (_videoImagesDataPath == null)
  72. {
  73. _videoImagesDataPath = Path.Combine(_appPaths.DataPath, "extracted-video-images");
  74. if (!Directory.Exists(_videoImagesDataPath))
  75. {
  76. Directory.CreateDirectory(_videoImagesDataPath);
  77. }
  78. }
  79. return _videoImagesDataPath;
  80. }
  81. }
  82. /// <summary>
  83. /// The _audio images data path
  84. /// </summary>
  85. private string _audioImagesDataPath;
  86. /// <summary>
  87. /// Gets the audio images data path.
  88. /// </summary>
  89. /// <value>The audio images data path.</value>
  90. public string AudioImagesDataPath
  91. {
  92. get
  93. {
  94. if (_audioImagesDataPath == null)
  95. {
  96. _audioImagesDataPath = Path.Combine(_appPaths.DataPath, "extracted-audio-images");
  97. if (!Directory.Exists(_audioImagesDataPath))
  98. {
  99. Directory.CreateDirectory(_audioImagesDataPath);
  100. }
  101. }
  102. return _audioImagesDataPath;
  103. }
  104. }
  105. /// <summary>
  106. /// The _subtitle cache path
  107. /// </summary>
  108. private string _subtitleCachePath;
  109. /// <summary>
  110. /// Gets the subtitle cache path.
  111. /// </summary>
  112. /// <value>The subtitle cache path.</value>
  113. public string SubtitleCachePath
  114. {
  115. get
  116. {
  117. if (_subtitleCachePath == null)
  118. {
  119. _subtitleCachePath = Path.Combine(_appPaths.CachePath, "subtitles");
  120. if (!Directory.Exists(_subtitleCachePath))
  121. {
  122. Directory.CreateDirectory(_subtitleCachePath);
  123. }
  124. }
  125. return _subtitleCachePath;
  126. }
  127. }
  128. /// <summary>
  129. /// The first chapter ticks
  130. /// </summary>
  131. private static readonly long FirstChapterTicks = TimeSpan.FromSeconds(15).Ticks;
  132. /// <summary>
  133. /// Extracts the chapter images.
  134. /// </summary>
  135. /// <param name="video">The video.</param>
  136. /// <param name="cancellationToken">The cancellation token.</param>
  137. /// <param name="extractImages">if set to <c>true</c> [extract images].</param>
  138. /// <param name="saveItem">if set to <c>true</c> [save item].</param>
  139. /// <returns>Task.</returns>
  140. /// <exception cref="System.ArgumentNullException"></exception>
  141. public async Task PopulateChapterImages(Video video, CancellationToken cancellationToken, bool extractImages, bool saveItem)
  142. {
  143. if (video.Chapters == null)
  144. {
  145. throw new ArgumentNullException();
  146. }
  147. // Can't extract images if there are no video streams
  148. if (video.MediaStreams == null || video.MediaStreams.All(m => m.Type != MediaStreamType.Video))
  149. {
  150. return;
  151. }
  152. var changesMade = false;
  153. foreach (var chapter in video.Chapters)
  154. {
  155. var filename = video.Id + "_" + video.DateModified.Ticks + "_" + chapter.StartPositionTicks;
  156. var path = VideoImageCache.GetResourcePath(filename, ".jpg");
  157. if (!VideoImageCache.ContainsFilePath(path))
  158. {
  159. if (extractImages)
  160. {
  161. // Disable for now on folder rips
  162. if (video.VideoType != VideoType.VideoFile)
  163. {
  164. continue;
  165. }
  166. // Add some time for the first chapter to make sure we don't end up with a black image
  167. var time = chapter.StartPositionTicks == 0 ? TimeSpan.FromTicks(Math.Min(FirstChapterTicks, video.RunTimeTicks ?? 0)) : TimeSpan.FromTicks(chapter.StartPositionTicks);
  168. InputType type;
  169. var inputPath = MediaEncoderHelpers.GetInputArgument(video, null, out type);
  170. try
  171. {
  172. await _encoder.ExtractImage(inputPath, type, time, path, cancellationToken).ConfigureAwait(false);
  173. chapter.ImagePath = path;
  174. changesMade = true;
  175. }
  176. catch
  177. {
  178. break;
  179. }
  180. }
  181. }
  182. else if (!string.Equals(path, chapter.ImagePath, StringComparison.OrdinalIgnoreCase))
  183. {
  184. chapter.ImagePath = path;
  185. changesMade = true;
  186. }
  187. }
  188. if (saveItem && changesMade)
  189. {
  190. await _kernel.ItemRepository.SaveItem(video, CancellationToken.None).ConfigureAwait(false);
  191. }
  192. }
  193. /// <summary>
  194. /// Gets the subtitle cache path.
  195. /// </summary>
  196. /// <param name="input">The input.</param>
  197. /// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
  198. /// <param name="outputExtension">The output extension.</param>
  199. /// <returns>System.String.</returns>
  200. public string GetSubtitleCachePath(Video input, int subtitleStreamIndex, string outputExtension)
  201. {
  202. return SubtitleCache.GetResourcePath(input.Id + "_" + subtitleStreamIndex + "_" + input.DateModified.Ticks, outputExtension);
  203. }
  204. }
  205. }