2016-01-11 09:52:22 -07:00
|
|
|
|
using System;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-02-06 16:52:59 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-04-15 08:10:12 -07:00
|
|
|
|
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;
|
2013-04-15 08:10:12 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-05 21:39:16 -07:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-06 16:52:59 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-04-15 08:10:12 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-04-15 08:10:12 -07:00
|
|
|
|
|
2013-06-09 09:47:28 -07:00
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses ffmpeg to create video images
|
|
|
|
|
/// </summary>
|
2014-09-10 18:57:11 -07:00
|
|
|
|
public class AudioImageProvider : IDynamicImageProvider, IHasItemChangeMonitor
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
2013-05-20 21:04:38 -07:00
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2014-02-05 21:39:16 -07:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-02-06 16:52:59 -07:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-20 21:04:38 -07:00
|
|
|
|
|
2014-06-30 10:40:46 -07:00
|
|
|
|
public AudioImageProvider(IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
2013-05-20 21:04:38 -07:00
|
|
|
|
_mediaEncoder = mediaEncoder;
|
2014-02-05 21:39:16 -07:00
|
|
|
|
_config = config;
|
2014-02-06 16:52:59 -07:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-04-15 08:10:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 21:39:16 -07:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
2014-02-05 21:39:16 -07:00
|
|
|
|
return new List<ImageType> { ImageType.Primary };
|
2013-06-24 18:22:21 -07:00
|
|
|
|
}
|
2013-12-15 09:53:32 -07:00
|
|
|
|
|
2014-02-05 21:39:16 -07:00
|
|
|
|
public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
2013-05-20 21:04:38 -07:00
|
|
|
|
var audio = (Audio)item;
|
|
|
|
|
|
2016-01-11 09:52:22 -07:00
|
|
|
|
var imageStreams =
|
|
|
|
|
audio.GetMediaSources(false)
|
|
|
|
|
.Take(1)
|
|
|
|
|
.SelectMany(i => i.MediaStreams)
|
|
|
|
|
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-02-05 21:39:16 -07:00
|
|
|
|
// 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)
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
2014-02-05 21:39:16 -07:00
|
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
2013-04-15 08:10:12 -07:00
|
|
|
|
}
|
2013-05-20 21:04:38 -07:00
|
|
|
|
|
2016-01-11 09:52:22 -07:00
|
|
|
|
return GetImage((Audio)item, imageStreams, cancellationToken);
|
2013-04-15 08:10:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 09:52:22 -07:00
|
|
|
|
public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
2014-02-06 16:52:59 -07:00
|
|
|
|
var path = GetAudioImagePath(item);
|
|
|
|
|
|
2016-01-11 09:52:22 -07:00
|
|
|
|
if (!_fileSystem.FileExists(path))
|
2014-02-06 16:52:59 -07:00
|
|
|
|
{
|
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-06-30 19:35:18 -07:00
|
|
|
|
|
2016-10-30 21:28:23 -07:00
|
|
|
|
var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
|
2016-06-30 19:35:18 -07:00
|
|
|
|
|
2016-10-30 21:28:23 -07:00
|
|
|
|
_fileSystem.CopyFile(tempFile, path, true);
|
2016-06-30 19:35:18 -07:00
|
|
|
|
|
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
|
|
|
|
|
2014-02-06 16:52:59 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-20 21:04:38 -07:00
|
|
|
|
|
2014-02-05 21:39:16 -07:00
|
|
|
|
return new DynamicImageResponse
|
2013-04-15 08:10:12 -07:00
|
|
|
|
{
|
2014-02-05 21:39:16 -07:00
|
|
|
|
HasImage = true,
|
2014-02-06 16:52:59 -07:00
|
|
|
|
Path = path
|
2014-02-05 21:39:16 -07:00
|
|
|
|
};
|
2013-05-20 21:04:38 -07:00
|
|
|
|
}
|
2013-04-15 08:10:12 -07:00
|
|
|
|
|
2014-02-06 16:52:59 -07:00
|
|
|
|
private string GetAudioImagePath(Audio item)
|
|
|
|
|
{
|
|
|
|
|
var filename = item.Album ?? string.Empty;
|
2015-03-28 13:22:27 -07:00
|
|
|
|
filename += string.Join(",", item.Artists.ToArray());
|
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");
|
|
|
|
|
}
|
2014-02-06 16:52:59 -07:00
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 21:39:16 -07:00
|
|
|
|
public string Name
|
2013-12-15 09:53:32 -07:00
|
|
|
|
{
|
2014-02-19 11:50:37 -07:00
|
|
|
|
get { return "Image Extractor"; }
|
2013-12-15 09:53:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 21:39:16 -07:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-12-15 09:53:32 -07:00
|
|
|
|
{
|
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;
|
2013-12-15 09:53:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 11:32:38 -07:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
|
2013-05-20 21:04:38 -07:00
|
|
|
|
{
|
2016-08-23 23:13:15 -07:00
|
|
|
|
if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
|
2014-09-10 18:57:11 -07:00
|
|
|
|
{
|
2016-08-23 23:13:15 -07:00
|
|
|
|
var file = directoryService.GetFile(item.Path);
|
|
|
|
|
if (file != null && file.LastWriteTimeUtc != item.DateModified)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-09-10 18:57:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2013-04-15 08:10:12 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|