2014-02-04 13:19:29 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
2013-09-14 14:19:32 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.MediaInfo;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using System;
|
2014-02-04 13:19:29 -07:00
|
|
|
|
using System.Collections.Generic;
|
2013-09-14 14:19:32 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
public class VideoImageProvider : IDynamicImageProvider
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly IIsoManager _isoManager;
|
2014-02-04 13:19:29 -07:00
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2013-09-14 14:19:32 -07:00
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
public VideoImageProvider(IIsoManager isoManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config)
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
|
|
|
|
_isoManager = isoManager;
|
2014-02-04 13:19:29 -07:00
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
_config = config;
|
2013-09-14 14:19:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Qualifieses for extraction.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
private bool QualifiesForExtraction(Video item)
|
|
|
|
|
{
|
|
|
|
|
// No support for this
|
|
|
|
|
if (item.VideoType == VideoType.HdDvd)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Can't extract from iso's if we weren't unable to determine iso type
|
|
|
|
|
if (item.VideoType == VideoType.Iso && !item.IsoType.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Can't extract if we didn't find a video stream in the file
|
2013-12-05 20:39:44 -07:00
|
|
|
|
if (!item.DefaultVideoStreamIndex.HasValue)
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-04 13:19:29 -07:00
|
|
|
|
/// The null mount task result
|
2013-09-14 14:19:32 -07:00
|
|
|
|
/// </summary>
|
2014-02-04 13:19:29 -07:00
|
|
|
|
protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
|
2013-09-14 14:19:32 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-04 13:19:29 -07:00
|
|
|
|
/// Mounts the iso if needed.
|
2013-09-14 14:19:32 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2014-02-04 13:19:29 -07:00
|
|
|
|
/// <returns>Task{IIsoMount}.</returns>
|
|
|
|
|
protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
if (item.VideoType == VideoType.Iso)
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
return _isoManager.Mount(item.Path, cancellationToken);
|
2013-09-14 14:19:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
return NullMountTaskResult;
|
2013-09-14 14:19:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
return new List<ImageType> { ImageType.Primary };
|
|
|
|
|
}
|
2013-09-14 14:19:32 -07:00
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return GetVideoImage((Video)item, cancellationToken);
|
2013-09-14 14:19:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
var isoMount = await MountIsoIfNeeded(item, cancellationToken).ConfigureAwait(false);
|
2013-09-14 14:19:32 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 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
|
2014-02-04 13:19:29 -07:00
|
|
|
|
var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
|
|
|
|
|
item.RunTimeTicks.Value > 0
|
|
|
|
|
? TimeSpan.FromTicks(Convert.ToInt64(item.RunTimeTicks.Value * .1))
|
2013-09-14 14:19:32 -07:00
|
|
|
|
: TimeSpan.FromSeconds(10);
|
|
|
|
|
|
|
|
|
|
InputType type;
|
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
var inputPath = MediaEncoderHelpers.GetInputArgument(item.Path, item.LocationType == LocationType.Remote, item.VideoType, item.IsoType, isoMount, item.PlayableStreamFileNames, out type);
|
2013-09-14 14:19:32 -07:00
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
var stream = await _mediaEncoder.ExtractImage(inputPath, type, false, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
|
2013-09-14 14:19:32 -07:00
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
return new DynamicImageResponse
|
|
|
|
|
{
|
|
|
|
|
Format = ImageFormat.Jpg,
|
|
|
|
|
HasImage = true,
|
|
|
|
|
Stream = stream
|
|
|
|
|
};
|
2013-09-14 14:19:32 -07:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (isoMount != null)
|
|
|
|
|
{
|
|
|
|
|
isoMount.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
public string Name
|
2013-09-14 14:19:32 -07:00
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
get { return "Embedded Image"; }
|
2013-09-14 14:19:32 -07:00
|
|
|
|
}
|
2013-12-15 09:53:32 -07:00
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-12-15 09:53:32 -07:00
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
if (!_config.Configuration.EnableVideoImageExtraction)
|
2013-12-15 09:53:32 -07:00
|
|
|
|
{
|
2014-02-04 13:19:29 -07:00
|
|
|
|
return false;
|
2013-12-15 09:53:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 13:19:29 -07:00
|
|
|
|
return item.LocationType == LocationType.FileSystem && item is Video;
|
2013-12-15 09:53:32 -07:00
|
|
|
|
}
|
2013-09-14 14:19:32 -07:00
|
|
|
|
}
|
|
|
|
|
}
|