jellyfin-server/MediaBrowser.Controller/Providers/MediaInfo/FFMpegAudioImageProvider.cs

144 lines
5.5 KiB
C#
Raw Normal View History

using MediaBrowser.Common.MediaInfo;
2013-04-05 04:13:41 +00:00
using MediaBrowser.Controller.Configuration;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller.Entities;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Model.Entities;
2013-04-05 04:13:41 +00:00
using MediaBrowser.Model.Logging;
2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Concurrent;
2013-02-21 01:33:05 +00:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers.MediaInfo
{
/// <summary>
/// Uses ffmpeg to create video images
/// </summary>
2013-04-05 04:13:41 +00:00
public class FFMpegAudioImageProvider : BaseFFMpegProvider<Audio>
2013-02-21 01:33:05 +00:00
{
public FFMpegAudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder)
: base(logManager, configurationManager, mediaEncoder)
2013-03-02 17:59:15 +00:00
{
}
/// <summary>
/// The true task result
/// </summary>
protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
2013-04-05 04:13:41 +00:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Last; }
}
/// <summary>
/// The _locks
/// </summary>
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
/// <summary>
/// Gets the lock.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>System.Object.</returns>
private SemaphoreSlim GetLock(string filename)
{
return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
}
2013-04-09 12:50:51 +00:00
/// <summary>
/// Needses the refresh internal.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (!string.IsNullOrEmpty(item.PrimaryImagePath))
{
return false;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
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>
2013-04-05 04:13:41 +00:00
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
2013-02-21 01:33:05 +00:00
{
2013-04-05 04:13:41 +00:00
var success = ProviderRefreshStatus.Success;
2013-02-21 01:33:05 +00:00
2013-04-05 04:13:41 +00:00
if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
2013-02-21 01:33:05 +00:00
{
2013-04-05 04:13:41 +00:00
var album = item.ResolveArgs.Parent as MusicAlbum;
if (album != null)
{
// First try to use the parent's image
item.PrimaryImagePath = item.ResolveArgs.Parent.PrimaryImagePath;
}
2013-02-21 01:33:05 +00:00
// If it's still empty see if there's an embedded image
2013-04-05 04:13:41 +00:00
if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
2013-02-21 01:33:05 +00:00
{
2013-04-05 04:13:41 +00:00
var audio = (Audio)item;
2013-02-21 01:33:05 +00:00
if (audio.MediaStreams != null && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
{
2013-04-05 04:13:41 +00:00
var filename = album != null && string.IsNullOrEmpty(audio.Album + album.DateModified.Ticks) ? (audio.Id.ToString() + audio.DateModified.Ticks) : audio.Album;
2013-02-21 01:33:05 +00:00
2013-04-05 04:13:41 +00:00
var path = Kernel.Instance.FFMpegManager.AudioImageCache.GetResourcePath(filename + "_primary", ".jpg");
2013-02-21 01:33:05 +00:00
if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
{
2013-04-05 04:13:41 +00:00
var semaphore = GetLock(path);
2013-02-21 01:33:05 +00:00
2013-04-05 04:13:41 +00:00
// Acquire a lock
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
2013-04-05 04:13:41 +00:00
// Check again
if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
{
try
{
await MediaEncoder.ExtractImage(new[] { audio.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
}
catch
{
success = ProviderRefreshStatus.Failure;
2013-04-05 04:13:41 +00:00
}
finally
{
semaphore.Release();
}
}
else
{
semaphore.Release();
}
}
2013-02-21 01:33:05 +00:00
2013-04-05 04:13:41 +00:00
if (success == ProviderRefreshStatus.Success)
{
// Image is already in the cache
audio.PrimaryImagePath = path;
}
}
}
2013-02-21 01:33:05 +00:00
}
2013-04-05 04:13:41 +00:00
SetLastRefreshed(item, DateTime.UtcNow, success);
2013-02-21 01:33:05 +00:00
return true;
}
}
}