123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using MediaBrowser.Common.Extensions;
- using MediaBrowser.Common.IO;
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Entities.Audio;
- using MediaBrowser.Controller.MediaInfo;
- using MediaBrowser.Controller.Providers;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.IO;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- 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 : IDynamicImageProvider, IHasChangeMonitor
- {
- private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
- private readonly IIsoManager _isoManager;
- private readonly IMediaEncoder _mediaEncoder;
- private readonly IServerConfigurationManager _config;
- private readonly IFileSystem _fileSystem;
- public AudioImageProvider(IIsoManager isoManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
- {
- _isoManager = isoManager;
- _mediaEncoder = mediaEncoder;
- _config = config;
- _fileSystem = fileSystem;
- }
- /// <summary>
- /// The null mount task result
- /// </summary>
- protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
- /// <summary>
- /// Mounts the iso if needed.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task{IIsoMount}.</returns>
- protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
- {
- if (item.VideoType == VideoType.Iso)
- {
- return _isoManager.Mount(item.Path, cancellationToken);
- }
- return NullMountTaskResult;
- }
- public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
- {
- return new List<ImageType> { ImageType.Primary };
- }
- public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
- {
- var audio = (Audio)item;
- // Can't extract if we didn't find a video stream in the file
- if (!audio.HasEmbeddedImage)
- {
- return Task.FromResult(new DynamicImageResponse { HasImage = false });
- }
- return GetImage((Audio)item, cancellationToken);
- }
- public async Task<DynamicImageResponse> GetImage(Audio item, CancellationToken cancellationToken)
- {
- var path = GetAudioImagePath(item);
- if (!File.Exists(path))
- {
- using (var stream = await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.File, true, null, null, cancellationToken).ConfigureAwait(false))
- {
- var semaphore = GetLock(path);
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- // Acquire a lock
- await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
- try
- {
- using (var fileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
- {
- await stream.CopyToAsync(fileStream).ConfigureAwait(false);
- }
- }
- finally
- {
- semaphore.Release();
- }
- }
- }
- return new DynamicImageResponse
- {
- HasImage = true,
- Path = path
- };
- }
- private string GetAudioImagePath(Audio item)
- {
- var album = item.Parent as MusicAlbum;
- var filename = item.Album ?? string.Empty;
- filename += item.Artists.FirstOrDefault() ?? string.Empty;
- filename += album == null ? item.Id.ToString("N") + "_primary" + item.DateModified.Ticks : album.Id.ToString("N") + album.DateModified.Ticks + "_primary";
- filename = filename.GetMD5() + ".jpg";
- var prefix = filename.Substring(0, 1);
- return Path.Combine(AudioImagesPath, prefix, filename);
- }
- public string AudioImagesPath
- {
- get
- {
- return Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
- }
- }
- /// <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));
- }
- public string Name
- {
- get { return "Embedded Image"; }
- }
- public bool Supports(IHasImages item)
- {
- return item.LocationType == LocationType.FileSystem && item is Audio;
- }
- public bool HasChanged(IHasMetadata item, DateTime date)
- {
- return item.DateModified > date;
- }
- }
- }
|