2016-01-11 16:52:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
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;
|
2014-02-20 16:37:41 +00:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-06 23:52:59 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2017-08-09 19:56:38 +00:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
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>
|
|
|
|
|
/// Uses ffmpeg to create video images
|
|
|
|
|
/// </summary>
|
2017-10-13 19:22:24 +00:00
|
|
|
|
public class AudioImageProvider : IDynamicImageProvider
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2014-06-30 17:40:46 +00:00
|
|
|
|
public AudioImageProvider(IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 21:06:13 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasMetadata item)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return new List<ImageType> { ImageType.Primary };
|
2013-06-25 01:22:21 +00:00
|
|
|
|
}
|
2013-12-15 16:53:32 +00:00
|
|
|
|
|
2017-08-07 21:06:13 +00:00
|
|
|
|
public Task<DynamicImageResponse> GetImage(IHasMetadata item, ImageType type, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
var audio = (Audio)item;
|
|
|
|
|
|
2016-01-11 16:52:22 +00:00
|
|
|
|
var imageStreams =
|
2017-08-05 19:02:33 +00:00
|
|
|
|
audio.GetMediaStreams(MediaStreamType.EmbeddedImage)
|
2016-01-11 16:52:22 +00:00
|
|
|
|
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 16:52:22 +00:00
|
|
|
|
public 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);
|
|
|
|
|
|
2016-01-11 16:52:22 +00:00
|
|
|
|
if (!_fileSystem.FileExists(path))
|
2014-02-06 23:52:59 +00:00
|
|
|
|
{
|
2017-05-04 18:14:45 +00:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
|
2016-01-11 16:52:22 +00:00
|
|
|
|
|
2016-10-31 04:28:23 +00: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 23:54:33 +00:00
|
|
|
|
|
2016-10-31 04:28:23 +00:00
|
|
|
|
var imageStreamIndex = imageStream == null ? (int?)null : 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
|
|
|
|
|
2016-10-31 04:28:23 +00:00
|
|
|
|
_fileSystem.CopyFile(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
|
|
|
|
{
|
2016-10-31 04:28:23 +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)
|
|
|
|
|
{
|
2017-11-02 16:00:58 +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
|
|
|
|
{
|
2017-11-02 16:00:58 +00:00
|
|
|
|
filename = item.Album ?? string.Empty;
|
|
|
|
|
filename += string.Join(",", item.Artists);
|
|
|
|
|
|
|
|
|
|
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";
|
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
|
|
|
|
|
filename = item.Id.ToString("N") + ".jpg";
|
2016-03-14 01:34:24 +00:00
|
|
|
|
}
|
2014-02-06 23:52:59 +00:00
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public string Name
|
2013-12-15 16:53:32 +00:00
|
|
|
|
{
|
2014-02-19 18:50:37 +00:00
|
|
|
|
get { return "Image Extractor"; }
|
2013-12-15 16:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 21:06:13 +00:00
|
|
|
|
public bool Supports(IHasMetadata item)
|
2013-12-15 16:53:32 +00:00
|
|
|
|
{
|
2014-12-29 20:18:48 +00:00
|
|
|
|
var audio = item as Audio;
|
|
|
|
|
|
2016-10-08 05:57:38 +00:00
|
|
|
|
return item.LocationType == LocationType.FileSystem && audio != null;
|
2013-12-15 16:53:32 +00:00
|
|
|
|
}
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|