VideoImageProvider.cs 5.2 KB

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