AudioImageProvider.cs 4.6 KB

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