jellyfin/MediaBrowser.Controller/Providers/MediaInfo/FFMpegVideoImageProvider.cs

160 lines
6.0 KiB
C#
Raw Normal View History

2013-02-21 01:33:05 +00:00
using MediaBrowser.Common.IO;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller.Configuration;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Model.Logging;
2013-02-21 01:33:05 +00:00
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers.MediaInfo
{
/// <summary>
/// Uses ffmpeg to create video images
/// </summary>
2013-03-02 17:59:15 +00:00
public class FfMpegVideoImageProvider : BaseFFMpegImageProvider<Video>
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// The _iso manager
/// </summary>
private readonly IIsoManager _isoManager;
/// <summary>
2013-03-02 17:59:15 +00:00
/// Initializes a new instance of the <see cref="FfMpegVideoImageProvider" /> class.
/// </summary>
/// <param name="isoManager">The iso manager.</param>
2013-03-04 05:43:06 +00:00
/// <param name="logManager">The log manager.</param>
/// <param name="configurationManager">The configuration manager.</param>
public FfMpegVideoImageProvider(IIsoManager isoManager, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(logManager, configurationManager)
{
_isoManager = isoManager;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Supportses the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
public override bool Supports(BaseItem item)
{
if (item.LocationType != LocationType.FileSystem)
{
return false;
}
var video = item as Video;
if (video != null)
{
if (video.VideoType == VideoType.Iso && video.IsoType.HasValue && _isoManager.CanMount(item.Path))
2013-02-21 01:33:05 +00:00
{
return true;
}
// We can only extract images from folder rips if we know the largest stream path
return video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.BluRay || video.VideoType == VideoType.Dvd;
}
return false;
}
/// <summary>
/// The true task result
/// </summary>
protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
2013-02-21 01:33:05 +00:00
/// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary>
/// <param name="item">The item.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
2013-02-21 01:33:05 +00:00
{
if (string.IsNullOrEmpty(item.PrimaryImagePath))
{
var video = (Video)item;
var filename = item.Id + "_" + item.DateModified.Ticks + "_primary";
var path = Kernel.Instance.FFMpegManager.VideoImageCache.GetResourcePath(filename, ".jpg");
if (!Kernel.Instance.FFMpegManager.VideoImageCache.ContainsFilePath(path))
{
return ExtractImage(video, path, cancellationToken);
}
// Image is already in the cache
item.PrimaryImagePath = path;
}
SetLastRefreshed(item, DateTime.UtcNow);
return TrueTaskResult;
}
/// <summary>
/// Mounts the iso if needed.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>IsoMount.</returns>
protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
{
if (item.VideoType == VideoType.Iso)
{
return _isoManager.Mount(item.Path, cancellationToken);
2013-02-21 01:33:05 +00:00
}
return NullMountTaskResult;
}
/// <summary>
/// Extracts the image.
/// </summary>
/// <param name="video">The video.</param>
/// <param name="path">The path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
private async Task<bool> ExtractImage(Video video, string path, CancellationToken cancellationToken)
{
var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
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
var imageOffset = video.VideoType != VideoType.Dvd && video.RunTimeTicks.HasValue && video.RunTimeTicks.Value > 0
? TimeSpan.FromTicks(Convert.ToInt64(video.RunTimeTicks.Value * .1))
: TimeSpan.FromSeconds(10);
var inputPath = isoMount == null ?
Kernel.Instance.FFMpegManager.GetInputArgument(video) :
Kernel.Instance.FFMpegManager.GetInputArgument(video, isoMount);
var success = await Kernel.Instance.FFMpegManager.ExtractImage(inputPath, imageOffset, path, cancellationToken).ConfigureAwait(false);
if (success)
{
video.PrimaryImagePath = path;
SetLastRefreshed(video, DateTime.UtcNow);
}
else
{
SetLastRefreshed(video, DateTime.UtcNow, ProviderRefreshStatus.Failure);
}
}
finally
{
if (isoMount != null)
{
isoMount.Dispose();
}
}
return true;
}
}
}