AudioImageProvider.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using MediaBrowser.Common.Extensions;
  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.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Extensions;
  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(IHasMetadata item)
  33. {
  34. return new List<ImageType> { ImageType.Primary };
  35. }
  36. public Task<DynamicImageResponse> GetImage(IHasMetadata 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 (!_fileSystem.FileExists(path))
  54. {
  55. _fileSystem.CreateDirectory(_fileSystem.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. _fileSystem.CopyFile(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;
  79. if (item.GetType() == typeof(Audio))
  80. {
  81. filename = item.Album ?? string.Empty;
  82. filename += string.Join(",", item.Artists);
  83. if (!string.IsNullOrWhiteSpace(item.Album))
  84. {
  85. filename += "_" + item.Album;
  86. }
  87. else if (!string.IsNullOrWhiteSpace(item.Name))
  88. {
  89. filename += "_" + item.Name;
  90. }
  91. else
  92. {
  93. filename += "_" + item.Id.ToString("N");
  94. }
  95. filename = filename.GetMD5() + ".jpg";
  96. }
  97. else
  98. {
  99. // If it's an audio book or audio podcast, allow unique image per item
  100. filename = item.Id.ToString("N") + ".jpg";
  101. }
  102. var prefix = filename.Substring(0, 1);
  103. return Path.Combine(AudioImagesPath, prefix, filename);
  104. }
  105. public string AudioImagesPath
  106. {
  107. get
  108. {
  109. return Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
  110. }
  111. }
  112. public string Name
  113. {
  114. get { return "Image Extractor"; }
  115. }
  116. public bool Supports(IHasMetadata item)
  117. {
  118. var audio = item as Audio;
  119. return item.LocationType == LocationType.FileSystem && audio != null;
  120. }
  121. }
  122. }