| | | 1 | | #pragma warning disable CA1826 // CA1826 Do not use Enumerable methods on Indexable collections. |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using MediaBrowser.Common.Extensions; |
| | | 11 | | using MediaBrowser.Controller.Configuration; |
| | | 12 | | using MediaBrowser.Controller.Entities; |
| | | 13 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 14 | | using MediaBrowser.Controller.Library; |
| | | 15 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 16 | | using MediaBrowser.Controller.Persistence; |
| | | 17 | | using MediaBrowser.Controller.Providers; |
| | | 18 | | using MediaBrowser.Model.Entities; |
| | | 19 | | using MediaBrowser.Model.IO; |
| | | 20 | | |
| | | 21 | | namespace MediaBrowser.Providers.MediaInfo |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Uses <see cref="IMediaEncoder"/> to extract embedded images. |
| | | 25 | | /// </summary> |
| | | 26 | | public class AudioImageProvider : IDynamicImageProvider |
| | | 27 | | { |
| | | 28 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | | 29 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 30 | | private readonly IServerConfigurationManager _config; |
| | | 31 | | private readonly IFileSystem _fileSystem; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="AudioImageProvider"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="mediaSourceManager">The media source manager for fetching item streams.</param> |
| | | 37 | | /// <param name="mediaEncoder">The media encoder for extracting embedded images.</param> |
| | | 38 | | /// <param name="config">The server configuration manager for getting image paths.</param> |
| | | 39 | | /// <param name="fileSystem">The filesystem.</param> |
| | | 40 | | public AudioImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, IServerConfigurati |
| | | 41 | | { |
| | 21 | 42 | | _mediaSourceManager = mediaSourceManager; |
| | 21 | 43 | | _mediaEncoder = mediaEncoder; |
| | 21 | 44 | | _config = config; |
| | 21 | 45 | | _fileSystem = fileSystem; |
| | 21 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | private string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images"); |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | 0 | 51 | | public string Name => "Image Extractor"; |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public IEnumerable<ImageType> GetSupportedImages(BaseItem item) |
| | | 55 | | { |
| | 0 | 56 | | return new[] { ImageType.Primary }; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken) |
| | | 61 | | { |
| | 0 | 62 | | var imageStreams = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery |
| | 0 | 63 | | { |
| | 0 | 64 | | ItemId = item.Id, |
| | 0 | 65 | | Type = MediaStreamType.EmbeddedImage |
| | 0 | 66 | | }); |
| | | 67 | | |
| | | 68 | | // Can't extract if we didn't find a video stream in the file |
| | 0 | 69 | | if (imageStreams.Count == 0) |
| | | 70 | | { |
| | 0 | 71 | | return Task.FromResult(new DynamicImageResponse { HasImage = false }); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | return GetImage((Audio)item, imageStreams, cancellationToken); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | private async Task<DynamicImageResponse> GetImage(Audio item, IReadOnlyList<MediaStream> imageStreams, Cancellat |
| | | 78 | | { |
| | | 79 | | var path = GetAudioImagePath(item); |
| | | 80 | | |
| | | 81 | | if (!File.Exists(path)) |
| | | 82 | | { |
| | | 83 | | var directoryName = Path.GetDirectoryName(path) ?? throw new InvalidOperationException($"Invalid path '{ |
| | | 84 | | Directory.CreateDirectory(directoryName); |
| | | 85 | | var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).Contains("front", StringC |
| | | 86 | | imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).Contains("cover", StringComparison.Ordi |
| | | 87 | | imageStreams.FirstOrDefault(); |
| | | 88 | | var imageStreamIndex = imageStream?.Index; |
| | | 89 | | |
| | | 90 | | var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).Con |
| | | 91 | | |
| | | 92 | | File.Copy(tempFile, path, true); |
| | | 93 | | |
| | | 94 | | try |
| | | 95 | | { |
| | | 96 | | _fileSystem.DeleteFile(tempFile); |
| | | 97 | | } |
| | | 98 | | catch |
| | | 99 | | { |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | return new DynamicImageResponse |
| | | 104 | | { |
| | | 105 | | HasImage = true, |
| | | 106 | | Path = path |
| | | 107 | | }; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | private string GetAudioImagePath(Audio item) |
| | | 111 | | { |
| | | 112 | | string filename; |
| | | 113 | | |
| | 0 | 114 | | if (item.GetType() == typeof(Audio)) |
| | | 115 | | { |
| | 0 | 116 | | if (item.AlbumArtists.Count > 0 |
| | 0 | 117 | | && !string.IsNullOrWhiteSpace(item.Album) |
| | 0 | 118 | | && !string.IsNullOrWhiteSpace(item.AlbumArtists[0])) |
| | | 119 | | { |
| | 0 | 120 | | filename = (item.Album + "-" + item.AlbumArtists[0]).GetMD5().ToString("N", CultureInfo.InvariantCul |
| | | 121 | | } |
| | | 122 | | else |
| | | 123 | | { |
| | 0 | 124 | | filename = item.Id.ToString("N", CultureInfo.InvariantCulture); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | filename += ".jpg"; |
| | | 128 | | } |
| | | 129 | | else |
| | | 130 | | { |
| | | 131 | | // If it's an audio book or audio podcast, allow unique image per item |
| | 0 | 132 | | filename = item.Id.ToString("N", CultureInfo.InvariantCulture) + ".jpg"; |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | var prefix = filename.AsSpan().Slice(0, 1); |
| | | 136 | | |
| | 0 | 137 | | return Path.Join(AudioImagesPath, prefix, filename); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <inheritdoc /> |
| | | 141 | | public bool Supports(BaseItem item) |
| | | 142 | | { |
| | 55 | 143 | | if (item.IsShortcut) |
| | | 144 | | { |
| | 0 | 145 | | return false; |
| | | 146 | | } |
| | | 147 | | |
| | 55 | 148 | | if (!item.IsFileProtocol) |
| | | 149 | | { |
| | 0 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | 55 | 153 | | return item is Audio; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |