mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-12-22 00:35:26 +03:00
extract images for small numbers of items on discovery
This commit is contained in:
@@ -11,6 +11,8 @@ 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
|
||||
{
|
||||
@@ -34,24 +36,101 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
||||
/// </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 = 300000;
|
||||
|
||||
/// <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)
|
||||
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(20))
|
||||
{
|
||||
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>
|
||||
@@ -89,7 +168,7 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
||||
{
|
||||
var items = _libraryManager.RootFolder.RecursiveChildren
|
||||
.OfType<Audio>()
|
||||
.Where(i => i.LocationType == LocationType.FileSystem && string.IsNullOrEmpty(i.PrimaryImagePath) && i.MediaStreams != null && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
|
||||
.Where(i => i.LocationType == LocationType.FileSystem && string.IsNullOrEmpty(i.PrimaryImagePath) && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
|
||||
.ToList();
|
||||
|
||||
progress.Report(0);
|
||||
@@ -98,45 +177,14 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var album = item.Parent as MusicAlbum;
|
||||
|
||||
var filename = item.Album ?? string.Empty;
|
||||
|
||||
filename += album == null ? item.Id.ToString() + item.DateModified.Ticks : album.Id.ToString() + album.DateModified.Ticks;
|
||||
|
||||
var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
|
||||
|
||||
var success = true;
|
||||
|
||||
if (!ImageCache.ContainsFilePath(path))
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
catch
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphore.Release();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
semaphore.Release();
|
||||
}
|
||||
await CreateImagesForSong(item, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Already logged at lower levels.
|
||||
// Just don't let the task fail
|
||||
}
|
||||
|
||||
numComplete++;
|
||||
@@ -144,17 +192,63 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
||||
percent /= items.Count;
|
||||
|
||||
progress.Report(100 * percent);
|
||||
}
|
||||
|
||||
if (success)
|
||||
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() + item.DateModified.Ticks : album.Id.ToString() + 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();
|
||||
}
|
||||
}
|
||||
|
||||
progress.Report(100);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user