AudioImageProvider.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.Extensions;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Controller.Entities.Audio;
  13. using MediaBrowser.Controller.MediaEncoding;
  14. using MediaBrowser.Controller.Providers;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.IO;
  17. namespace MediaBrowser.Providers.MediaInfo
  18. {
  19. /// <summary>
  20. /// Uses ffmpeg to create video images.
  21. /// </summary>
  22. public class AudioImageProvider : IDynamicImageProvider
  23. {
  24. private readonly IMediaEncoder _mediaEncoder;
  25. private readonly IServerConfigurationManager _config;
  26. private readonly IFileSystem _fileSystem;
  27. public AudioImageProvider(IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
  28. {
  29. _mediaEncoder = mediaEncoder;
  30. _config = config;
  31. _fileSystem = fileSystem;
  32. }
  33. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  34. {
  35. return new List<ImageType> { ImageType.Primary };
  36. }
  37. public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
  38. {
  39. var audio = (Audio)item;
  40. var imageStreams =
  41. audio.GetMediaStreams(MediaStreamType.EmbeddedImage)
  42. .Where(i => i.Type == MediaStreamType.EmbeddedImage)
  43. .ToList();
  44. // Can't extract if we didn't find a video stream in the file
  45. if (imageStreams.Count == 0)
  46. {
  47. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  48. }
  49. return GetImage((Audio)item, imageStreams, cancellationToken);
  50. }
  51. public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
  52. {
  53. var path = GetAudioImagePath(item);
  54. if (!File.Exists(path))
  55. {
  56. Directory.CreateDirectory(Path.GetDirectoryName(path));
  57. var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
  58. imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
  59. imageStreams.FirstOrDefault();
  60. var imageStreamIndex = imageStream == null ? (int?)null : imageStream.Index;
  61. var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
  62. File.Copy(tempFile, path, true);
  63. try
  64. {
  65. _fileSystem.DeleteFile(tempFile);
  66. }
  67. catch
  68. {
  69. }
  70. }
  71. return new DynamicImageResponse
  72. {
  73. HasImage = true,
  74. Path = path
  75. };
  76. }
  77. private string GetAudioImagePath(Audio item)
  78. {
  79. string filename;
  80. if (item.GetType() == typeof(Audio))
  81. {
  82. var albumArtist = item.AlbumArtists.FirstOrDefault();
  83. if (!string.IsNullOrWhiteSpace(item.Album) && !string.IsNullOrWhiteSpace(albumArtist))
  84. {
  85. filename = (item.Album + "-" + albumArtist).GetMD5().ToString("N", CultureInfo.InvariantCulture);
  86. }
  87. else
  88. {
  89. filename = item.Id.ToString("N", CultureInfo.InvariantCulture);
  90. }
  91. filename += ".jpg";
  92. }
  93. else
  94. {
  95. // If it's an audio book or audio podcast, allow unique image per item
  96. filename = item.Id.ToString("N", CultureInfo.InvariantCulture) + ".jpg";
  97. }
  98. var prefix = filename.AsSpan().Slice(0, 1);
  99. return Path.Join(AudioImagesPath, prefix, filename);
  100. }
  101. public string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
  102. public string Name => "Image Extractor";
  103. public bool Supports(BaseItem item)
  104. {
  105. if (item.IsShortcut)
  106. {
  107. return false;
  108. }
  109. if (!item.IsFileProtocol)
  110. {
  111. return false;
  112. }
  113. var audio = item as Audio;
  114. return audio != null;
  115. }
  116. }
  117. }