mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-07 18:24:19 -04:00
extract images for small numbers of items on discovery
This commit is contained in:
parent
ceaae430c6
commit
43845b4052
@ -1,5 +1,5 @@
|
|||||||
using System;
|
using MediaBrowser.Model.Entities;
|
||||||
using MediaBrowser.Model.Entities;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.Serialization;
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
@ -10,6 +10,11 @@ namespace MediaBrowser.Controller.Entities.Audio
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Audio : BaseItem, IHasMediaStreams
|
public class Audio : BaseItem, IHasMediaStreams
|
||||||
{
|
{
|
||||||
|
public Audio()
|
||||||
|
{
|
||||||
|
MediaStreams = new List<MediaStream>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the media streams.
|
/// Gets or sets the media streams.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -5,9 +5,7 @@ using MediaBrowser.Controller.Library;
|
|||||||
using MediaBrowser.Controller.Providers.MediaInfo;
|
using MediaBrowser.Controller.Providers.MediaInfo;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
using MediaBrowser.Model.Logging;
|
using MediaBrowser.Model.Logging;
|
||||||
using MoreLinq;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -38,22 +36,6 @@ namespace MediaBrowser.Controller.MediaInfo
|
|||||||
private readonly IMediaEncoder _encoder;
|
private readonly IMediaEncoder _encoder;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Holds the list of new items to generate chapter image for when the NewItemTimer expires
|
|
||||||
/// </summary>
|
|
||||||
private readonly List<Video> _newlyAddedItems = new List<Video>();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The amount of time to wait before generating chapter images
|
|
||||||
/// </summary>
|
|
||||||
private const int NewItemDelay = 300000;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The current new item timer
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The new item timer.</value>
|
|
||||||
private Timer NewItemTimer { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="FFMpegManager" /> class.
|
/// Initializes a new instance of the <see cref="FFMpegManager" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -71,38 +53,6 @@ namespace MediaBrowser.Controller.MediaInfo
|
|||||||
|
|
||||||
VideoImageCache = new FileSystemRepository(VideoImagesDataPath);
|
VideoImageCache = new FileSystemRepository(VideoImagesDataPath);
|
||||||
SubtitleCache = new FileSystemRepository(SubtitleCachePath);
|
SubtitleCache = new FileSystemRepository(SubtitleCachePath);
|
||||||
|
|
||||||
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 video = e.Item as Video;
|
|
||||||
|
|
||||||
if (video == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
lock (_newlyAddedItems)
|
|
||||||
{
|
|
||||||
_newlyAddedItems.Add(video);
|
|
||||||
|
|
||||||
if (NewItemTimer == null)
|
|
||||||
{
|
|
||||||
NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -183,40 +133,6 @@ namespace MediaBrowser.Controller.MediaInfo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when the new item timer expires
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="state">The state.</param>
|
|
||||||
private async void NewItemTimerCallback(object state)
|
|
||||||
{
|
|
||||||
List<Video> newItems;
|
|
||||||
|
|
||||||
// Lock the list and release all resources
|
|
||||||
lock (_newlyAddedItems)
|
|
||||||
{
|
|
||||||
newItems = _newlyAddedItems.DistinctBy(i => i.Id).ToList();
|
|
||||||
_newlyAddedItems.Clear();
|
|
||||||
|
|
||||||
NewItemTimer.Dispose();
|
|
||||||
NewItemTimer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Limit the number of videos we generate images for
|
|
||||||
// The idea is to catch new items that are added here and there
|
|
||||||
// Mass image generation can be left to the scheduled task
|
|
||||||
foreach (var video in newItems.Where(c => c.Chapters != null).Take(5))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await PopulateChapterImages(video, CancellationToken.None, true, true).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error creating chapter images for {0}", ex, video.Name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The first chapter ticks
|
/// The first chapter ticks
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -11,6 +11,8 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Model.Logging;
|
||||||
|
using MoreLinq;
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
||||||
{
|
{
|
||||||
@ -34,22 +36,99 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly IMediaEncoder _mediaEncoder;
|
private readonly IMediaEncoder _mediaEncoder;
|
||||||
|
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The _locks
|
/// The _locks
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
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>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="AudioImagesTask" /> class.
|
/// Initializes a new instance of the <see cref="AudioImagesTask" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
/// <param name="mediaEncoder">The media encoder.</param>
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
public AudioImagesTask(ILibraryManager libraryManager, IMediaEncoder mediaEncoder)
|
public AudioImagesTask(ILibraryManager libraryManager, IMediaEncoder mediaEncoder, ILogManager logManager)
|
||||||
{
|
{
|
||||||
_libraryManager = libraryManager;
|
_libraryManager = libraryManager;
|
||||||
_mediaEncoder = mediaEncoder;
|
_mediaEncoder = mediaEncoder;
|
||||||
|
_logger = logManager.GetLogger(GetType().Name);
|
||||||
|
|
||||||
ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.AudioImagesDataPath);
|
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>
|
/// <summary>
|
||||||
@ -89,7 +168,7 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
{
|
{
|
||||||
var items = _libraryManager.RootFolder.RecursiveChildren
|
var items = _libraryManager.RootFolder.RecursiveChildren
|
||||||
.OfType<Audio>()
|
.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();
|
.ToList();
|
||||||
|
|
||||||
progress.Report(0);
|
progress.Report(0);
|
||||||
@ -97,9 +176,42 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
var numComplete = 0;
|
var numComplete = 0;
|
||||||
|
|
||||||
foreach (var item in items)
|
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();
|
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 album = item.Parent as MusicAlbum;
|
||||||
|
|
||||||
var filename = item.Album ?? string.Empty;
|
var filename = item.Album ?? string.Empty;
|
||||||
@ -108,8 +220,6 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
|
|
||||||
var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
|
var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
|
||||||
|
|
||||||
var success = true;
|
|
||||||
|
|
||||||
if (!ImageCache.ContainsFilePath(path))
|
if (!ImageCache.ContainsFilePath(path))
|
||||||
{
|
{
|
||||||
var semaphore = GetLock(path);
|
var semaphore = GetLock(path);
|
||||||
@ -124,37 +234,21 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
{
|
{
|
||||||
await _mediaEncoder.ExtractImage(new[] {item.Path}, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
|
await _mediaEncoder.ExtractImage(new[] {item.Path}, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
|
||||||
success = false;
|
|
||||||
}
|
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
semaphore.Release();
|
semaphore.Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Image is already in the cache
|
||||||
|
item.PrimaryImagePath = path;
|
||||||
|
|
||||||
|
await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
semaphore.Release();
|
semaphore.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
numComplete++;
|
|
||||||
double percent = numComplete;
|
|
||||||
percent /= items.Count;
|
|
||||||
|
|
||||||
progress.Report(100 * percent);
|
|
||||||
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
// Image is already in the cache
|
|
||||||
item.PrimaryImagePath = path;
|
|
||||||
|
|
||||||
await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
progress.Report(100);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -9,6 +9,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MoreLinq;
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
||||||
{
|
{
|
||||||
@ -30,17 +31,81 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly ILibraryManager _libraryManager;
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
|
||||||
|
private readonly List<Video> _newlyAddedItems = new List<Video>();
|
||||||
|
|
||||||
|
private const int NewItemDelay = 300000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current new item timer
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The new item timer.</value>
|
||||||
|
private Timer NewItemTimer { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
|
/// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="kernel">The kernel.</param>
|
/// <param name="kernel">The kernel.</param>
|
||||||
/// <param name="logger">The logger.</param>
|
/// <param name="logManager">The log manager.</param>
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
public ChapterImagesTask(Kernel kernel, ILogger logger, ILibraryManager libraryManager)
|
public ChapterImagesTask(Kernel kernel, ILogManager logManager, ILibraryManager libraryManager)
|
||||||
{
|
{
|
||||||
_kernel = kernel;
|
_kernel = kernel;
|
||||||
_logger = logger;
|
_logger = logManager.GetLogger(GetType().Name);
|
||||||
_libraryManager = libraryManager;
|
_libraryManager = libraryManager;
|
||||||
|
|
||||||
|
libraryManager.ItemAdded += libraryManager_ItemAdded;
|
||||||
|
libraryManager.ItemUpdated += libraryManager_ItemAdded;
|
||||||
|
}
|
||||||
|
|
||||||
|
void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
|
||||||
|
{
|
||||||
|
var video = e.Item as Video;
|
||||||
|
|
||||||
|
if (video != null)
|
||||||
|
{
|
||||||
|
lock (_newlyAddedItems)
|
||||||
|
{
|
||||||
|
_newlyAddedItems.Add(video);
|
||||||
|
|
||||||
|
if (NewItemTimer == null)
|
||||||
|
{
|
||||||
|
NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void NewItemTimerCallback(object state)
|
||||||
|
{
|
||||||
|
List<Video> newItems;
|
||||||
|
|
||||||
|
// Lock the list and release all resources
|
||||||
|
lock (_newlyAddedItems)
|
||||||
|
{
|
||||||
|
newItems = _newlyAddedItems.DistinctBy(i => i.Id).ToList();
|
||||||
|
_newlyAddedItems.Clear();
|
||||||
|
|
||||||
|
NewItemTimer.Dispose();
|
||||||
|
NewItemTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in newItems
|
||||||
|
.Where(i => i.LocationType == LocationType.FileSystem && string.IsNullOrEmpty(i.PrimaryImagePath) && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
|
||||||
|
.Take(5))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _kernel.FFMpegManager.PopulateChapterImages(item, CancellationToken.None, true, true);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.ErrorException("Error creating image for {0}", ex, item.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -13,6 +13,8 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Model.Logging;
|
||||||
|
using MoreLinq;
|
||||||
|
|
||||||
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
||||||
{
|
{
|
||||||
@ -41,24 +43,94 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly IIsoManager _isoManager;
|
private readonly IIsoManager _isoManager;
|
||||||
|
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The _locks
|
/// The _locks
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
||||||
|
|
||||||
|
private readonly List<BaseItem> _newlyAddedItems = new List<BaseItem>();
|
||||||
|
|
||||||
|
private const int NewItemDelay = 300000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current new item timer
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The new item timer.</value>
|
||||||
|
private Timer NewItemTimer { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="AudioImagesTask" /> class.
|
/// Initializes a new instance of the <see cref="AudioImagesTask" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
|
/// <param name="logManager">The log manager.</param>
|
||||||
/// <param name="mediaEncoder">The media encoder.</param>
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
/// <param name="isoManager">The iso manager.</param>
|
/// <param name="isoManager">The iso manager.</param>
|
||||||
public VideoImagesTask(ILibraryManager libraryManager, IMediaEncoder mediaEncoder, IIsoManager isoManager)
|
public VideoImagesTask(ILibraryManager libraryManager, ILogManager logManager, IMediaEncoder mediaEncoder, IIsoManager isoManager)
|
||||||
{
|
{
|
||||||
_libraryManager = libraryManager;
|
_libraryManager = libraryManager;
|
||||||
_mediaEncoder = mediaEncoder;
|
_mediaEncoder = mediaEncoder;
|
||||||
_isoManager = isoManager;
|
_isoManager = isoManager;
|
||||||
|
_logger = logManager.GetLogger(GetType().Name);
|
||||||
|
|
||||||
ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.VideoImagesDataPath);
|
ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.VideoImagesDataPath);
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
lock (_newlyAddedItems)
|
||||||
|
{
|
||||||
|
_newlyAddedItems.Add(e.Item);
|
||||||
|
|
||||||
|
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<BaseItem> newItems;
|
||||||
|
|
||||||
|
// Lock the list and release all resources
|
||||||
|
lock (_newlyAddedItems)
|
||||||
|
{
|
||||||
|
newItems = _newlyAddedItems.DistinctBy(i => i.Id).ToList();
|
||||||
|
_newlyAddedItems.Clear();
|
||||||
|
|
||||||
|
NewItemTimer.Dispose();
|
||||||
|
NewItemTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in GetItemsForExtraction(newItems.Take(5)))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await ExtractImage(item, CancellationToken.None).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.ErrorException("Error creating image for {0}", ex, item.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -96,7 +168,42 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
|
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
|
||||||
{
|
{
|
||||||
var allItems = _libraryManager.RootFolder.RecursiveChildren.ToList();
|
var items = GetItemsForExtraction(_libraryManager.RootFolder.RecursiveChildren).ToList();
|
||||||
|
|
||||||
|
progress.Report(0);
|
||||||
|
|
||||||
|
var numComplete = 0;
|
||||||
|
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await ExtractImage(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>
|
||||||
|
/// Gets the items for extraction.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sourceItems">The source items.</param>
|
||||||
|
/// <returns>IEnumerable{BaseItem}.</returns>
|
||||||
|
private IEnumerable<Video> GetItemsForExtraction(IEnumerable<BaseItem> sourceItems)
|
||||||
|
{
|
||||||
|
var allItems = sourceItems.ToList();
|
||||||
|
|
||||||
var localTrailers = allItems.SelectMany(i => i.LocalTrailers);
|
var localTrailers = allItems.SelectMany(i => i.LocalTrailers);
|
||||||
var themeVideos = allItems.SelectMany(i => i.ThemeVideos);
|
var themeVideos = allItems.SelectMany(i => i.ThemeVideos);
|
||||||
@ -108,7 +215,7 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
items.AddRange(themeVideos);
|
items.AddRange(themeVideos);
|
||||||
items.AddRange(videos.OfType<Movie>().SelectMany(i => i.SpecialFeatures).ToList());
|
items.AddRange(videos.OfType<Movie>().SelectMany(i => i.SpecialFeatures).ToList());
|
||||||
|
|
||||||
items = items.Where(i =>
|
return items.Where(i =>
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(i.PrimaryImagePath))
|
if (!string.IsNullOrEmpty(i.PrimaryImagePath))
|
||||||
{
|
{
|
||||||
@ -131,13 +238,16 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
}
|
}
|
||||||
|
|
||||||
return i.MediaStreams != null && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video);
|
return i.MediaStreams != null && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video);
|
||||||
}).ToList();
|
});
|
||||||
|
}
|
||||||
|
|
||||||
progress.Report(0);
|
/// <summary>
|
||||||
|
/// Extracts the image.
|
||||||
var numComplete = 0;
|
/// </summary>
|
||||||
|
/// <param name="item">The item.</param>
|
||||||
foreach (var item in items)
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns>Task.</returns>
|
||||||
|
private async Task ExtractImage(Video item, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
@ -145,8 +255,6 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
|
|
||||||
var path = ImageCache.GetResourcePath(filename, ".jpg");
|
var path = ImageCache.GetResourcePath(filename, ".jpg");
|
||||||
|
|
||||||
var success = true;
|
|
||||||
|
|
||||||
if (!ImageCache.ContainsFilePath(path))
|
if (!ImageCache.ContainsFilePath(path))
|
||||||
{
|
{
|
||||||
var semaphore = GetLock(path);
|
var semaphore = GetLock(path);
|
||||||
@ -159,39 +267,23 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ExtractImage(item, path, cancellationToken).ConfigureAwait(false);
|
await ExtractImageInternal(item, path, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
success = false;
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
semaphore.Release();
|
semaphore.Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Image is already in the cache
|
||||||
|
item.PrimaryImagePath = path;
|
||||||
|
|
||||||
|
await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
semaphore.Release();
|
semaphore.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
numComplete++;
|
|
||||||
double percent = numComplete;
|
|
||||||
percent /= items.Count;
|
|
||||||
|
|
||||||
progress.Report(100 * percent);
|
|
||||||
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
// Image is already in the cache
|
|
||||||
item.PrimaryImagePath = path;
|
|
||||||
|
|
||||||
await _libraryManager.UpdateItem(item, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
progress.Report(100);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -201,7 +293,7 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
|
|||||||
/// <param name="path">The path.</param>
|
/// <param name="path">The path.</param>
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
private async Task ExtractImage(Video video, string path, CancellationToken cancellationToken)
|
private async Task ExtractImageInternal(Video video, string path, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
|
var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user