FFMpegManager.cs 8.3 KB

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