AudioImageProvider.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, IHasItemChangeMonitor
  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. var filename = item.Album ?? string.Empty;
  79. filename += string.Join(",", item.Artists);
  80. if (!string.IsNullOrWhiteSpace(item.Album))
  81. {
  82. filename += "_" + item.Album;
  83. }
  84. else if (!string.IsNullOrWhiteSpace(item.Name))
  85. {
  86. filename += "_" + item.Name;
  87. }
  88. else
  89. {
  90. filename += "_" + item.Id.ToString("N");
  91. }
  92. filename = filename.GetMD5() + ".jpg";
  93. var prefix = filename.Substring(0, 1);
  94. return Path.Combine(AudioImagesPath, prefix, filename);
  95. }
  96. public string AudioImagesPath
  97. {
  98. get
  99. {
  100. return Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
  101. }
  102. }
  103. public string Name
  104. {
  105. get { return "Image Extractor"; }
  106. }
  107. public bool Supports(IHasMetadata item)
  108. {
  109. var audio = item as Audio;
  110. return item.LocationType == LocationType.FileSystem && audio != null;
  111. }
  112. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  113. {
  114. if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
  115. {
  116. var file = directoryService.GetFile(item.Path);
  117. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  118. {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. }
  125. }