2013-12-15 16:53:32 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
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;
|
2014-01-12 06:31:21 +00:00
|
|
|
|
using MediaBrowser.Controller.MediaInfo;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
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;
|
2013-06-25 01:22:21 +00:00
|
|
|
|
using System.IO;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
using System.Linq;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses ffmpeg to create video images
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AudioImageProvider : BaseMetadataProvider
|
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _locks
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _media encoder
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
|
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="mediaEncoder">The media encoder.</param>
|
2013-09-04 17:02:19 +00:00
|
|
|
|
public AudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
: base(logManager, configurationManager)
|
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
_mediaEncoder = mediaEncoder;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 16:51:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether [refresh on version change].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
|
|
|
|
|
protected override bool RefreshOnVersionChange
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the provider version.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The provider version.</value>
|
|
|
|
|
protected override string ProviderVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "1";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 01:22:21 +00:00
|
|
|
|
public override ItemUpdateType ItemUpdateType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 16:53:32 +00:00
|
|
|
|
|
2013-04-15 15:10:12 +00:00
|
|
|
|
/// <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>
|
2013-12-06 20:07:34 +00:00
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-11-30 06:08:14 +00:00
|
|
|
|
item.ValidateImages();
|
|
|
|
|
|
2013-05-21 04:04:38 +00:00
|
|
|
|
var audio = (Audio)item;
|
|
|
|
|
|
2013-12-06 03:39:44 +00:00
|
|
|
|
if (string.IsNullOrEmpty(audio.PrimaryImagePath) && audio.HasEmbeddedImage)
|
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
|
|
|
|
|
2013-12-06 20:07:34 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
|
2013-05-21 04:04:38 +00:00
|
|
|
|
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();
|
|
|
|
|
|
2013-12-15 16:53:32 +00:00
|
|
|
|
var path = GetAudioImagePath(item);
|
2013-05-21 04:04:38 +00:00
|
|
|
|
|
2013-06-04 02:02:49 +00:00
|
|
|
|
if (!File.Exists(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
|
2013-06-04 02:02:49 +00:00
|
|
|
|
if (!File.Exists(path))
|
2013-05-21 04:04:38 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-06-04 02:02:49 +00:00
|
|
|
|
var parentPath = Path.GetDirectoryName(path);
|
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
Directory.CreateDirectory(parentPath);
|
2013-06-04 02:02:49 +00:00
|
|
|
|
|
2014-01-12 06:31:21 +00:00
|
|
|
|
await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.File, true, null, null, path, cancellationToken).ConfigureAwait(false);
|
2013-05-21 04:04:38 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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-22 16:51:53 +00:00
|
|
|
|
|
|
|
|
|
// Image is already in the cache
|
|
|
|
|
item.PrimaryImagePath = path;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
}
|
2013-04-15 15:10:12 +00:00
|
|
|
|
|
2013-12-15 16:53:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the audio image path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string GetAudioImagePath(Audio item)
|
|
|
|
|
{
|
|
|
|
|
var album = item.Parent as MusicAlbum;
|
|
|
|
|
|
|
|
|
|
var filename = item.Album ?? string.Empty;
|
|
|
|
|
filename += item.Artists.FirstOrDefault() ?? string.Empty;
|
2014-01-01 18:26:31 +00:00
|
|
|
|
filename += album == null ? item.Id.ToString("N") + "_primary" + item.DateModified.Ticks : album.Id.ToString("N") + album.DateModified.Ticks + "_primary";
|
2013-12-15 16:53:32 +00:00
|
|
|
|
|
|
|
|
|
filename = filename.GetMD5() + ".jpg";
|
|
|
|
|
|
|
|
|
|
var prefix = filename.Substring(0, 1);
|
|
|
|
|
|
|
|
|
|
return Path.Combine(AudioImagesPath, prefix, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the audio images data path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The audio images data path.</value>
|
|
|
|
|
public string AudioImagesPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(ConfigurationManager.ApplicationPaths.DataPath, "extracted-audio-images");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|