VideoImageProvider.cs 4.9 KB

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