2021-09-01 09:47:24 -07:00
|
|
|
#nullable enable
|
2020-06-19 11:24:13 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2019-01-13 12:26:31 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2013-09-14 14:19:32 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-02-20 09:37:41 -07:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2013-09-14 14:19:32 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-11-29 12:51:30 -07:00
|
|
|
using MediaBrowser.Model.Drawing;
|
2020-09-23 23:41:42 -07:00
|
|
|
using MediaBrowser.Model.Dto;
|
2013-09-14 14:19:32 -07:00
|
|
|
using MediaBrowser.Model.Entities;
|
2014-08-18 18:42:53 -07:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2019-01-13 12:26:31 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-09-14 14:19:32 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
2017-10-13 12:22:24 -07:00
|
|
|
public class VideoImageProvider : IDynamicImageProvider, IHasOrder
|
2013-09-14 14:19:32 -07:00
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2020-06-05 17:15:56 -07:00
|
|
|
private readonly ILogger<VideoImageProvider> _logger;
|
2013-09-14 14:19:32 -07:00
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
public VideoImageProvider(IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> logger)
|
2013-09-14 14:19:32 -07:00
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
_mediaEncoder = mediaEncoder;
|
2015-04-15 20:23:13 -07:00
|
|
|
_logger = logger;
|
2013-09-14 14:19:32 -07:00
|
|
|
}
|
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
/// <inheritdoc />
|
2020-09-07 04:20:39 -07:00
|
|
|
public string Name => "Screen Grabber";
|
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
/// <inheritdoc />
|
2020-09-07 04:20:39 -07:00
|
|
|
// Make sure this comes after internet image providers
|
|
|
|
public int Order => 100;
|
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
/// <inheritdoc />
|
2018-09-12 10:26:21 -07:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2013-09-14 14:19:32 -07:00
|
|
|
{
|
2021-09-01 09:47:24 -07:00
|
|
|
return new[] { ImageType.Primary };
|
2014-02-04 13:19:29 -07:00
|
|
|
}
|
2013-09-14 14:19:32 -07:00
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
/// <inheritdoc />
|
2018-09-12 10:26:21 -07:00
|
|
|
public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
|
2014-02-04 13:19:29 -07:00
|
|
|
{
|
2014-02-05 21:39:16 -07:00
|
|
|
var video = (Video)item;
|
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
// No support for these
|
|
|
|
if (video.IsPlaceHolder || video.VideoType == VideoType.Dvd)
|
2014-02-05 21:39:16 -07: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 09:47:24 -07:00
|
|
|
_logger.LogInformation("Skipping image extraction due to missing DefaultVideoStreamIndex for {Path}.", video.Path ?? string.Empty);
|
2014-02-05 21:39:16 -07:00
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetVideoImage(video, cancellationToken);
|
2013-09-14 14:19:32 -07:00
|
|
|
}
|
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
private async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
|
2013-09-14 14:19:32 -07:00
|
|
|
{
|
2021-09-01 09:47:24 -07:00
|
|
|
MediaSourceInfo mediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
VideoType = item.VideoType,
|
|
|
|
IsoType = item.IsoType,
|
|
|
|
Protocol = item.PathProtocol ?? MediaProtocol.File,
|
|
|
|
};
|
2013-09-14 14:19:32 -07:00
|
|
|
|
2017-08-05 12:02:33 -07:00
|
|
|
var mediaStreams =
|
|
|
|
item.GetMediaStreams();
|
2016-04-13 13:49:16 -07:00
|
|
|
|
2017-08-05 12:02:33 -07:00
|
|
|
var imageStreams =
|
|
|
|
mediaStreams
|
|
|
|
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
|
|
|
|
.ToList();
|
2016-04-13 13:49:16 -07:00
|
|
|
|
2017-08-05 12:02:33 -07:00
|
|
|
string extractedImagePath;
|
2016-04-13 13:49:16 -07:00
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
if (imageStreams.Count == 0)
|
2017-08-05 12:02:33 -07: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
|
|
|
|
var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
|
|
|
|
item.RunTimeTicks.Value > 0
|
2021-09-01 09:47:24 -07:00
|
|
|
? TimeSpan.FromTicks(item.RunTimeTicks.Value / 10)
|
2017-08-05 12:02:33 -07:00
|
|
|
: TimeSpan.FromSeconds(10);
|
2017-08-01 12:43:39 -07:00
|
|
|
|
2017-08-05 12:02:33 -07:00
|
|
|
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
|
2021-09-01 09:47:24 -07:00
|
|
|
extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, videoStream, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var imageStream = imageStreams.Find(i => (i.Comment ?? string.Empty).Contains("front", StringComparison.OrdinalIgnoreCase))
|
|
|
|
?? imageStreams.Find(i => (i.Comment ?? string.Empty).Contains("cover", StringComparison.OrdinalIgnoreCase))
|
|
|
|
?? imageStreams[0];
|
|
|
|
|
|
|
|
extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, cancellationToken).ConfigureAwait(false);
|
2013-09-14 14:19:32 -07:00
|
|
|
}
|
2017-08-05 12:02:33 -07:00
|
|
|
|
|
|
|
return new DynamicImageResponse
|
2013-09-14 14:19:32 -07:00
|
|
|
{
|
2017-08-05 12:02:33 -07:00
|
|
|
Format = ImageFormat.Jpg,
|
|
|
|
HasImage = true,
|
|
|
|
Path = extractedImagePath,
|
|
|
|
Protocol = MediaProtocol.File
|
|
|
|
};
|
2013-09-14 14:19:32 -07:00
|
|
|
}
|
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
/// <inheritdoc />
|
2018-09-12 10:26:21 -07:00
|
|
|
public bool Supports(BaseItem item)
|
2013-12-15 09:53:32 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (item.IsShortcut)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-15 14:43:52 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!item.IsFileProtocol)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-01 09:47:24 -07:00
|
|
|
return item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia;
|
2013-12-15 09:53:32 -07:00
|
|
|
}
|
2013-09-14 14:19:32 -07:00
|
|
|
}
|
|
|
|
}
|