mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
move audio image extraction back into library scan
This commit is contained in:
parent
f3a7307ebb
commit
710c1073c1
@ -118,16 +118,12 @@ namespace MediaBrowser.Controller.Entities.Audio
|
|||||||
var images = base.Images;
|
var images = base.Images;
|
||||||
string primaryImagePath;
|
string primaryImagePath;
|
||||||
|
|
||||||
if (images == null || !images.TryGetValue(ImageType.Primary, out primaryImagePath))
|
if (!images.TryGetValue(ImageType.Primary, out primaryImagePath))
|
||||||
{
|
{
|
||||||
var image = Children.Select(c => c.PrimaryImagePath).FirstOrDefault(c => !string.IsNullOrEmpty(c));
|
var image = Children.Select(c => c.PrimaryImagePath).FirstOrDefault(c => !string.IsNullOrEmpty(c));
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(image))
|
if (!string.IsNullOrEmpty(image))
|
||||||
{
|
{
|
||||||
if (images == null)
|
|
||||||
{
|
|
||||||
images = new Dictionary<ImageType, string>();
|
|
||||||
}
|
|
||||||
images[ImageType.Primary] = image;
|
images[ImageType.Primary] = image;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
using MediaBrowser.Controller.Configuration;
|
using MediaBrowser.Common.IO;
|
||||||
|
using MediaBrowser.Common.MediaInfo;
|
||||||
|
using MediaBrowser.Controller.Configuration;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Controller.Entities.Audio;
|
using MediaBrowser.Controller.Entities.Audio;
|
||||||
|
using MediaBrowser.Controller.Library;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
using MediaBrowser.Model.Logging;
|
using MediaBrowser.Model.Logging;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -14,14 +19,41 @@ namespace MediaBrowser.Controller.Providers.MediaInfo
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class AudioImageProvider : BaseMetadataProvider
|
public class AudioImageProvider : BaseMetadataProvider
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the image cache.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The image cache.</value>
|
||||||
|
public FileSystemRepository ImageCache { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The _locks
|
||||||
|
/// </summary>
|
||||||
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The _media encoder
|
||||||
|
/// </summary>
|
||||||
|
private readonly IMediaEncoder _mediaEncoder;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The _library manager
|
||||||
|
/// </summary>
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="BaseMetadataProvider" /> class.
|
/// Initializes a new instance of the <see cref="BaseMetadataProvider" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logManager">The log manager.</param>
|
/// <param name="logManager">The log manager.</param>
|
||||||
/// <param name="configurationManager">The configuration manager.</param>
|
/// <param name="configurationManager">The configuration manager.</param>
|
||||||
public AudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
|
public AudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, IMediaEncoder mediaEncoder)
|
||||||
: base(logManager, configurationManager)
|
: base(logManager, configurationManager)
|
||||||
{
|
{
|
||||||
|
_libraryManager = libraryManager;
|
||||||
|
_mediaEncoder = mediaEncoder;
|
||||||
|
|
||||||
|
ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.AudioImagesDataPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -54,21 +86,6 @@ namespace MediaBrowser.Controller.Providers.MediaInfo
|
|||||||
get { return MetadataProviderPriority.Last; }
|
get { return MetadataProviderPriority.Last; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Needses the refresh internal.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item.</param>
|
|
||||||
/// <param name="providerInfo">The provider info.</param>
|
|
||||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
||||||
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(item.PrimaryImagePath))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return base.NeedsRefreshInternal(item, providerInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
|
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -76,21 +93,84 @@ namespace MediaBrowser.Controller.Providers.MediaInfo
|
|||||||
/// <param name="force">if set to <c>true</c> [force].</param>
|
/// <param name="force">if set to <c>true</c> [force].</param>
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
/// <returns>Task{System.Boolean}.</returns>
|
/// <returns>Task{System.Boolean}.</returns>
|
||||||
public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
|
var audio = (Audio)item;
|
||||||
{
|
|
||||||
var album = item.ResolveArgs.Parent as MusicAlbum;
|
|
||||||
|
|
||||||
if (album != null)
|
if (string.IsNullOrEmpty(audio.PrimaryImagePath) && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
|
||||||
{
|
{
|
||||||
// First try to use the parent's image
|
try
|
||||||
item.PrimaryImagePath = item.ResolveArgs.Parent.PrimaryImagePath;
|
{
|
||||||
|
await CreateImagesForSong(audio, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.ErrorException("Error extracting image for {0}", ex, item.Name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SetLastRefreshed(item, DateTime.UtcNow);
|
SetLastRefreshed(item, DateTime.UtcNow);
|
||||||
return TrueTaskResult;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates the images for song.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The item.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns>Task.</returns>
|
||||||
|
/// <exception cref="System.InvalidOperationException">Can't extract an image unless the audio file has an embedded image.</exception>
|
||||||
|
private async Task CreateImagesForSong(Audio item, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
var album = item.Parent as MusicAlbum;
|
||||||
|
|
||||||
|
var filename = item.Album ?? string.Empty;
|
||||||
|
filename += item.Artist ?? string.Empty;
|
||||||
|
filename += album == null ? item.Id.ToString("N") + item.DateModified.Ticks : album.Id.ToString("N") + album.DateModified.Ticks;
|
||||||
|
|
||||||
|
var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
|
||||||
|
|
||||||
|
if (!ImageCache.ContainsFilePath(path))
|
||||||
|
{
|
||||||
|
var semaphore = GetLock(path);
|
||||||
|
|
||||||
|
// Acquire a lock
|
||||||
|
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Check again
|
||||||
|
if (!ImageCache.ContainsFilePath(path))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
semaphore.Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Image is already in the cache
|
||||||
|
item.PrimaryImagePath = path;
|
||||||
|
|
||||||
|
await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
semaphore.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,8 +153,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Don't refresh if we already have both poster and backdrop and we're not refreshing images
|
// Don't refresh if we already have both poster and backdrop and we're not refreshing images
|
||||||
if (item.LocationType == LocationType.FileSystem && !ConfigurationManager.Configuration.RefreshItemImages
|
if (!ConfigurationManager.Configuration.RefreshItemImages && item.HasImage(ImageType.Primary) && item.BackdropImagePaths.Count > 0)
|
||||||
&& item.HasLocalImage("folder") && item.HasLocalImage("backdrop"))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -211,8 +210,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = string.Format(GetImages, id, MovieDbProvider.ApiKey, item is BoxSet ? "collection" : "movie"),
|
Url = string.Format(GetImages, id, MovieDbProvider.ApiKey, item is BoxSet ? "collection" : "movie"),
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = MovieDbProvider.AcceptHeader,
|
AcceptHeader = MovieDbProvider.AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
|
@ -175,8 +175,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = string.Format(TmdbConfigUrl, ApiKey),
|
Url = string.Format(TmdbConfigUrl, ApiKey),
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = AcceptHeader,
|
AcceptHeader = AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -475,8 +474,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url3,
|
Url = url3,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = AcceptHeader,
|
AcceptHeader = AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -509,8 +507,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url3,
|
Url = url3,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = AcceptHeader,
|
AcceptHeader = AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -549,8 +546,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url3,
|
Url = url3,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = AcceptHeader,
|
AcceptHeader = AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -632,8 +628,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url,
|
Url = url,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = AcceptHeader,
|
AcceptHeader = AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -710,8 +705,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url,
|
Url = url,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = AcceptHeader,
|
AcceptHeader = AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -732,8 +726,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url,
|
Url = url,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = AcceptHeader,
|
AcceptHeader = AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -896,7 +889,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Limit to three requests per second
|
// Limit to three requests per second
|
||||||
var diff = 500 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
|
var diff = 340 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
|
||||||
|
|
||||||
if (diff > 0)
|
if (diff > 0)
|
||||||
{
|
{
|
||||||
|
@ -145,8 +145,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url,
|
Url = url,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = MovieDbProvider.AcceptHeader,
|
AcceptHeader = MovieDbProvider.AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -172,8 +171,7 @@ namespace MediaBrowser.Controller.Providers.Movies
|
|||||||
{
|
{
|
||||||
Url = url,
|
Url = url,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken,
|
||||||
AcceptHeader = MovieDbProvider.AcceptHeader,
|
AcceptHeader = MovieDbProvider.AcceptHeader
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
|
@ -590,8 +590,7 @@ namespace MediaBrowser.Controller.Providers.TV
|
|||||||
{
|
{
|
||||||
Url = url,
|
Url = url,
|
||||||
ResourcePool = TvDbResourcePool,
|
ResourcePool = TvDbResourcePool,
|
||||||
CancellationToken = cancellationToken,
|
CancellationToken = cancellationToken
|
||||||
EnableResponseCache = true
|
|
||||||
|
|
||||||
}).ConfigureAwait(false))
|
}).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
|
@ -151,7 +151,6 @@
|
|||||||
<Compile Include="ScheduledTasks\ChapterImagesTask.cs" />
|
<Compile Include="ScheduledTasks\ChapterImagesTask.cs" />
|
||||||
<Compile Include="ScheduledTasks\ImageCleanupTask.cs" />
|
<Compile Include="ScheduledTasks\ImageCleanupTask.cs" />
|
||||||
<Compile Include="ScheduledTasks\PluginUpdateTask.cs" />
|
<Compile Include="ScheduledTasks\PluginUpdateTask.cs" />
|
||||||
<Compile Include="ScheduledTasks\AudioImagesTask.cs" />
|
|
||||||
<Compile Include="ScheduledTasks\RefreshMediaLibraryTask.cs" />
|
<Compile Include="ScheduledTasks\RefreshMediaLibraryTask.cs" />
|
||||||
<Compile Include="ScheduledTasks\VideoImagesTask.cs" />
|
<Compile Include="ScheduledTasks\VideoImagesTask.cs" />
|
||||||
<Compile Include="ServerApplicationPaths.cs" />
|
<Compile Include="ServerApplicationPaths.cs" />
|
||||||
|
@ -1,276 +0,0 @@
|
|||||||
using MediaBrowser.Common.IO;
|
|
||||||
using MediaBrowser.Common.MediaInfo;
|
|
||||||
using MediaBrowser.Common.ScheduledTasks;
|
|
||||||
using MediaBrowser.Controller;
|
|
||||||
using MediaBrowser.Controller.Entities.Audio;
|
|
||||||
using MediaBrowser.Controller.Library;
|
|
||||||
using MediaBrowser.Model.Entities;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using MediaBrowser.Model.Logging;
|
|
||||||
using MoreLinq;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Class AudioImagesTask
|
|
||||||
/// </summary>
|
|
||||||
public class AudioImagesTask : IScheduledTask
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the image cache.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The image cache.</value>
|
|
||||||
public FileSystemRepository ImageCache { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The _library manager
|
|
||||||
/// </summary>
|
|
||||||
private readonly ILibraryManager _libraryManager;
|
|
||||||
/// <summary>
|
|
||||||
/// The _media encoder
|
|
||||||
/// </summary>
|
|
||||||
private readonly IMediaEncoder _mediaEncoder;
|
|
||||||
|
|
||||||
private readonly ILogger _logger;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The _locks
|
|
||||||
/// </summary>
|
|
||||||
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
||||||
|
|
||||||
private readonly List<Audio> _newlyAddedItems = new List<Audio>();
|
|
||||||
|
|
||||||
private const int NewItemDelay = 60000;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The current new item timer
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The new item timer.</value>
|
|
||||||
private Timer NewItemTimer { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="AudioImagesTask" /> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
|
||||||
/// <param name="mediaEncoder">The media encoder.</param>
|
|
||||||
public AudioImagesTask(ILibraryManager libraryManager, IMediaEncoder mediaEncoder, ILogManager logManager)
|
|
||||||
{
|
|
||||||
_libraryManager = libraryManager;
|
|
||||||
_mediaEncoder = mediaEncoder;
|
|
||||||
_logger = logManager.GetLogger(GetType().Name);
|
|
||||||
|
|
||||||
ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.AudioImagesDataPath);
|
|
||||||
|
|
||||||
libraryManager.ItemAdded += libraryManager_ItemAdded;
|
|
||||||
libraryManager.ItemUpdated += libraryManager_ItemAdded;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Handles the ItemAdded event of the libraryManager control.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">The source of the event.</param>
|
|
||||||
/// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
|
|
||||||
void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
|
|
||||||
{
|
|
||||||
var audio = e.Item as Audio;
|
|
||||||
|
|
||||||
if (audio != null)
|
|
||||||
{
|
|
||||||
lock (_newlyAddedItems)
|
|
||||||
{
|
|
||||||
_newlyAddedItems.Add(audio);
|
|
||||||
|
|
||||||
if (NewItemTimer == null)
|
|
||||||
{
|
|
||||||
NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// News the item timer callback.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="state">The state.</param>
|
|
||||||
private async void NewItemTimerCallback(object state)
|
|
||||||
{
|
|
||||||
List<Audio> newSongs;
|
|
||||||
|
|
||||||
// Lock the list and release all resources
|
|
||||||
lock (_newlyAddedItems)
|
|
||||||
{
|
|
||||||
newSongs = _newlyAddedItems.DistinctBy(i => i.Id).ToList();
|
|
||||||
_newlyAddedItems.Clear();
|
|
||||||
|
|
||||||
NewItemTimer.Dispose();
|
|
||||||
NewItemTimer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in newSongs
|
|
||||||
.Where(i => i.LocationType == LocationType.FileSystem && string.IsNullOrEmpty(i.PrimaryImagePath) && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
|
|
||||||
.Take(10))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await CreateImagesForSong(item, CancellationToken.None).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error creating image for {0}", ex, item.Name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the name of the task
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The name.</value>
|
|
||||||
public string Name
|
|
||||||
{
|
|
||||||
get { return "Audio image extraction"; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the description.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The description.</value>
|
|
||||||
public string Description
|
|
||||||
{
|
|
||||||
get { return "Extracts images from audio files that do not have external images."; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the category.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The category.</value>
|
|
||||||
public string Category
|
|
||||||
{
|
|
||||||
get { return "Library"; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Executes the task
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
/// <param name="progress">The progress.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
|
|
||||||
{
|
|
||||||
var items = _libraryManager.RootFolder.RecursiveChildren
|
|
||||||
.OfType<Audio>()
|
|
||||||
.Where(i => i.LocationType == LocationType.FileSystem && string.IsNullOrEmpty(i.PrimaryImagePath) && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
progress.Report(0);
|
|
||||||
|
|
||||||
var numComplete = 0;
|
|
||||||
|
|
||||||
foreach (var item in items)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await CreateImagesForSong(item, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Already logged at lower levels.
|
|
||||||
// Just don't let the task fail
|
|
||||||
}
|
|
||||||
|
|
||||||
numComplete++;
|
|
||||||
double percent = numComplete;
|
|
||||||
percent /= items.Count;
|
|
||||||
|
|
||||||
progress.Report(100 * percent);
|
|
||||||
}
|
|
||||||
|
|
||||||
progress.Report(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates the images for song.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
private async Task CreateImagesForSong(Audio item, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
if (item.MediaStreams.All(i => i.Type != MediaStreamType.Video))
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Can't extract an image unless the audio file has an embedded image.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var album = item.Parent as MusicAlbum;
|
|
||||||
|
|
||||||
var filename = item.Album ?? string.Empty;
|
|
||||||
|
|
||||||
filename += album == null ? item.Id.ToString("N") + item.DateModified.Ticks : album.Id.ToString("N") + album.DateModified.Ticks;
|
|
||||||
|
|
||||||
var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
|
|
||||||
|
|
||||||
if (!ImageCache.ContainsFilePath(path))
|
|
||||||
{
|
|
||||||
var semaphore = GetLock(path);
|
|
||||||
|
|
||||||
// Acquire a lock
|
|
||||||
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
// Check again
|
|
||||||
if (!ImageCache.ContainsFilePath(path))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await _mediaEncoder.ExtractImage(new[] { item.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
semaphore.Release();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Image is already in the cache
|
|
||||||
item.PrimaryImagePath = path;
|
|
||||||
|
|
||||||
await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
semaphore.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the default triggers.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>IEnumerable{BaseTaskTrigger}.</returns>
|
|
||||||
public IEnumerable<ITaskTrigger> GetDefaultTriggers()
|
|
||||||
{
|
|
||||||
return new ITaskTrigger[]
|
|
||||||
{
|
|
||||||
new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1) }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the lock.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="filename">The filename.</param>
|
|
||||||
/// <returns>System.Object.</returns>
|
|
||||||
private SemaphoreSlim GetLock(string filename)
|
|
||||||
{
|
|
||||||
return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user