VideoImageProvider.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Threading;
  14. using System.Threading.Tasks;
  15. using CommonIO;
  16. namespace MediaBrowser.Providers.MediaInfo
  17. {
  18. public class VideoImageProvider : IDynamicImageProvider, IHasItemChangeMonitor, IHasOrder
  19. {
  20. private readonly IIsoManager _isoManager;
  21. private readonly IMediaEncoder _mediaEncoder;
  22. private readonly IServerConfigurationManager _config;
  23. private readonly ILibraryManager _libraryManager;
  24. private readonly ILogger _logger;
  25. private readonly IFileSystem _fileSystem;
  26. public VideoImageProvider(IIsoManager isoManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem)
  27. {
  28. _isoManager = isoManager;
  29. _mediaEncoder = mediaEncoder;
  30. _config = config;
  31. _libraryManager = libraryManager;
  32. _logger = logger;
  33. _fileSystem = fileSystem;
  34. }
  35. /// <summary>
  36. /// The null mount task result
  37. /// </summary>
  38. protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
  39. /// <summary>
  40. /// Mounts the iso if needed.
  41. /// </summary>
  42. /// <param name="item">The item.</param>
  43. /// <param name="cancellationToken">The cancellation token.</param>
  44. /// <returns>Task{IIsoMount}.</returns>
  45. protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
  46. {
  47. if (item.VideoType == VideoType.Iso)
  48. {
  49. return _isoManager.Mount(item.Path, cancellationToken);
  50. }
  51. return NullMountTaskResult;
  52. }
  53. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  54. {
  55. return new List<ImageType> { ImageType.Primary };
  56. }
  57. public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
  58. {
  59. var video = (Video)item;
  60. // No support for this
  61. if (video.VideoType == VideoType.HdDvd || video.IsPlaceHolder)
  62. {
  63. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  64. }
  65. // Can't extract from iso's if we weren't unable to determine iso type
  66. if (video.VideoType == VideoType.Iso && !video.IsoType.HasValue)
  67. {
  68. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  69. }
  70. // Can't extract if we didn't find a video stream in the file
  71. if (!video.DefaultVideoStreamIndex.HasValue)
  72. {
  73. _logger.Info("Skipping image extraction due to missing DefaultVideoStreamIndex for {0}.", video.Path ?? string.Empty);
  74. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  75. }
  76. return GetVideoImage(video, cancellationToken);
  77. }
  78. public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
  79. {
  80. var isoMount = await MountIsoIfNeeded(item, cancellationToken).ConfigureAwait(false);
  81. try
  82. {
  83. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  84. // Always use 10 seconds for dvd because our duration could be out of whack
  85. var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
  86. item.RunTimeTicks.Value > 0
  87. ? TimeSpan.FromTicks(Convert.ToInt64(item.RunTimeTicks.Value * .1))
  88. : TimeSpan.FromSeconds(10);
  89. var protocol = item.LocationType == LocationType.Remote
  90. ? MediaProtocol.Http
  91. : MediaProtocol.File;
  92. var inputPath = MediaEncoderHelpers.GetInputArgument(_fileSystem, item.Path, protocol, isoMount, item.PlayableStreamFileNames);
  93. var stream = await _mediaEncoder.ExtractVideoImage(inputPath, protocol, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
  94. return new DynamicImageResponse
  95. {
  96. Format = ImageFormat.Jpg,
  97. HasImage = true,
  98. Stream = stream
  99. };
  100. }
  101. finally
  102. {
  103. if (isoMount != null)
  104. {
  105. isoMount.Dispose();
  106. }
  107. }
  108. }
  109. public string Name
  110. {
  111. get { return "Screen Grabber"; }
  112. }
  113. public bool Supports(IHasImages item)
  114. {
  115. var video = item as Video;
  116. if (item.LocationType == LocationType.FileSystem && video != null && !video.IsPlaceHolder &&
  117. !video.IsShortcut && !video.IsArchive)
  118. {
  119. return true;
  120. }
  121. return false;
  122. }
  123. public int Order
  124. {
  125. get
  126. {
  127. // Make sure this comes after internet image providers
  128. return 100;
  129. }
  130. }
  131. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  132. {
  133. if (item.DateModifiedDuringLastRefresh.HasValue)
  134. {
  135. if (item.DateModifiedDuringLastRefresh.Value != item.DateModified)
  136. {
  137. return true;
  138. }
  139. }
  140. return false;
  141. }
  142. }
  143. }