jellyfin/MediaBrowser.Providers/MediaInfo/AudioImageProvider.cs

139 lines
4.4 KiB
C#
Raw Normal View History

2016-01-11 09:52:22 -07:00
using System;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
2014-02-20 09:37:41 -07:00
using MediaBrowser.Controller.MediaEncoding;
2013-06-09 09:47:28 -07:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Extensions;
2013-06-09 09:47:28 -07:00
namespace MediaBrowser.Providers.MediaInfo
{
/// <summary>
/// Uses ffmpeg to create video images
/// </summary>
2017-10-13 12:22:24 -07: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;
}
2017-08-07 14:06:13 -07:00
public IEnumerable<ImageType> GetSupportedImages(IHasMetadata item)
{
return new List<ImageType> { ImageType.Primary };
}
2017-08-07 14:06:13 -07:00
public Task<DynamicImageResponse> GetImage(IHasMetadata item, ImageType type, CancellationToken cancellationToken)
{
var audio = (Audio)item;
2016-01-11 09:52:22 -07:00
var imageStreams =
2017-08-05 12:02:33 -07:00
audio.GetMediaStreams(MediaStreamType.EmbeddedImage)
2016-01-11 09:52:22 -07: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 09:52:22 -07:00
if (imageStreams.Count == 0)
{
return Task.FromResult(new DynamicImageResponse { HasImage = false });
}
2016-01-11 09:52:22 -07:00
return GetImage((Audio)item, imageStreams, cancellationToken);
}
2016-01-11 09:52:22 -07:00
public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
{
var path = GetAudioImagePath(item);
2016-01-11 09:52:22 -07:00
if (!_fileSystem.FileExists(path))
{
2017-05-04 11:14:45 -07:00
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
2016-01-11 09:52:22 -07:00
2016-10-30 21:28:23 -07: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 16:54:33 -07:00
2016-10-30 21:28:23 -07:00
var imageStreamIndex = imageStream == null ? (int?)null : imageStream.Index;
2016-10-30 21:28:23 -07:00
var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
2016-10-30 21:28:23 -07:00
_fileSystem.CopyFile(tempFile, path, true);
2016-10-30 21:28:23 -07:00
try
{
_fileSystem.DeleteFile(tempFile);
2014-02-06 16:54:33 -07:00
}
2016-10-30 21:28:23 -07:00
catch
2014-02-06 16:54:33 -07:00
{
2016-10-30 21:28:23 -07:00
}
}
return new DynamicImageResponse
{
HasImage = true,
Path = path
};
}
private string GetAudioImagePath(Audio item)
{
var filename = item.Album ?? string.Empty;
2017-08-24 12:52:19 -07:00
filename += string.Join(",", item.Artists);
2016-03-13 18:34:24 -07:00
if (!string.IsNullOrWhiteSpace(item.Album))
{
filename += "_" + item.Album;
}
else if (!string.IsNullOrWhiteSpace(item.Name))
{
filename += "_" + item.Name;
}
else
{
filename += "_" + item.Id.ToString("N");
}
filename = filename.GetMD5() + ".jpg";
var prefix = filename.Substring(0, 1);
return Path.Combine(AudioImagesPath, prefix, filename);
}
public string AudioImagesPath
{
get
{
return Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
}
}
public string Name
{
2014-02-19 11:50:37 -07:00
get { return "Image Extractor"; }
}
2017-08-07 14:06:13 -07:00
public bool Supports(IHasMetadata item)
{
2014-12-29 13:18:48 -07:00
var audio = item as Audio;
2016-10-07 22:57:38 -07:00
return item.LocationType == LocationType.FileSystem && audio != null;
}
}
}