jellyfin/MediaBrowser.Providers/MediaInfo/VideoImageProvider.cs

168 lines
6.3 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
2015-01-06 04:25:23 +01:00
using MediaBrowser.Controller.Library;
2014-02-20 17:37:41 +01:00
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Providers;
2014-11-29 20:51:30 +01:00
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
2015-04-16 05:23:13 +02:00
using MediaBrowser.Model.Logging;
2014-08-19 03:42:53 +02:00
using MediaBrowser.Model.MediaInfo;
using System;
using System.Collections.Generic;
2016-04-13 22:49:16 +02:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.MediaInfo
{
2014-09-11 03:57:11 +02:00
public class VideoImageProvider : IDynamicImageProvider, IHasItemChangeMonitor, IHasOrder
{
private readonly IMediaEncoder _mediaEncoder;
2015-04-16 05:23:13 +02:00
private readonly ILogger _logger;
2015-09-24 19:50:49 +02:00
private readonly IFileSystem _fileSystem;
2017-08-05 21:02:33 +02:00
public VideoImageProvider(IMediaEncoder mediaEncoder, ILogger logger, IFileSystem fileSystem)
{
_mediaEncoder = mediaEncoder;
2015-04-16 05:23:13 +02:00
_logger = logger;
2015-09-24 19:50:49 +02:00
_fileSystem = fileSystem;
}
2017-08-07 23:06:13 +02:00
public IEnumerable<ImageType> GetSupportedImages(IHasMetadata item)
{
return new List<ImageType> { ImageType.Primary };
}
2017-08-07 23:06:13 +02:00
public Task<DynamicImageResponse> GetImage(IHasMetadata item, ImageType type, CancellationToken cancellationToken)
{
var video = (Video)item;
// No support for this
2017-08-07 22:36:41 +02:00
if (video.IsPlaceHolder)
{
return Task.FromResult(new DynamicImageResponse { HasImage = false });
}
2017-08-05 21:02:33 +02:00
// No support for this
2017-08-06 07:50:37 +02:00
if (video.VideoType == VideoType.Iso || video.VideoType == VideoType.Dvd || video.VideoType == VideoType.BluRay)
{
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)
{
2015-10-05 00:04:56 +02:00
_logger.Info("Skipping image extraction due to missing DefaultVideoStreamIndex for {0}.", video.Path ?? string.Empty);
return Task.FromResult(new DynamicImageResponse { HasImage = false });
}
return GetVideoImage(video, cancellationToken);
}
public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
{
2017-08-05 21:02:33 +02:00
var protocol = item.LocationType == LocationType.Remote
? MediaProtocol.Http
: MediaProtocol.File;
2017-08-05 21:02:33 +02:00
var inputPath = MediaEncoderHelpers.GetInputArgument(_fileSystem, item.Path, protocol, null, item.GetPlayableStreamFileNames());
2017-08-05 21:02:33 +02:00
var mediaStreams =
item.GetMediaStreams();
2016-04-13 22:49:16 +02:00
2017-08-05 21:02:33 +02:00
var imageStreams =
mediaStreams
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
.ToList();
2016-04-13 22:49:16 +02:00
2017-08-05 21:02:33 +02: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 22:49:16 +02:00
2017-08-05 21:02:33 +02:00
string extractedImagePath;
2016-04-13 22:49:16 +02:00
2017-08-05 21:02:33 +02: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 22:49:16 +02:00
{
2017-08-05 21:02:33 +02:00
if (mediaStream.Type == MediaStreamType.Video ||
mediaStream.Type == MediaStreamType.EmbeddedImage)
2016-04-13 22:49:16 +02:00
{
2017-08-05 21:02:33 +02:00
videoIndex++;
}
if (mediaStream == imageStream)
{
break;
2016-04-13 22:49:16 +02:00
}
}
2017-08-05 21:02:33 +02:00
extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, imageStream, videoIndex, cancellationToken).ConfigureAwait(false);
}
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-05 21:02:33 +02:00
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
2017-08-05 21:02:33 +02:00
extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, videoStream, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
}
2017-08-05 21:02:33 +02:00
return new DynamicImageResponse
{
2017-08-05 21:02:33 +02:00
Format = ImageFormat.Jpg,
HasImage = true,
Path = extractedImagePath,
Protocol = MediaProtocol.File
};
}
public string Name
{
2014-02-19 19:50:37 +01:00
get { return "Screen Grabber"; }
}
2017-08-07 23:06:13 +02:00
public bool Supports(IHasMetadata item)
{
2014-10-14 06:22:17 +02:00
var video = item as Video;
2017-09-18 18:52:22 +02:00
if (item.LocationType == LocationType.FileSystem && video != null && !video.IsPlaceHolder && !video.IsShortcut && video.IsCompleteMedia)
2015-01-06 04:25:23 +01:00
{
return true;
}
return false;
}
public int Order
{
get
{
// Make sure this comes after internet image providers
return 100;
}
}
2014-09-11 03:57:11 +02:00
2016-04-08 20:32:38 +02:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
2014-09-11 03:57:11 +02:00
{
2016-08-24 08:13:15 +02:00
if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
2014-09-11 03:57:11 +02:00
{
var file = directoryService.GetFile(item.Path);
if (file != null && file.LastWriteTimeUtc != item.DateModified)
{
return true;
}
2014-09-11 03:57:11 +02:00
}
return false;
}
}
}