2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2013-09-14 21:19:32 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2021-11-18 14:04:30 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 16:37:41 +00:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2021-11-18 14:04:30 +00:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
2013-09-14 21:19:32 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-11-29 19:51:30 +00:00
|
|
|
using MediaBrowser.Model.Drawing;
|
2020-09-24 06:41:42 +00:00
|
|
|
using MediaBrowser.Model.Dto;
|
2013-09-14 21:19:32 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2014-08-19 01:42:53 +00:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
2021-10-03 19:28:21 +00:00
|
|
|
/// <summary>
|
2021-10-11 19:25:12 +00:00
|
|
|
/// Uses <see cref="IMediaEncoder"/> to create still images from the main video.
|
2021-10-03 19:28:21 +00:00
|
|
|
/// </summary>
|
2017-10-13 19:22:24 +00:00
|
|
|
public class VideoImageProvider : IDynamicImageProvider, IHasOrder
|
2013-09-14 21:19:32 +00:00
|
|
|
{
|
2021-11-18 14:04:30 +00:00
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2014-02-04 20:19:29 +00:00
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<VideoImageProvider> _logger;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
2021-10-11 19:25:12 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="VideoImageProvider"/> class.
|
|
|
|
/// </summary>
|
2021-11-18 14:04:30 +00:00
|
|
|
/// <param name="mediaSourceManager">The media source manager for fetching item streams.</param>
|
2021-10-11 19:25:12 +00:00
|
|
|
/// <param name="mediaEncoder">The media encoder for capturing images.</param>
|
|
|
|
/// <param name="logger">The logger.</param>
|
2021-11-18 14:04:30 +00:00
|
|
|
public VideoImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> logger)
|
2013-09-14 21:19:32 +00:00
|
|
|
{
|
2021-11-18 14:04:30 +00:00
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2014-02-04 20:19:29 +00:00
|
|
|
_mediaEncoder = mediaEncoder;
|
2015-04-16 03:23:13 +00:00
|
|
|
_logger = logger;
|
2013-09-14 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
/// <inheritdoc />
|
2020-09-07 11:20:39 +00:00
|
|
|
public string Name => "Screen Grabber";
|
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
/// <inheritdoc />
|
2020-09-07 11:20:39 +00:00
|
|
|
// Make sure this comes after internet image providers
|
|
|
|
public int Order => 100;
|
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2013-09-14 21:19:32 +00:00
|
|
|
{
|
2021-09-01 16:47:24 +00:00
|
|
|
return new[] { ImageType.Primary };
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
2013-09-14 21:19:32 +00:00
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
var video = (Video)item;
|
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
// No support for these
|
|
|
|
if (video.IsPlaceHolder || video.VideoType == VideoType.Dvd)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't extract if we didn't find a video stream in the file
|
|
|
|
if (!video.DefaultVideoStreamIndex.HasValue)
|
|
|
|
{
|
2021-09-01 16:47:24 +00:00
|
|
|
_logger.LogInformation("Skipping image extraction due to missing DefaultVideoStreamIndex for {Path}.", video.Path ?? string.Empty);
|
2014-02-06 04:39:16 +00:00
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetVideoImage(video, cancellationToken);
|
2013-09-14 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
private async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
|
2013-09-14 21:19:32 +00:00
|
|
|
{
|
2021-09-01 16:47:24 +00:00
|
|
|
MediaSourceInfo mediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
VideoType = item.VideoType,
|
|
|
|
IsoType = item.IsoType,
|
|
|
|
Protocol = item.PathProtocol ?? MediaProtocol.File,
|
|
|
|
};
|
2013-09-14 21:19:32 +00:00
|
|
|
|
2021-10-03 19:28:21 +00:00
|
|
|
// If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
|
|
|
|
// Always use 10 seconds for dvd because our duration could be out of whack
|
2021-11-19 21:40:42 +00:00
|
|
|
var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks > 0
|
2021-10-03 19:28:21 +00:00
|
|
|
? TimeSpan.FromTicks(item.RunTimeTicks.Value / 10)
|
|
|
|
: TimeSpan.FromSeconds(10);
|
2016-04-13 20:49:16 +00:00
|
|
|
|
2021-11-19 21:40:42 +00:00
|
|
|
var query = new MediaStreamQuery { ItemId = item.Id, Index = item.DefaultVideoStreamIndex };
|
|
|
|
var videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
|
2022-12-05 14:00:20 +00:00
|
|
|
if (videoStream is null)
|
2021-11-19 21:40:42 +00:00
|
|
|
{
|
|
|
|
query.Type = MediaStreamType.Video;
|
|
|
|
query.Index = null;
|
|
|
|
videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
|
|
|
|
}
|
2021-10-11 10:34:18 +00:00
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (videoStream is null)
|
2021-10-11 10:34:18 +00:00
|
|
|
{
|
|
|
|
_logger.LogInformation("Skipping image extraction: no video stream found for {Path}.", item.Path ?? string.Empty);
|
|
|
|
return new DynamicImageResponse { HasImage = false };
|
|
|
|
}
|
|
|
|
|
2021-10-03 19:28:21 +00:00
|
|
|
string extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, videoStream, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
|
2017-08-05 19:02:33 +00:00
|
|
|
|
|
|
|
return new DynamicImageResponse
|
2013-09-14 21:19:32 +00:00
|
|
|
{
|
2017-08-05 19:02:33 +00:00
|
|
|
Format = ImageFormat.Jpg,
|
|
|
|
HasImage = true,
|
|
|
|
Path = extractedImagePath,
|
|
|
|
Protocol = MediaProtocol.File
|
|
|
|
};
|
2013-09-14 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-12-15 16:53:32 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (item.IsShortcut)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!item.IsFileProtocol)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:47:24 +00:00
|
|
|
return item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia;
|
2013-12-15 16:53:32 +00:00
|
|
|
}
|
2013-09-14 21:19:32 +00:00
|
|
|
}
|
|
|
|
}
|