2014-02-04 20:19:29 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2015-01-06 03:25:23 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 16:37:41 +00:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-11-29 19:51:30 +00:00
|
|
|
|
using MediaBrowser.Model.Drawing;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2015-04-16 03:23:13 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-08-19 01:42:53 +00:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
using System;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-04-13 20:49:16 +00:00
|
|
|
|
using System.Linq;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
|
{
|
2014-09-11 01:57:11 +00:00
|
|
|
|
public class VideoImageProvider : IDynamicImageProvider, IHasItemChangeMonitor, IHasOrder
|
2013-09-14 21:19:32 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IIsoManager _isoManager;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2015-01-06 03:25:23 +00:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2015-04-16 03:23:13 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2015-09-24 17:50:49 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
2015-09-24 17:50:49 +00:00
|
|
|
|
public VideoImageProvider(IIsoManager isoManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem)
|
2013-09-14 21:19:32 +00:00
|
|
|
|
{
|
|
|
|
|
_isoManager = isoManager;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
_config = config;
|
2015-01-06 03:25:23 +00:00
|
|
|
|
_libraryManager = libraryManager;
|
2015-04-16 03:23:13 +00:00
|
|
|
|
_logger = logger;
|
2015-09-24 17:50:49 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-04 20:19:29 +00:00
|
|
|
|
/// The null mount task result
|
2013-09-14 21:19:32 +00:00
|
|
|
|
/// </summary>
|
2014-02-04 20:19:29 +00:00
|
|
|
|
protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-04 20:19:29 +00:00
|
|
|
|
/// Mounts the iso if needed.
|
2013-09-14 21:19:32 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2014-02-04 20:19:29 +00:00
|
|
|
|
/// <returns>Task{IIsoMount}.</returns>
|
|
|
|
|
protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
|
2013-09-14 21:19:32 +00:00
|
|
|
|
{
|
2014-02-04 20:19:29 +00:00
|
|
|
|
if (item.VideoType == VideoType.Iso)
|
2013-09-14 21:19:32 +00:00
|
|
|
|
{
|
2014-02-04 20:19:29 +00:00
|
|
|
|
return _isoManager.Mount(item.Path, cancellationToken);
|
2013-09-14 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
return NullMountTaskResult;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2013-09-14 21:19:32 +00:00
|
|
|
|
{
|
2014-02-04 20:19:29 +00:00
|
|
|
|
return new List<ImageType> { ImageType.Primary };
|
|
|
|
|
}
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
var video = (Video)item;
|
|
|
|
|
|
|
|
|
|
// No support for this
|
2014-03-03 05:11:03 +00:00
|
|
|
|
if (video.VideoType == VideoType.HdDvd || video.IsPlaceHolder)
|
2014-02-06 04:39:16 +00:00
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Can't extract from iso's if we weren't unable to determine iso type
|
|
|
|
|
if (video.VideoType == VideoType.Iso && !video.IsoType.HasValue)
|
|
|
|
|
{
|
|
|
|
|
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-04 22:04:56 +00:00
|
|
|
|
_logger.Info("Skipping image extraction due to missing DefaultVideoStreamIndex for {0}.", 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
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
|
2013-09-14 21:19:32 +00:00
|
|
|
|
{
|
2014-02-04 20:19:29 +00:00
|
|
|
|
var isoMount = await MountIsoIfNeeded(item, cancellationToken).ConfigureAwait(false);
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-06-17 01:56:23 +00:00
|
|
|
|
var protocol = item.LocationType == LocationType.Remote
|
|
|
|
|
? MediaProtocol.Http
|
|
|
|
|
: MediaProtocol.File;
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
2015-09-24 17:50:49 +00:00
|
|
|
|
var inputPath = MediaEncoderHelpers.GetInputArgument(_fileSystem, item.Path, protocol, isoMount, item.PlayableStreamFileNames);
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
2016-04-13 20:49:16 +00:00
|
|
|
|
var mediaStreams =
|
|
|
|
|
item.GetMediaSources(false)
|
|
|
|
|
.Take(1)
|
|
|
|
|
.SelectMany(i => i.MediaStreams)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var imageStreams =
|
|
|
|
|
mediaStreams
|
|
|
|
|
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
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-07-01 02:35:18 +00:00
|
|
|
|
string extractedImagePath;
|
2016-04-13 20:49:16 +00: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)
|
|
|
|
|
{
|
|
|
|
|
if (mediaStream.Type == MediaStreamType.Video ||
|
|
|
|
|
mediaStream.Type == MediaStreamType.EmbeddedImage)
|
|
|
|
|
{
|
|
|
|
|
videoIndex++;
|
|
|
|
|
}
|
|
|
|
|
if (mediaStream == imageStream)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-30 18:43:59 +00:00
|
|
|
|
extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, videoIndex, cancellationToken).ConfigureAwait(false);
|
2016-04-13 20:49:16 +00: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);
|
|
|
|
|
|
2016-09-30 18:43:59 +00:00
|
|
|
|
extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
|
2016-04-13 20:49:16 +00:00
|
|
|
|
}
|
2013-09-14 21:19:32 +00:00
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
return new DynamicImageResponse
|
|
|
|
|
{
|
|
|
|
|
Format = ImageFormat.Jpg,
|
|
|
|
|
HasImage = true,
|
2016-07-01 02:35:18 +00:00
|
|
|
|
Path = extractedImagePath,
|
|
|
|
|
Protocol = MediaProtocol.File
|
2014-02-04 20:19:29 +00:00
|
|
|
|
};
|
2013-09-14 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (isoMount != null)
|
|
|
|
|
{
|
|
|
|
|
isoMount.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
public string Name
|
2013-09-14 21:19:32 +00:00
|
|
|
|
{
|
2014-02-19 18:50:37 +00:00
|
|
|
|
get { return "Screen Grabber"; }
|
2013-09-14 21:19:32 +00:00
|
|
|
|
}
|
2013-12-15 16:53:32 +00:00
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-12-15 16:53:32 +00:00
|
|
|
|
{
|
2014-10-14 04:22:17 +00:00
|
|
|
|
var video = item as Video;
|
|
|
|
|
|
2015-01-06 03:25:23 +00:00
|
|
|
|
if (item.LocationType == LocationType.FileSystem && video != null && !video.IsPlaceHolder &&
|
|
|
|
|
!video.IsShortcut && !video.IsArchive)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2013-12-15 16:53:32 +00:00
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
public int Order
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// Make sure this comes after internet image providers
|
|
|
|
|
return 100;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-11 01:57:11 +00:00
|
|
|
|
|
2016-04-08 18:32:38 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
|
2014-09-11 01:57:11 +00:00
|
|
|
|
{
|
2016-08-24 06:13:15 +00:00
|
|
|
|
if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
|
2014-09-11 01:57:11 +00:00
|
|
|
|
{
|
2016-08-13 05:49:00 +00:00
|
|
|
|
var file = directoryService.GetFile(item.Path);
|
|
|
|
|
if (file != null && file.LastWriteTimeUtc != item.DateModified)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-09-11 01:57:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-09-14 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|