FFMpegManager.cs 8.1 KB

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