2021-10-26 13:49:01 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 22:22:57 +00:00
|
|
|
using System.Globalization;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2016-01-11 16:52:22 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-02-06 23:52:59 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-04-15 15:10:12 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2021-11-18 14:04:30 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 16:37:41 +00:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2021-11-18 14:04:30 +00:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
2013-06-09 16:47:28 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-04-15 15:10:12 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-25 19:02:04 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2021-11-18 14:04:30 +00:00
|
|
|
/// Uses <see cref="IMediaEncoder"/> to extract embedded images.
|
2013-04-15 15:10:12 +00:00
|
|
|
/// </summary>
|
2017-10-13 19:22:24 +00:00
|
|
|
public class AudioImageProvider : IDynamicImageProvider
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
2021-11-18 14:04:30 +00:00
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2013-05-21 04:04:38 +00:00
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2014-02-06 04:39:16 +00:00
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-02-06 23:52:59 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="AudioImageProvider"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mediaSourceManager">The media source manager for fetching item streams.</param>
|
|
|
|
/// <param name="mediaEncoder">The media encoder for extracting embedded images.</param>
|
|
|
|
/// <param name="config">The server configuration manager for getting image paths.</param>
|
|
|
|
/// <param name="fileSystem">The filesystem.</param>
|
|
|
|
public AudioImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
2021-11-18 14:04:30 +00:00
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2013-05-21 04:04:38 +00:00
|
|
|
_mediaEncoder = mediaEncoder;
|
2014-02-06 04:39:16 +00:00
|
|
|
_config = config;
|
2014-02-06 23:52:59 +00:00
|
|
|
_fileSystem = fileSystem;
|
2013-04-15 15:10:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
private string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
|
2020-09-07 11:20:39 +00:00
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
/// <inheritdoc />
|
2020-09-07 11:20:39 +00:00
|
|
|
public string Name => "Image Extractor";
|
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
2021-11-18 14:04:30 +00:00
|
|
|
return new[] { ImageType.Primary };
|
2013-06-25 01:22:21 +00:00
|
|
|
}
|
2013-12-15 16:53:32 +00:00
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
2021-11-18 14:04:30 +00:00
|
|
|
var imageStreams = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery
|
|
|
|
{
|
|
|
|
ItemId = item.Id,
|
|
|
|
Type = MediaStreamType.EmbeddedImage
|
|
|
|
});
|
2016-01-11 16:52:22 +00:00
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
// Can't extract if we didn't find a video stream in the file
|
2016-01-11 16:52:22 +00:00
|
|
|
if (imageStreams.Count == 0)
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
2013-04-15 15:10:12 +00:00
|
|
|
}
|
2013-05-21 04:04:38 +00:00
|
|
|
|
2016-01-11 16:52:22 +00:00
|
|
|
return GetImage((Audio)item, imageStreams, cancellationToken);
|
2013-04-15 15:10:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
private async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
2014-02-06 23:52:59 +00:00
|
|
|
var path = GetAudioImagePath(item);
|
|
|
|
|
2019-01-26 21:59:53 +00:00
|
|
|
if (!File.Exists(path))
|
2014-02-06 23:52:59 +00:00
|
|
|
{
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
2016-01-11 16:52:22 +00:00
|
|
|
|
2023-10-23 22:10:31 +00:00
|
|
|
var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).Contains("front", StringComparison.OrdinalIgnoreCase)) ??
|
|
|
|
imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).Contains("cover", StringComparison.OrdinalIgnoreCase)) ??
|
2016-10-31 04:28:23 +00:00
|
|
|
imageStreams.FirstOrDefault();
|
2014-02-06 23:54:33 +00:00
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
var imageStreamIndex = imageStream?.Index;
|
2016-07-01 02:35:18 +00:00
|
|
|
|
2016-10-31 04:28:23 +00:00
|
|
|
var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
|
2016-07-01 02:35:18 +00:00
|
|
|
|
2019-01-26 21:31:59 +00:00
|
|
|
File.Copy(tempFile, path, true);
|
2016-07-01 02:35:18 +00:00
|
|
|
|
2016-10-31 04:28:23 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_fileSystem.DeleteFile(tempFile);
|
2014-02-06 23:54:33 +00:00
|
|
|
}
|
2016-10-31 04:28:23 +00:00
|
|
|
catch
|
2014-02-06 23:54:33 +00:00
|
|
|
{
|
2014-02-06 23:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-21 04:04:38 +00:00
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
return new DynamicImageResponse
|
2013-04-15 15:10:12 +00:00
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
HasImage = true,
|
2014-02-06 23:52:59 +00:00
|
|
|
Path = path
|
2014-02-06 04:39:16 +00:00
|
|
|
};
|
2013-05-21 04:04:38 +00:00
|
|
|
}
|
2013-04-15 15:10:12 +00:00
|
|
|
|
2014-02-06 23:52:59 +00:00
|
|
|
private string GetAudioImagePath(Audio item)
|
|
|
|
{
|
2020-07-29 11:17:01 +00:00
|
|
|
string filename;
|
2016-03-14 01:34:24 +00:00
|
|
|
|
2017-11-02 16:00:58 +00:00
|
|
|
if (item.GetType() == typeof(Audio))
|
2016-03-14 01:34:24 +00:00
|
|
|
{
|
2020-09-07 11:20:39 +00:00
|
|
|
if (item.AlbumArtists.Count > 0
|
|
|
|
&& !string.IsNullOrWhiteSpace(item.Album)
|
|
|
|
&& !string.IsNullOrWhiteSpace(item.AlbumArtists[0]))
|
2017-11-02 16:00:58 +00:00
|
|
|
{
|
2020-09-07 11:20:39 +00:00
|
|
|
filename = (item.Album + "-" + item.AlbumArtists[0]).GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
2017-11-02 16:00:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-28 22:22:57 +00:00
|
|
|
filename = item.Id.ToString("N", CultureInfo.InvariantCulture);
|
2017-11-02 16:00:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
filename += ".jpg";
|
2016-03-14 01:34:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-02 16:00:58 +00:00
|
|
|
// If it's an audio book or audio podcast, allow unique image per item
|
2019-02-28 22:22:57 +00:00
|
|
|
filename = item.Id.ToString("N", CultureInfo.InvariantCulture) + ".jpg";
|
2016-03-14 01:34:24 +00:00
|
|
|
}
|
2014-02-06 23:52:59 +00:00
|
|
|
|
2020-07-29 11:17:01 +00:00
|
|
|
var prefix = filename.AsSpan().Slice(0, 1);
|
2014-02-06 23:52:59 +00:00
|
|
|
|
2020-07-29 11:17:01 +00:00
|
|
|
return Path.Join(AudioImagesPath, prefix, filename);
|
2014-02-06 23:52:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:04:30 +00:00
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-12-15 16:53:32 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (item.IsShortcut)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!item.IsFileProtocol)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-05 11:37:36 +00:00
|
|
|
return item is Audio;
|
2013-12-15 16:53:32 +00:00
|
|
|
}
|
2013-04-15 15:10:12 +00:00
|
|
|
}
|
|
|
|
}
|