jellyfin/MediaBrowser.Providers/MediaInfo/AudioImageProvider.cs

185 lines
6.1 KiB
C#
Raw Normal View History

2016-01-11 16:52:22 +00:00
using System;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
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;
using MediaBrowser.Model.Entities;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.MediaInfo
{
/// <summary>
/// Uses ffmpeg to create video images
/// </summary>
2014-09-11 01:57:11 +00:00
public class AudioImageProvider : IDynamicImageProvider, IHasItemChangeMonitor
{
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
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;
}
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
{
return new List<ImageType> { ImageType.Primary };
}
public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
{
var audio = (Audio)item;
2016-01-11 16:52:22 +00:00
var imageStreams =
audio.GetMediaSources(false)
.Take(1)
.SelectMany(i => i.MediaStreams)
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
.ToList();
// 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)
{
return Task.FromResult(new DynamicImageResponse { HasImage = false });
}
2016-01-11 16:52:22 +00:00
return GetImage((Audio)item, imageStreams, cancellationToken);
}
2016-01-11 16:52:22 +00:00
public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
{
var path = GetAudioImagePath(item);
2016-01-11 16:52:22 +00:00
if (!_fileSystem.FileExists(path))
{
2014-02-06 23:54:33 +00:00
var semaphore = GetLock(path);
2014-02-06 23:54:33 +00:00
// Acquire a lock
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
2014-02-06 23:54:33 +00:00
try
{
// Check again in case it was saved while waiting for the lock
2016-01-11 16:52:22 +00:00
if (!_fileSystem.FileExists(path))
{
2016-01-11 16:52:22 +00:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(path));
var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
2016-04-13 20:49:16 +00:00
imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
imageStreams.FirstOrDefault();
2016-01-11 16:52:22 +00:00
var imageStreamIndex = imageStream == null ? (int?)null : imageStream.Index;
2014-02-06 23:54:33 +00:00
var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
_fileSystem.CopyFile(tempFile, path, true);
try
{
_fileSystem.DeleteFile(tempFile);
}
catch
{
}
}
2014-02-06 23:54:33 +00:00
}
finally
{
semaphore.Release();
}
}
return new DynamicImageResponse
{
HasImage = true,
Path = path
};
}
private string GetAudioImagePath(Audio item)
{
var filename = item.Album ?? string.Empty;
2015-03-28 20:22:27 +00:00
filename += string.Join(",", item.Artists.ToArray());
2016-03-14 01:34:24 +00: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");
}
}
/// <summary>
/// Gets the lock.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>SemaphoreSlim.</returns>
private SemaphoreSlim GetLock(string filename)
{
return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
}
public string Name
{
2014-02-19 18:50:37 +00:00
get { return "Image Extractor"; }
}
public bool Supports(IHasImages item)
{
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;
}
2016-04-08 18:32:38 +00:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
{
2016-08-24 06:13:15 +00:00
if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
2014-09-11 01:57:11 +00:00
{
2016-08-24 06:13:15 +00:00
var file = directoryService.GetFile(item.Path);
if (file != null && file.LastWriteTimeUtc != item.DateModified)
{
return true;
}
2014-09-11 01:57:11 +00:00
}
return false;
}
}
}