123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using System.IO;
- using MediaBrowser.Common.IO;
- using MediaBrowser.Common.MediaInfo;
- using MediaBrowser.Controller;
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Entities.Audio;
- using MediaBrowser.Controller.Library;
- using MediaBrowser.Controller.Providers;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.Logging;
- using System;
- using System.Collections.Concurrent;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MediaBrowser.Providers.MediaInfo
- {
- /// <summary>
- /// Uses ffmpeg to create video images
- /// </summary>
- public class AudioImageProvider : BaseMetadataProvider
- {
- /// <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;
- /// <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>
- /// <param name="libraryManager">The library manager.</param>
- /// <param name="mediaEncoder">The media encoder.</param>
- public AudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, IMediaEncoder mediaEncoder)
- : base(logManager, configurationManager)
- {
- _libraryManager = libraryManager;
- _mediaEncoder = mediaEncoder;
- ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.AudioImagesDataPath);
- }
- /// <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";
- }
- }
- /// <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>
- /// 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 async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
- {
- var audio = (Audio)item;
- if (string.IsNullOrEmpty(audio.PrimaryImagePath) && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
- {
- try
- {
- await CreateImagesForSong(audio, cancellationToken).ConfigureAwait(false);
- }
- catch (Exception ex)
- {
- Logger.ErrorException("Error extracting image for {0}", ex, item.Name);
- }
- }
- SetLastRefreshed(item, DateTime.UtcNow);
- return true;
- }
- /// <summary>
- /// Creates the images for song.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <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)
- {
- 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 (!File.Exists(path))
- {
- var semaphore = GetLock(path);
- // Acquire a lock
- await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
- // Check again
- if (!File.Exists(path))
- {
- try
- {
- var parentPath = Path.GetDirectoryName(path);
- if (!Directory.Exists(parentPath))
- {
- Directory.CreateDirectory(parentPath);
- }
- await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
- }
- finally
- {
- semaphore.Release();
- }
- }
- else
- {
- semaphore.Release();
- }
- }
- // Image is already in the cache
- item.PrimaryImagePath = path;
- await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
- }
- /// <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));
- }
- }
- }
|