VideoImageProvider.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.MediaEncoding;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Drawing;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.IO;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.MediaInfo;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using CommonIO;
  17. namespace MediaBrowser.Providers.MediaInfo
  18. {
  19. public class VideoImageProvider : IDynamicImageProvider, IHasItemChangeMonitor, IHasOrder
  20. {
  21. private readonly IIsoManager _isoManager;
  22. private readonly IMediaEncoder _mediaEncoder;
  23. private readonly IServerConfigurationManager _config;
  24. private readonly ILibraryManager _libraryManager;
  25. private readonly ILogger _logger;
  26. private readonly IFileSystem _fileSystem;
  27. public VideoImageProvider(IIsoManager isoManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem)
  28. {
  29. _isoManager = isoManager;
  30. _mediaEncoder = mediaEncoder;
  31. _config = config;
  32. _libraryManager = libraryManager;
  33. _logger = logger;
  34. _fileSystem = fileSystem;
  35. }
  36. /// <summary>
  37. /// The null mount task result
  38. /// </summary>
  39. protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
  40. /// <summary>
  41. /// Mounts the iso if needed.
  42. /// </summary>
  43. /// <param name="item">The item.</param>
  44. /// <param name="cancellationToken">The cancellation token.</param>
  45. /// <returns>Task{IIsoMount}.</returns>
  46. protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
  47. {
  48. if (item.VideoType == VideoType.Iso)
  49. {
  50. return _isoManager.Mount(item.Path, cancellationToken);
  51. }
  52. return NullMountTaskResult;
  53. }
  54. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  55. {
  56. return new List<ImageType> { ImageType.Primary };
  57. }
  58. public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
  59. {
  60. var video = (Video)item;
  61. // No support for this
  62. if (video.VideoType == VideoType.HdDvd || video.IsPlaceHolder)
  63. {
  64. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  65. }
  66. // Can't extract from iso's if we weren't unable to determine iso type
  67. if (video.VideoType == VideoType.Iso && !video.IsoType.HasValue)
  68. {
  69. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  70. }
  71. // Can't extract if we didn't find a video stream in the file
  72. if (!video.DefaultVideoStreamIndex.HasValue)
  73. {
  74. _logger.Info("Skipping image extraction due to missing DefaultVideoStreamIndex for {0}.", video.Path ?? string.Empty);
  75. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  76. }
  77. return GetVideoImage(video, cancellationToken);
  78. }
  79. public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
  80. {
  81. var isoMount = await MountIsoIfNeeded(item, cancellationToken).ConfigureAwait(false);
  82. try
  83. {
  84. var protocol = item.LocationType == LocationType.Remote
  85. ? MediaProtocol.Http
  86. : MediaProtocol.File;
  87. var inputPath = MediaEncoderHelpers.GetInputArgument(_fileSystem, item.Path, protocol, isoMount, item.PlayableStreamFileNames);
  88. var mediaStreams =
  89. item.GetMediaSources(false)
  90. .Take(1)
  91. .SelectMany(i => i.MediaStreams)
  92. .ToList();
  93. var imageStreams =
  94. mediaStreams
  95. .Where(i => i.Type == MediaStreamType.EmbeddedImage)
  96. .ToList();
  97. var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
  98. imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
  99. imageStreams.FirstOrDefault();
  100. string extractedImagePath;
  101. if (imageStream != null)
  102. {
  103. // Instead of using the raw stream index, we need to use nth video/embedded image stream
  104. var videoIndex = -1;
  105. foreach (var mediaStream in mediaStreams)
  106. {
  107. if (mediaStream.Type == MediaStreamType.Video ||
  108. mediaStream.Type == MediaStreamType.EmbeddedImage)
  109. {
  110. videoIndex++;
  111. }
  112. if (mediaStream == imageStream)
  113. {
  114. break;
  115. }
  116. }
  117. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, videoIndex, cancellationToken).ConfigureAwait(false);
  118. }
  119. else
  120. {
  121. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  122. // Always use 10 seconds for dvd because our duration could be out of whack
  123. var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
  124. item.RunTimeTicks.Value > 0
  125. ? TimeSpan.FromTicks(Convert.ToInt64(item.RunTimeTicks.Value * .1))
  126. : TimeSpan.FromSeconds(10);
  127. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
  128. }
  129. return new DynamicImageResponse
  130. {
  131. Format = ImageFormat.Jpg,
  132. HasImage = true,
  133. Path = extractedImagePath,
  134. Protocol = MediaProtocol.File
  135. };
  136. }
  137. finally
  138. {
  139. if (isoMount != null)
  140. {
  141. isoMount.Dispose();
  142. }
  143. }
  144. }
  145. public string Name
  146. {
  147. get { return "Screen Grabber"; }
  148. }
  149. public bool Supports(IHasImages item)
  150. {
  151. var video = item as Video;
  152. if (item.LocationType == LocationType.FileSystem && video != null && !video.IsPlaceHolder &&
  153. !video.IsShortcut && !video.IsArchive)
  154. {
  155. return true;
  156. }
  157. return false;
  158. }
  159. public int Order
  160. {
  161. get
  162. {
  163. // Make sure this comes after internet image providers
  164. return 100;
  165. }
  166. }
  167. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  168. {
  169. if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
  170. {
  171. var file = directoryService.GetFile(item.Path);
  172. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  173. {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. }
  180. }