AudioImageProvider.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace MediaBrowser.Providers.MediaInfo
  16. {
  17. /// <summary>
  18. /// Uses ffmpeg to create video images
  19. /// </summary>
  20. public class AudioImageProvider : IDynamicImageProvider, IHasItemChangeMonitor
  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(IHasImages item)
  32. {
  33. return new List<ImageType> { ImageType.Primary };
  34. }
  35. public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
  36. {
  37. var audio = (Audio)item;
  38. var imageStreams =
  39. audio.GetMediaSources(false)
  40. .Take(1)
  41. .SelectMany(i => i.MediaStreams)
  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 (!_fileSystem.FileExists(path))
  55. {
  56. _fileSystem.CreateDirectory(_fileSystem.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. _fileSystem.CopyFile(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. var filename = item.Album ?? string.Empty;
  80. filename += string.Join(",", item.Artists.ToArray());
  81. if (!string.IsNullOrWhiteSpace(item.Album))
  82. {
  83. filename += "_" + item.Album;
  84. }
  85. else if (!string.IsNullOrWhiteSpace(item.Name))
  86. {
  87. filename += "_" + item.Name;
  88. }
  89. else
  90. {
  91. filename += "_" + item.Id.ToString("N");
  92. }
  93. filename = filename.GetMD5() + ".jpg";
  94. var prefix = filename.Substring(0, 1);
  95. return Path.Combine(AudioImagesPath, prefix, filename);
  96. }
  97. public string AudioImagesPath
  98. {
  99. get
  100. {
  101. return Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
  102. }
  103. }
  104. public string Name
  105. {
  106. get { return "Image Extractor"; }
  107. }
  108. public bool Supports(IHasImages item)
  109. {
  110. var audio = item as Audio;
  111. return item.LocationType == LocationType.FileSystem && audio != null;
  112. }
  113. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  114. {
  115. if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
  116. {
  117. var file = directoryService.GetFile(item.Path);
  118. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  119. {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. }
  126. }