2013-05-21 04:04:38 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.MediaInfo;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Linq;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers.MediaInfo
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses ffmpeg to create video images
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AudioImageProvider : BaseMetadataProvider
|
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the image cache.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The image cache.</value>
|
|
|
|
|
public FileSystemRepository ImageCache { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _locks
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _media encoder
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
2013-04-15 15:10:12 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="BaseMetadataProvider" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="configurationManager">The configuration manager.</param>
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
|
|
|
|
/// <param name="mediaEncoder">The media encoder.</param>
|
|
|
|
|
public AudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, IMediaEncoder mediaEncoder)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
: base(logManager, configurationManager)
|
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
|
|
|
|
|
ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.AudioImagesDataPath);
|
2013-04-15 15:10:12 +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)
|
|
|
|
|
{
|
|
|
|
|
return item.LocationType == LocationType.FileSystem && item is Audio;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Override this to return the date that should be compared to the last refresh date
|
|
|
|
|
/// to determine if this provider should be re-fetched.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns>DateTime.</returns>
|
|
|
|
|
protected override DateTime CompareDate(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
return item.DateModified;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get { return MetadataProviderPriority.Last; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
|
2013-04-15 15:10:12 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// <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 async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
var audio = (Audio)item;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(audio.PrimaryImagePath) && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await CreateImagesForSong(audio, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.ErrorException("Error extracting image for {0}", ex, item.Name);
|
|
|
|
|
}
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
2013-05-21 04:04:38 +00:00
|
|
|
|
|
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
|
|
|
|
return true;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// Creates the images for song.
|
2013-04-15 15:10:12 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
/// <exception cref="System.InvalidOperationException">Can't extract an image unless the audio file has an embedded image.</exception>
|
|
|
|
|
private async Task CreateImagesForSong(Audio item, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var album = item.Parent as MusicAlbum;
|
|
|
|
|
|
|
|
|
|
var filename = item.Album ?? string.Empty;
|
|
|
|
|
filename += item.Artist ?? string.Empty;
|
|
|
|
|
filename += album == null ? item.Id.ToString("N") + item.DateModified.Ticks : album.Id.ToString("N") + album.DateModified.Ticks;
|
|
|
|
|
|
|
|
|
|
var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
|
|
|
|
|
|
|
|
|
|
if (!ImageCache.ContainsFilePath(path))
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
var semaphore = GetLock(path);
|
2013-04-15 15:10:12 +00:00
|
|
|
|
|
2013-05-21 04:04:38 +00:00
|
|
|
|
// Acquire a lock
|
|
|
|
|
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Check again
|
|
|
|
|
if (!ImageCache.ContainsFilePath(path))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Image is already in the cache
|
|
|
|
|
item.PrimaryImagePath = path;
|
|
|
|
|
|
|
|
|
|
await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
semaphore.Release();
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-21 04:04:38 +00:00
|
|
|
|
}
|
2013-04-15 15:10:12 +00:00
|
|
|
|
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the lock.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
|
|
/// <returns>SemaphoreSlim.</returns>
|
|
|
|
|
private SemaphoreSlim GetLock(string filename)
|
|
|
|
|
{
|
|
|
|
|
return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|