AudioImageProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Controller.Providers;
  8. using MediaBrowser.Model.Entities;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Providers.MediaInfo
  16. {
  17. /// <summary>
  18. /// Uses ffmpeg to create video images
  19. /// </summary>
  20. public class AudioImageProvider : IDynamicImageProvider, IHasItemChangeMonitor
  21. {
  22. private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
  23. private readonly IMediaEncoder _mediaEncoder;
  24. private readonly IServerConfigurationManager _config;
  25. private readonly IFileSystem _fileSystem;
  26. public AudioImageProvider(IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
  27. {
  28. _mediaEncoder = mediaEncoder;
  29. _config = config;
  30. _fileSystem = fileSystem;
  31. }
  32. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  33. {
  34. return new List<ImageType> { ImageType.Primary };
  35. }
  36. public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
  37. {
  38. var audio = (Audio)item;
  39. // Can't extract if we didn't find a video stream in the file
  40. if (!audio.HasEmbeddedImage)
  41. {
  42. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  43. }
  44. return GetImage((Audio)item, cancellationToken);
  45. }
  46. public async Task<DynamicImageResponse> GetImage(Audio item, CancellationToken cancellationToken)
  47. {
  48. var path = GetAudioImagePath(item);
  49. if (!File.Exists(path))
  50. {
  51. var semaphore = GetLock(path);
  52. // Acquire a lock
  53. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  54. try
  55. {
  56. // Check again in case it was saved while waiting for the lock
  57. if (!File.Exists(path))
  58. {
  59. Directory.CreateDirectory(Path.GetDirectoryName(path));
  60. using (var stream = await _mediaEncoder.ExtractAudioImage(item.Path, cancellationToken).ConfigureAwait(false))
  61. {
  62. using (var fileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  63. {
  64. await stream.CopyToAsync(fileStream).ConfigureAwait(false);
  65. }
  66. }
  67. }
  68. }
  69. finally
  70. {
  71. semaphore.Release();
  72. }
  73. }
  74. return new DynamicImageResponse
  75. {
  76. HasImage = true,
  77. Path = path
  78. };
  79. }
  80. private string GetAudioImagePath(Audio item)
  81. {
  82. var album = item.Parent as MusicAlbum;
  83. var filename = item.Album ?? string.Empty;
  84. filename += string.Join(",", item.Artists.ToArray());
  85. filename += album == null ? item.Id.ToString("N") + "_primary" + item.DateModified.Ticks : album.Id.ToString("N") + album.DateModified.Ticks + "_primary";
  86. filename = filename.GetMD5() + ".jpg";
  87. var prefix = filename.Substring(0, 1);
  88. return Path.Combine(AudioImagesPath, prefix, filename);
  89. }
  90. public string AudioImagesPath
  91. {
  92. get
  93. {
  94. return Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
  95. }
  96. }
  97. /// <summary>
  98. /// Gets the lock.
  99. /// </summary>
  100. /// <param name="filename">The filename.</param>
  101. /// <returns>SemaphoreSlim.</returns>
  102. private SemaphoreSlim GetLock(string filename)
  103. {
  104. return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  105. }
  106. public string Name
  107. {
  108. get { return "Image Extractor"; }
  109. }
  110. public bool Supports(IHasImages item)
  111. {
  112. var audio = item as Audio;
  113. return item.LocationType == LocationType.FileSystem && audio != null && !audio.IsArchive;
  114. }
  115. public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
  116. {
  117. if (status.ItemDateModified.HasValue)
  118. {
  119. if (status.ItemDateModified.Value != item.DateModified)
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. }
  127. }