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