jellyfin/MediaBrowser.Providers/MediaInfo/AudioImageProvider.cs

144 lines
4.7 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-01-11 17:52:22 +01:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
2014-02-20 17:37:41 +01:00
using MediaBrowser.Controller.MediaEncoding;
2013-06-09 18:47:28 +02:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
2013-06-09 18:47:28 +02:00
namespace MediaBrowser.Providers.MediaInfo
{
/// <summary>
/// Uses ffmpeg to create video images.
/// </summary>
2017-10-13 21:22:24 +02:00
public class AudioImageProvider : IDynamicImageProvider
{
private readonly IMediaEncoder _mediaEncoder;
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
public AudioImageProvider(IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
{
_mediaEncoder = mediaEncoder;
_config = config;
_fileSystem = fileSystem;
}
2020-09-07 13:20:39 +02:00
public string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
public string Name => "Image Extractor";
2018-09-12 19:26:21 +02:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new List<ImageType> { ImageType.Primary };
}
2018-09-12 19:26:21 +02:00
public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
{
var audio = (Audio)item;
2016-01-11 17:52:22 +01:00
var imageStreams =
2017-08-05 21:02:33 +02:00
audio.GetMediaStreams(MediaStreamType.EmbeddedImage)
2016-01-11 17:52:22 +01:00
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
.ToList();
// Can't extract if we didn't find a video stream in the file
2016-01-11 17:52:22 +01:00
if (imageStreams.Count == 0)
{
return Task.FromResult(new DynamicImageResponse { HasImage = false });
}
2016-01-11 17:52:22 +01:00
return GetImage((Audio)item, imageStreams, cancellationToken);
}
2016-01-11 17:52:22 +01:00
public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
{
var path = GetAudioImagePath(item);
if (!File.Exists(path))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
2016-01-11 17:52:22 +01:00
2016-10-31 05:28:23 +01:00
var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
imageStreams.FirstOrDefault();
2014-02-07 00:54:33 +01:00
2016-10-31 05:28:23 +01:00
var imageStreamIndex = imageStream == null ? (int?)null : imageStream.Index;
2016-10-31 05:28:23 +01:00
var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
2019-01-26 22:31:59 +01:00
File.Copy(tempFile, path, true);
2016-10-31 05:28:23 +01:00
try
{
_fileSystem.DeleteFile(tempFile);
2014-02-07 00:54:33 +01:00
}
2016-10-31 05:28:23 +01:00
catch
2014-02-07 00:54:33 +01:00
{
}
}
return new DynamicImageResponse
{
HasImage = true,
Path = path
};
}
private string GetAudioImagePath(Audio item)
{
string filename;
2016-03-14 02:34:24 +01:00
if (item.GetType() == typeof(Audio))
2016-03-14 02:34:24 +01:00
{
2020-09-07 13:20:39 +02:00
if (item.AlbumArtists.Count > 0
&& !string.IsNullOrWhiteSpace(item.Album)
&& !string.IsNullOrWhiteSpace(item.AlbumArtists[0]))
{
2020-09-07 13:20:39 +02:00
filename = (item.Album + "-" + item.AlbumArtists[0]).GetMD5().ToString("N", CultureInfo.InvariantCulture);
}
else
{
filename = item.Id.ToString("N", CultureInfo.InvariantCulture);
}
2018-09-12 19:26:21 +02:00
filename += ".jpg";
2016-03-14 02:34:24 +01:00
}
else
{
// If it's an audio book or audio podcast, allow unique image per item
filename = item.Id.ToString("N", CultureInfo.InvariantCulture) + ".jpg";
2016-03-14 02:34:24 +01:00
}
var prefix = filename.AsSpan().Slice(0, 1);
return Path.Join(AudioImagesPath, prefix, filename);
}
2018-09-12 19:26:21 +02:00
public bool Supports(BaseItem item)
{
2018-09-12 19:26:21 +02:00
if (item.IsShortcut)
{
return false;
}
2020-06-15 23:43:52 +02:00
2018-09-12 19:26:21 +02:00
if (!item.IsFileProtocol)
{
return false;
}
return item is Audio;
}
}
}