aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Server.Implementations')
-rw-r--r--MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj2
-rw-r--r--MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs17
-rw-r--r--MediaBrowser.Server.Implementations/ScheduledTasks/AudioImagesTask.cs178
-rw-r--r--MediaBrowser.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs5
-rw-r--r--MediaBrowser.Server.Implementations/ScheduledTasks/VideoImagesTask.cs265
5 files changed, 463 insertions, 4 deletions
diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
index 66ec69f08..286710bc3 100644
--- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
+++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj
@@ -153,7 +153,9 @@
<Compile Include="ScheduledTasks\ChapterImagesTask.cs" />
<Compile Include="ScheduledTasks\ImageCleanupTask.cs" />
<Compile Include="ScheduledTasks\PluginUpdateTask.cs" />
+ <Compile Include="ScheduledTasks\AudioImagesTask.cs" />
<Compile Include="ScheduledTasks\RefreshMediaLibraryTask.cs" />
+ <Compile Include="ScheduledTasks\VideoImagesTask.cs" />
<Compile Include="ServerApplicationPaths.cs" />
<Compile Include="ServerManager\ServerManager.cs" />
<Compile Include="ServerManager\WebSocketConnection.cs" />
diff --git a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs
index 32c18822f..08e2eb774 100644
--- a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs
+++ b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs
@@ -50,12 +50,12 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder
/// <summary>
/// The video image resource pool
/// </summary>
- private readonly SemaphoreSlim _videoImageResourcePool = new SemaphoreSlim(2, 2);
+ private readonly SemaphoreSlim _videoImageResourcePool = new SemaphoreSlim(1, 1);
/// <summary>
/// The audio image resource pool
/// </summary>
- private readonly SemaphoreSlim _audioImageResourcePool = new SemaphoreSlim(3, 3);
+ private readonly SemaphoreSlim _audioImageResourcePool = new SemaphoreSlim(2, 2);
/// <summary>
/// The _subtitle extraction resource pool
@@ -65,7 +65,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder
/// <summary>
/// The FF probe resource pool
/// </summary>
- private readonly SemaphoreSlim _ffProbeResourcePool = new SemaphoreSlim(3, 3);
+ private readonly SemaphoreSlim _ffProbeResourcePool = new SemaphoreSlim(2, 2);
/// <summary>
/// Gets or sets the versioned directory path.
@@ -370,7 +370,18 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder
try
{
process.Start();
+ }
+ catch (Exception ex)
+ {
+ _ffProbeResourcePool.Release();
+
+ _logger.ErrorException("Error starting ffprobe", ex);
+ throw;
+ }
+
+ try
+ {
Task<string> standardErrorReadTask = null;
// MUST read both stdout and stderr asynchronously or a deadlock may occurr
diff --git a/MediaBrowser.Server.Implementations/ScheduledTasks/AudioImagesTask.cs b/MediaBrowser.Server.Implementations/ScheduledTasks/AudioImagesTask.cs
new file mode 100644
index 000000000..db809a47b
--- /dev/null
+++ b/MediaBrowser.Server.Implementations/ScheduledTasks/AudioImagesTask.cs
@@ -0,0 +1,178 @@
+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;
+
+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;
+
+ /// <summary>
+ /// The _locks
+ /// </summary>
+ private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
+
+ /// <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)
+ {
+ _libraryManager = libraryManager;
+ _mediaEncoder = mediaEncoder;
+
+ ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.AudioImagesDataPath);
+ }
+
+ /// <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 != null && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
+ .ToList();
+
+ progress.Report(0);
+
+ var numComplete = 0;
+
+ foreach (var item in items)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+
+ var album = item.Parent as MusicAlbum;
+
+ var filename = album != null && string.IsNullOrEmpty(item.Album + album.DateModified.Ticks) ? (item.Id.ToString() + item.DateModified.Ticks) : item.Album;
+
+ var path = ImageCache.GetResourcePath(filename + "_primary", ".jpg");
+
+ var success = true;
+
+ 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);
+ }
+ catch
+ {
+ success = false;
+ }
+ finally
+ {
+ semaphore.Release();
+ }
+ }
+ else
+ {
+ semaphore.Release();
+ }
+ }
+
+ numComplete++;
+ double percent = numComplete;
+ percent /= items.Count;
+
+ progress.Report(100 * percent);
+
+ if (success)
+ {
+ // Image is already in the cache
+ item.PrimaryImagePath = path;
+ }
+ }
+
+ progress.Report(100);
+ }
+
+ /// <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));
+ }
+ }
+}
diff --git a/MediaBrowser.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs b/MediaBrowser.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs
index 6cae2f75b..d46d4ec8a 100644
--- a/MediaBrowser.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs
+++ b/MediaBrowser.Server.Implementations/ScheduledTasks/ChapterImagesTask.cs
@@ -24,6 +24,9 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
/// The _logger
/// </summary>
private readonly ILogger _logger;
+ /// <summary>
+ /// The _library manager
+ /// </summary>
private readonly ILibraryManager _libraryManager;
/// <summary>
@@ -99,7 +102,7 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
/// <value>The name.</value>
public string Name
{
- get { return "Create video chapter thumbnails"; }
+ get { return "Chapter image extraction"; }
}
/// <summary>
diff --git a/MediaBrowser.Server.Implementations/ScheduledTasks/VideoImagesTask.cs b/MediaBrowser.Server.Implementations/ScheduledTasks/VideoImagesTask.cs
new file mode 100644
index 000000000..a82c22fe9
--- /dev/null
+++ b/MediaBrowser.Server.Implementations/ScheduledTasks/VideoImagesTask.cs
@@ -0,0 +1,265 @@
+using MediaBrowser.Common.IO;
+using MediaBrowser.Common.MediaInfo;
+using MediaBrowser.Common.ScheduledTasks;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Providers.MediaInfo;
+using MediaBrowser.Model.Entities;
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Server.Implementations.ScheduledTasks
+{
+ /// <summary>
+ /// Class VideoImagesTask
+ /// </summary>
+ public class VideoImagesTask : 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;
+
+ /// <summary>
+ /// The _iso manager
+ /// </summary>
+ private readonly IIsoManager _isoManager;
+
+ /// <summary>
+ /// The _locks
+ /// </summary>
+ private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
+
+ /// <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>
+ /// <param name="isoManager">The iso manager.</param>
+ public VideoImagesTask(ILibraryManager libraryManager, IMediaEncoder mediaEncoder, IIsoManager isoManager)
+ {
+ _libraryManager = libraryManager;
+ _mediaEncoder = mediaEncoder;
+ _isoManager = isoManager;
+
+ ImageCache = new FileSystemRepository(Kernel.Instance.FFMpegManager.VideoImagesDataPath);
+ }
+
+ /// <summary>
+ /// Gets the name of the task
+ /// </summary>
+ /// <value>The name.</value>
+ public string Name
+ {
+ get { return "Video 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<Video>()
+ .Where(i =>
+ {
+ if (!string.IsNullOrEmpty(i.PrimaryImagePath))
+ {
+ return false;
+ }
+
+ if (i.LocationType != LocationType.FileSystem)
+ {
+ return false;
+ }
+
+ if (i.VideoType == VideoType.HdDvd)
+ {
+ return false;
+ }
+
+ if (i.VideoType == VideoType.Iso && !i.IsoType.HasValue)
+ {
+ return false;
+ }
+
+ return i.MediaStreams != null && i.MediaStreams.Any(m => m.Type == MediaStreamType.Video);
+ })
+ .ToList();
+
+ progress.Report(0);
+
+ var numComplete = 0;
+
+ foreach (var item in items)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+
+ var filename = item.Id + "_" + item.DateModified.Ticks + "_primary";
+
+ var path = ImageCache.GetResourcePath(filename, ".jpg");
+
+ var success = true;
+
+ 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 ExtractImage(item, path, cancellationToken).ConfigureAwait(false);
+ }
+ catch
+ {
+ success = false;
+ }
+ finally
+ {
+ semaphore.Release();
+ }
+ }
+ else
+ {
+ semaphore.Release();
+ }
+ }
+
+ numComplete++;
+ double percent = numComplete;
+ percent /= items.Count;
+
+ progress.Report(100 * percent);
+
+ if (success)
+ {
+ // Image is already in the cache
+ item.PrimaryImagePath = path;
+ }
+ }
+
+ progress.Report(100);
+ }
+
+ /// <summary>
+ /// Extracts the image.
+ /// </summary>
+ /// <param name="video">The video.</param>
+ /// <param name="path">The path.</param>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task.</returns>
+ private async Task ExtractImage(Video video, string path, CancellationToken cancellationToken)
+ {
+ var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
+
+ try
+ {
+ // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
+ // Always use 10 seconds for dvd because our duration could be out of whack
+ var imageOffset = video.VideoType != VideoType.Dvd && video.RunTimeTicks.HasValue &&
+ video.RunTimeTicks.Value > 0
+ ? TimeSpan.FromTicks(Convert.ToInt64(video.RunTimeTicks.Value * .1))
+ : TimeSpan.FromSeconds(10);
+
+ InputType type;
+
+ var inputPath = MediaEncoderHelpers.GetInputArgument(video, isoMount, out type);
+
+ await _mediaEncoder.ExtractImage(inputPath, type, imageOffset, path, cancellationToken).ConfigureAwait(false);
+
+ video.PrimaryImagePath = path;
+ }
+ finally
+ {
+ if (isoMount != null)
+ {
+ isoMount.Dispose();
+ }
+ }
+ }
+
+ /// <summary>
+ /// The null mount task result
+ /// </summary>
+ protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
+
+ /// <summary>
+ /// Mounts the iso if needed.
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task{IIsoMount}.</returns>
+ protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
+ {
+ if (item.VideoType == VideoType.Iso)
+ {
+ return _isoManager.Mount(item.Path, cancellationToken);
+ }
+
+ return NullMountTaskResult;
+ }
+
+ /// <summary>
+ /// Gets the default triggers.
+ /// </summary>
+ /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
+ public IEnumerable<ITaskTrigger> GetDefaultTriggers()
+ {
+ return new ITaskTrigger[]
+ {
+ new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }
+ };
+ }
+
+ /// <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));
+ }
+ }
+}