2
0

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 string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
  34. public string Name => "Image Extractor";
  35. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  36. {
  37. return new List<ImageType> { ImageType.Primary };
  38. }
  39. public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
  40. {
  41. var audio = (Audio)item;
  42. var imageStreams =
  43. audio.GetMediaStreams(MediaStreamType.EmbeddedImage)
  44. .Where(i => i.Type == MediaStreamType.EmbeddedImage)
  45. .ToList();
  46. // Can't extract if we didn't find a video stream in the file
  47. if (imageStreams.Count == 0)
  48. {
  49. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  50. }
  51. return GetImage((Audio)item, imageStreams, cancellationToken);
  52. }
  53. public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
  54. {
  55. var path = GetAudioImagePath(item);
  56. if (!File.Exists(path))
  57. {
  58. Directory.CreateDirectory(Path.GetDirectoryName(path));
  59. var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
  60. imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
  61. imageStreams.FirstOrDefault();
  62. var imageStreamIndex = imageStream == null ? (int?)null : imageStream.Index;
  63. var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
  64. File.Copy(tempFile, path, true);
  65. try
  66. {
  67. _fileSystem.DeleteFile(tempFile);
  68. }
  69. catch
  70. {
  71. }
  72. }
  73. return new DynamicImageResponse
  74. {
  75. HasImage = true,
  76. Path = path
  77. };
  78. }
  79. private string GetAudioImagePath(Audio item)
  80. {
  81. string filename;
  82. if (item.GetType() == typeof(Audio))
  83. {
  84. if (item.AlbumArtists.Count > 0
  85. && !string.IsNullOrWhiteSpace(item.Album)
  86. && !string.IsNullOrWhiteSpace(item.AlbumArtists[0]))
  87. {
  88. filename = (item.Album + "-" + item.AlbumArtists[0]).GetMD5().ToString("N", CultureInfo.InvariantCulture);
  89. }
  90. else
  91. {
  92. filename = item.Id.ToString("N", CultureInfo.InvariantCulture);
  93. }
  94. filename += ".jpg";
  95. }
  96. else
  97. {
  98. // If it's an audio book or audio podcast, allow unique image per item
  99. filename = item.Id.ToString("N", CultureInfo.InvariantCulture) + ".jpg";
  100. }
  101. var prefix = filename.AsSpan().Slice(0, 1);
  102. return Path.Join(AudioImagesPath, prefix, filename);
  103. }
  104. public bool Supports(BaseItem item)
  105. {
  106. if (item.IsShortcut)
  107. {
  108. return false;
  109. }
  110. if (!item.IsFileProtocol)
  111. {
  112. return false;
  113. }
  114. var audio = item as Audio;
  115. return audio != null;
  116. }
  117. }
  118. }