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