AudioImageProvider.cs 4.7 KB

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