mirror of https://github.com/jellyfin/jellyfin.git
crobibero styling, format, code suggestions
This commit is contained in:
parent
dd8ef08592
commit
3377032228
|
@ -90,6 +90,13 @@ namespace MediaBrowser.Controller.MediaEncoding
|
||||||
{ "truehd", 6 },
|
{ "truehd", 6 },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static readonly string _defaultMjpegEncoder = "mjpeg";
|
||||||
|
private static readonly Dictionary<string, string> _mjpegCodecMap = new(StringComparer.OrdinalIgnoreCase)
|
||||||
|
{
|
||||||
|
{ "vaapi", _defaultMjpegEncoder + "_vaapi" },
|
||||||
|
{ "qsv", _defaultMjpegEncoder + "_qsv" }
|
||||||
|
};
|
||||||
|
|
||||||
public static readonly string[] LosslessAudioCodecs = new string[]
|
public static readonly string[] LosslessAudioCodecs = new string[]
|
||||||
{
|
{
|
||||||
"alac",
|
"alac",
|
||||||
|
@ -151,32 +158,20 @@ namespace MediaBrowser.Controller.MediaEncoding
|
||||||
|
|
||||||
private string GetMjpegEncoder(EncodingJobInfo state, EncodingOptions encodingOptions)
|
private string GetMjpegEncoder(EncodingJobInfo state, EncodingOptions encodingOptions)
|
||||||
{
|
{
|
||||||
var defaultEncoder = "mjpeg";
|
|
||||||
|
|
||||||
if (state.VideoType == VideoType.VideoFile)
|
if (state.VideoType == VideoType.VideoFile)
|
||||||
{
|
{
|
||||||
var hwType = encodingOptions.HardwareAccelerationType;
|
var hwType = encodingOptions.HardwareAccelerationType;
|
||||||
|
|
||||||
var codecMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
|
||||||
{
|
|
||||||
{ "vaapi", defaultEncoder + "_vaapi" },
|
|
||||||
{ "qsv", defaultEncoder + "_qsv" }
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(hwType)
|
if (!string.IsNullOrEmpty(hwType)
|
||||||
&& encodingOptions.EnableHardwareEncoding
|
&& encodingOptions.EnableHardwareEncoding
|
||||||
&& codecMap.ContainsKey(hwType))
|
&& _mjpegCodecMap.TryGetValue(hwType, out var preferredEncoder)
|
||||||
|
&& _mediaEncoder.SupportsEncoder(preferredEncoder))
|
||||||
{
|
{
|
||||||
var preferredEncoder = codecMap[hwType];
|
return preferredEncoder;
|
||||||
|
|
||||||
if (_mediaEncoder.SupportsEncoder(preferredEncoder))
|
|
||||||
{
|
|
||||||
return preferredEncoder;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultEncoder;
|
return _defaultMjpegEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsVaapiSupported(EncodingJobInfo state)
|
private bool IsVaapiSupported(EncodingJobInfo state)
|
||||||
|
|
|
@ -5,50 +5,49 @@ using System.Threading.Tasks;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
|
|
||||||
namespace MediaBrowser.Controller.Trickplay
|
namespace MediaBrowser.Controller.Trickplay;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface ITrickplayManager.
|
||||||
|
/// </summary>
|
||||||
|
public interface ITrickplayManager
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface ITrickplayManager.
|
/// Generate or replace trickplay data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface ITrickplayManager
|
/// <param name="video">The video.</param>
|
||||||
{
|
/// <param name="replace">Whether or not existing data should be replaced.</param>
|
||||||
/// <summary>
|
/// <param name="cancellationToken">CancellationToken to use for operation.</param>
|
||||||
/// Generate or replace trickplay data.
|
/// <returns>Task.</returns>
|
||||||
/// </summary>
|
Task RefreshTrickplayDataAsync(Video video, bool replace, CancellationToken cancellationToken);
|
||||||
/// <param name="video">The video.</param>
|
|
||||||
/// <param name="replace">Whether or not existing data should be replaced.</param>
|
|
||||||
/// <param name="cancellationToken">CancellationToken to use for operation.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
Task RefreshTrickplayData(Video video, bool replace, CancellationToken cancellationToken);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get available trickplay resolutions and corresponding info.
|
/// Get available trickplay resolutions and corresponding info.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="itemId">The item.</param>
|
/// <param name="itemId">The item.</param>
|
||||||
/// <returns>Map of width resolutions to trickplay tiles info.</returns>
|
/// <returns>Map of width resolutions to trickplay tiles info.</returns>
|
||||||
Dictionary<int, TrickplayTilesInfo> GetTilesResolutions(Guid itemId);
|
Dictionary<int, TrickplayTilesInfo> GetTilesResolutions(Guid itemId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Saves trickplay tiles info.
|
/// Saves trickplay tiles info.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="itemId">The item.</param>
|
/// <param name="itemId">The item.</param>
|
||||||
/// <param name="tilesInfo">The trickplay tiles info.</param>
|
/// <param name="tilesInfo">The trickplay tiles info.</param>
|
||||||
void SaveTilesInfo(Guid itemId, TrickplayTilesInfo tilesInfo);
|
void SaveTilesInfo(Guid itemId, TrickplayTilesInfo tilesInfo);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the trickplay manifest.
|
/// Gets the trickplay manifest.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="item">The item.</param>
|
/// <param name="item">The item.</param>
|
||||||
/// <returns>A map of media source id to a map of tile width to tile info.</returns>
|
/// <returns>A map of media source id to a map of tile width to tile info.</returns>
|
||||||
Dictionary<Guid, Dictionary<int, TrickplayTilesInfo>> GetTrickplayManifest(BaseItem item);
|
Dictionary<Guid, Dictionary<int, TrickplayTilesInfo>> GetTrickplayManifest(BaseItem item);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the path to a trickplay tiles image.
|
/// Gets the path to a trickplay tiles image.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="item">The item.</param>
|
/// <param name="item">The item.</param>
|
||||||
/// <param name="width">The width of a single tile.</param>
|
/// <param name="width">The width of a single tile.</param>
|
||||||
/// <param name="index">The tile grid's index.</param>
|
/// <param name="index">The tile grid's index.</param>
|
||||||
/// <returns>The absolute path.</returns>
|
/// <returns>The absolute path.</returns>
|
||||||
string GetTrickplayTilePath(BaseItem item, int width, int index);
|
string GetTrickplayTilePath(BaseItem item, int width, int index);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -793,7 +793,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var options = allowHwAccel ? _configurationManager.GetEncodingOptions() : new EncodingOptions();
|
var options = allowHwAccel ? _configurationManager.GetEncodingOptions() : new EncodingOptions();
|
||||||
threads = threads ?? _threads;
|
threads ??= _threads;
|
||||||
|
|
||||||
// A new EncodingOptions instance must be used as to not disable HW acceleration for all of Jellyfin.
|
// A new EncodingOptions instance must be used as to not disable HW acceleration for all of Jellyfin.
|
||||||
// Additionally, we must set a few fields without defaults to prevent null pointer exceptions.
|
// Additionally, we must set a few fields without defaults to prevent null pointer exceptions.
|
||||||
|
|
|
@ -1,61 +1,60 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace MediaBrowser.Model.Configuration
|
namespace MediaBrowser.Model.Configuration;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class TrickplayOptions.
|
||||||
|
/// </summary>
|
||||||
|
public class TrickplayOptions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class TrickplayOptions.
|
/// Gets or sets a value indicating whether or not to use HW acceleration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TrickplayOptions
|
public bool EnableHwAcceleration { get; set; } = false;
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets a value indicating whether or not to use HW acceleration.
|
|
||||||
/// </summary>
|
|
||||||
public bool EnableHwAcceleration { get; set; } = false;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the behavior used by trickplay provider on library scan/update.
|
/// Gets or sets the behavior used by trickplay provider on library scan/update.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public TrickplayScanBehavior ScanBehavior { get; set; } = TrickplayScanBehavior.NonBlocking;
|
public TrickplayScanBehavior ScanBehavior { get; set; } = TrickplayScanBehavior.NonBlocking;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the process priority for the ffmpeg process.
|
/// Gets or sets the process priority for the ffmpeg process.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ProcessPriorityClass ProcessPriority { get; set; } = ProcessPriorityClass.BelowNormal;
|
public ProcessPriorityClass ProcessPriority { get; set; } = ProcessPriorityClass.BelowNormal;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the interval, in ms, between each new trickplay image.
|
/// Gets or sets the interval, in ms, between each new trickplay image.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Interval { get; set; } = 10000;
|
public int Interval { get; set; } = 10000;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the target width resolutions, in px, to generates preview images for.
|
/// Gets or sets the target width resolutions, in px, to generates preview images for.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int[] WidthResolutions { get; set; } = new[] { 320 };
|
public int[] WidthResolutions { get; set; } = new[] { 320 };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets number of tile images to allow in X dimension.
|
/// Gets or sets number of tile images to allow in X dimension.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int TileWidth { get; set; } = 10;
|
public int TileWidth { get; set; } = 10;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets number of tile images to allow in Y dimension.
|
/// Gets or sets number of tile images to allow in Y dimension.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int TileHeight { get; set; } = 10;
|
public int TileHeight { get; set; } = 10;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the ffmpeg output quality level.
|
/// Gets or sets the ffmpeg output quality level.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int Qscale { get; set; } = 4;
|
public int Qscale { get; set; } = 4;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the jpeg quality to use for image tiles.
|
/// Gets or sets the jpeg quality to use for image tiles.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int JpegQuality { get; set; } = 90;
|
public int JpegQuality { get; set; } = 90;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the number of threads to be used by ffmpeg.
|
/// Gets or sets the number of threads to be used by ffmpeg.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public int ProcessThreads { get; set; } = 0;
|
public int ProcessThreads { get; set; } = 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,17 @@
|
||||||
namespace MediaBrowser.Model.Configuration
|
namespace MediaBrowser.Model.Configuration;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enum TrickplayScanBehavior.
|
||||||
|
/// </summary>
|
||||||
|
public enum TrickplayScanBehavior
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enum TrickplayScanBehavior.
|
/// Starts generation, only return once complete.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public enum TrickplayScanBehavior
|
Blocking,
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Starts generation, only return once complete.
|
|
||||||
/// </summary>
|
|
||||||
Blocking,
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Start generation, return immediately.
|
/// Start generation, return immediately.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
NonBlocking
|
NonBlocking
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,50 +1,49 @@
|
||||||
namespace MediaBrowser.Model.Entities
|
namespace MediaBrowser.Model.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class TrickplayTilesInfo.
|
||||||
|
/// </summary>
|
||||||
|
public class TrickplayTilesInfo
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class TrickplayTilesInfo.
|
/// Gets or sets width of an individual tile.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TrickplayTilesInfo
|
/// <value>The width.</value>
|
||||||
{
|
public int Width { get; set; }
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets width of an individual tile.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The width.</value>
|
|
||||||
public int Width { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets height of an individual tile.
|
/// Gets or sets height of an individual tile.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The height.</value>
|
/// <value>The height.</value>
|
||||||
public int Height { get; set; }
|
public int Height { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets amount of tiles per row.
|
/// Gets or sets amount of tiles per row.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The tile grid's width.</value>
|
/// <value>The tile grid's width.</value>
|
||||||
public int TileWidth { get; set; }
|
public int TileWidth { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets amount of tiles per column.
|
/// Gets or sets amount of tiles per column.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The tile grid's height.</value>
|
/// <value>The tile grid's height.</value>
|
||||||
public int TileHeight { get; set; }
|
public int TileHeight { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets total amount of non-black tiles.
|
/// Gets or sets total amount of non-black tiles.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The tile count.</value>
|
/// <value>The tile count.</value>
|
||||||
public int TileCount { get; set; }
|
public int TileCount { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets interval in milliseconds between each trickplay tile.
|
/// Gets or sets interval in milliseconds between each trickplay tile.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The interval.</value>
|
/// <value>The interval.</value>
|
||||||
public int Interval { get; set; }
|
public int Interval { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets peak bandwith usage in bits per second.
|
/// Gets or sets peak bandwith usage in bits per second.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The bandwidth.</value>
|
/// <value>The bandwidth.</value>
|
||||||
public int Bandwidth { get; set; }
|
public int Bandwidth { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,98 +12,97 @@ using MediaBrowser.Model.Globalization;
|
||||||
using MediaBrowser.Model.Tasks;
|
using MediaBrowser.Model.Tasks;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.Trickplay
|
namespace MediaBrowser.Providers.Trickplay;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class TrickplayImagesTask.
|
||||||
|
/// </summary>
|
||||||
|
public class TrickplayImagesTask : IScheduledTask
|
||||||
{
|
{
|
||||||
|
private readonly ILogger<TrickplayImagesTask> _logger;
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
private readonly ILocalizationManager _localization;
|
||||||
|
private readonly ITrickplayManager _trickplayManager;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class TrickplayImagesTask.
|
/// Initializes a new instance of the <see cref="TrickplayImagesTask"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TrickplayImagesTask : IScheduledTask
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
|
/// <param name="localization">The localization manager.</param>
|
||||||
|
/// <param name="trickplayManager">The trickplay manager.</param>
|
||||||
|
public TrickplayImagesTask(
|
||||||
|
ILogger<TrickplayImagesTask> logger,
|
||||||
|
ILibraryManager libraryManager,
|
||||||
|
ILocalizationManager localization,
|
||||||
|
ITrickplayManager trickplayManager)
|
||||||
{
|
{
|
||||||
private readonly ILogger<TrickplayImagesTask> _logger;
|
_libraryManager = libraryManager;
|
||||||
private readonly ILibraryManager _libraryManager;
|
_logger = logger;
|
||||||
private readonly ILocalizationManager _localization;
|
_localization = localization;
|
||||||
private readonly ITrickplayManager _trickplayManager;
|
_trickplayManager = trickplayManager;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc />
|
||||||
/// Initializes a new instance of the <see cref="TrickplayImagesTask"/> class.
|
public string Name => _localization.GetLocalizedString("TaskRefreshTrickplayImages");
|
||||||
/// </summary>
|
|
||||||
/// <param name="logger">The logger.</param>
|
/// <inheritdoc />
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
public string Description => _localization.GetLocalizedString("TaskRefreshTrickplayImagesDescription");
|
||||||
/// <param name="localization">The localization manager.</param>
|
|
||||||
/// <param name="trickplayManager">The trickplay manager.</param>
|
/// <inheritdoc />
|
||||||
public TrickplayImagesTask(
|
public string Key => "RefreshTrickplayImages";
|
||||||
ILogger<TrickplayImagesTask> logger,
|
|
||||||
ILibraryManager libraryManager,
|
/// <inheritdoc />
|
||||||
ILocalizationManager localization,
|
public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
|
||||||
ITrickplayManager trickplayManager)
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
||||||
|
{
|
||||||
|
return new[]
|
||||||
{
|
{
|
||||||
_libraryManager = libraryManager;
|
new TaskTriggerInfo
|
||||||
_logger = logger;
|
|
||||||
_localization = localization;
|
|
||||||
_trickplayManager = trickplayManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string Name => _localization.GetLocalizedString("TaskRefreshTrickplayImages");
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string Description => _localization.GetLocalizedString("TaskRefreshTrickplayImagesDescription");
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string Key => "RefreshTrickplayImages";
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
|
||||||
{
|
|
||||||
return new[]
|
|
||||||
{
|
{
|
||||||
new TaskTriggerInfo
|
Type = TaskTriggerInfo.TriggerDaily,
|
||||||
{
|
TimeOfDayTicks = TimeSpan.FromHours(3).Ticks
|
||||||
Type = TaskTriggerInfo.TriggerDaily,
|
|
||||||
TimeOfDayTicks = TimeSpan.FromHours(3).Ticks
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var items = _libraryManager.GetItemList(new InternalItemsQuery
|
|
||||||
{
|
|
||||||
MediaTypes = new[] { MediaType.Video },
|
|
||||||
IsVirtualItem = false,
|
|
||||||
IsFolder = false,
|
|
||||||
Recursive = true
|
|
||||||
}).OfType<Video>().ToList();
|
|
||||||
|
|
||||||
var numComplete = 0;
|
|
||||||
|
|
||||||
foreach (var item in items)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
await _trickplayManager.RefreshTrickplayData(item, false, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
catch (OperationCanceledException)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError("Error creating trickplay files for {ItemName}: {Msg}", item.Name, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
numComplete++;
|
|
||||||
double percent = numComplete;
|
|
||||||
percent /= items.Count;
|
|
||||||
percent *= 100;
|
|
||||||
|
|
||||||
progress.Report(percent);
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var items = _libraryManager.GetItemList(new InternalItemsQuery
|
||||||
|
{
|
||||||
|
MediaTypes = new[] { MediaType.Video },
|
||||||
|
IsVirtualItem = false,
|
||||||
|
IsFolder = false,
|
||||||
|
Recursive = true
|
||||||
|
}).OfType<Video>().ToList();
|
||||||
|
|
||||||
|
var numComplete = 0;
|
||||||
|
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
await _trickplayManager.RefreshTrickplayDataAsync(item, false, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Error creating trickplay files for {ItemName}: {Msg}", item.Name, ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
numComplete++;
|
||||||
|
double percent = numComplete;
|
||||||
|
percent /= items.Count;
|
||||||
|
percent *= 100;
|
||||||
|
|
||||||
|
progress.Report(percent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,370 +17,369 @@ using MediaBrowser.Model.IO;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SkiaSharp;
|
using SkiaSharp;
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.Trickplay
|
namespace MediaBrowser.Providers.Trickplay;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// ITrickplayManager implementation.
|
||||||
|
/// </summary>
|
||||||
|
public class TrickplayManager : ITrickplayManager
|
||||||
{
|
{
|
||||||
|
private readonly ILogger<TrickplayManager> _logger;
|
||||||
|
private readonly IItemRepository _itemRepo;
|
||||||
|
private readonly IMediaEncoder _mediaEncoder;
|
||||||
|
private readonly IFileSystem _fileSystem;
|
||||||
|
private readonly EncodingHelper _encodingHelper;
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
private readonly IServerConfigurationManager _config;
|
||||||
|
|
||||||
|
private static readonly SemaphoreSlim _resourcePool = new(1, 1);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ITrickplayManager implementation.
|
/// Initializes a new instance of the <see cref="TrickplayManager"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TrickplayManager : ITrickplayManager
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="itemRepo">The item repository.</param>
|
||||||
|
/// <param name="mediaEncoder">The media encoder.</param>
|
||||||
|
/// <param name="fileSystem">The file systen.</param>
|
||||||
|
/// <param name="encodingHelper">The encoding helper.</param>
|
||||||
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
|
/// <param name="config">The server configuration manager.</param>
|
||||||
|
public TrickplayManager(
|
||||||
|
ILogger<TrickplayManager> logger,
|
||||||
|
IItemRepository itemRepo,
|
||||||
|
IMediaEncoder mediaEncoder,
|
||||||
|
IFileSystem fileSystem,
|
||||||
|
EncodingHelper encodingHelper,
|
||||||
|
ILibraryManager libraryManager,
|
||||||
|
IServerConfigurationManager config)
|
||||||
{
|
{
|
||||||
private readonly ILogger<TrickplayManager> _logger;
|
_logger = logger;
|
||||||
private readonly IItemRepository _itemRepo;
|
_itemRepo = itemRepo;
|
||||||
private readonly IMediaEncoder _mediaEncoder;
|
_mediaEncoder = mediaEncoder;
|
||||||
private readonly IFileSystem _fileSystem;
|
_fileSystem = fileSystem;
|
||||||
private readonly EncodingHelper _encodingHelper;
|
_encodingHelper = encodingHelper;
|
||||||
private readonly ILibraryManager _libraryManager;
|
_libraryManager = libraryManager;
|
||||||
private readonly IServerConfigurationManager _config;
|
_config = config;
|
||||||
|
}
|
||||||
|
|
||||||
private static readonly SemaphoreSlim _resourcePool = new(1, 1);
|
/// <inheritdoc />
|
||||||
|
public async Task RefreshTrickplayDataAsync(Video video, bool replace, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Trickplay refresh for {ItemId} (replace existing: {Replace})", video.Id, replace);
|
||||||
|
|
||||||
/// <summary>
|
var options = _config.Configuration.TrickplayOptions;
|
||||||
/// Initializes a new instance of the <see cref="TrickplayManager"/> class.
|
foreach (var width in options.WidthResolutions)
|
||||||
/// </summary>
|
|
||||||
/// <param name="logger">The logger.</param>
|
|
||||||
/// <param name="itemRepo">The item repository.</param>
|
|
||||||
/// <param name="mediaEncoder">The media encoder.</param>
|
|
||||||
/// <param name="fileSystem">The file systen.</param>
|
|
||||||
/// <param name="encodingHelper">The encoding helper.</param>
|
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
|
||||||
/// <param name="config">The server configuration manager.</param>
|
|
||||||
public TrickplayManager(
|
|
||||||
ILogger<TrickplayManager> logger,
|
|
||||||
IItemRepository itemRepo,
|
|
||||||
IMediaEncoder mediaEncoder,
|
|
||||||
IFileSystem fileSystem,
|
|
||||||
EncodingHelper encodingHelper,
|
|
||||||
ILibraryManager libraryManager,
|
|
||||||
IServerConfigurationManager config)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
_itemRepo = itemRepo;
|
await RefreshTrickplayDataInternal(
|
||||||
_mediaEncoder = mediaEncoder;
|
video,
|
||||||
_fileSystem = fileSystem;
|
replace,
|
||||||
_encodingHelper = encodingHelper;
|
width,
|
||||||
_libraryManager = libraryManager;
|
options,
|
||||||
_config = config;
|
cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task RefreshTrickplayDataInternal(
|
||||||
|
Video video,
|
||||||
|
bool replace,
|
||||||
|
int width,
|
||||||
|
TrickplayOptions options,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (!CanGenerateTrickplay(video, options.Interval))
|
||||||
|
{
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
var imgTempDir = string.Empty;
|
||||||
public async Task RefreshTrickplayData(Video video, bool replace, CancellationToken cancellationToken)
|
var outputDir = GetTrickplayDirectory(video, width);
|
||||||
{
|
|
||||||
_logger.LogDebug("Trickplay refresh for {ItemId} (replace existing: {Replace})", video.Id, replace);
|
|
||||||
|
|
||||||
var options = _config.Configuration.TrickplayOptions;
|
try
|
||||||
foreach (var width in options.WidthResolutions)
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
await RefreshTrickplayDataInternal(
|
|
||||||
video,
|
|
||||||
replace,
|
|
||||||
width,
|
|
||||||
options,
|
|
||||||
cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task RefreshTrickplayDataInternal(
|
|
||||||
Video video,
|
|
||||||
bool replace,
|
|
||||||
int width,
|
|
||||||
TrickplayOptions options,
|
|
||||||
CancellationToken cancellationToken)
|
|
||||||
{
|
{
|
||||||
if (!CanGenerateTrickplay(video, options.Interval))
|
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (!replace && Directory.Exists(outputDir) && GetTilesResolutions(video.Id).ContainsKey(width))
|
||||||
{
|
{
|
||||||
|
_logger.LogDebug("Found existing trickplay files for {ItemId}. Exiting.", video.Id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var imgTempDir = string.Empty;
|
// Extract images
|
||||||
var outputDir = GetTrickplayDirectory(video, width);
|
// Note: Media sources under parent items exist as their own video/item as well. Only use this video stream for trickplay.
|
||||||
|
var mediaSource = video.GetMediaSources(false).Find(source => Guid.Parse(source.Id).Equals(video.Id));
|
||||||
|
|
||||||
|
if (mediaSource is null)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Found no matching media source for item {ItemId}", video.Id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mediaPath = mediaSource.Path;
|
||||||
|
var mediaStream = mediaSource.VideoStream;
|
||||||
|
var container = mediaSource.Container;
|
||||||
|
|
||||||
|
_logger.LogInformation("Creating trickplay files at {Width} width, for {Path} [ID: {ItemId}]", width, mediaPath, video.Id);
|
||||||
|
imgTempDir = await _mediaEncoder.ExtractVideoImagesOnIntervalAccelerated(
|
||||||
|
mediaPath,
|
||||||
|
container,
|
||||||
|
mediaSource,
|
||||||
|
mediaStream,
|
||||||
|
width,
|
||||||
|
TimeSpan.FromMilliseconds(options.Interval),
|
||||||
|
options.EnableHwAcceleration,
|
||||||
|
options.ProcessThreads,
|
||||||
|
options.Qscale,
|
||||||
|
options.ProcessPriority,
|
||||||
|
_encodingHelper,
|
||||||
|
cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(imgTempDir) || !Directory.Exists(imgTempDir))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Null or invalid directory from media encoder.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var images = _fileSystem.GetFiles(imgTempDir, new string[] { ".jpg" }, false, false)
|
||||||
|
.Where(img => string.Equals(img.Extension, ".jpg", StringComparison.Ordinal))
|
||||||
|
.OrderBy(i => i.FullName)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
// Create tiles
|
||||||
|
var tilesTempDir = Path.Combine(imgTempDir, Guid.NewGuid().ToString("N"));
|
||||||
|
var tilesInfo = CreateTiles(images, width, options, tilesTempDir, outputDir);
|
||||||
|
|
||||||
|
// Save tiles info
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
if (tilesInfo is not null)
|
||||||
|
|
||||||
if (!replace && Directory.Exists(outputDir) && GetTilesResolutions(video.Id).ContainsKey(width))
|
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Found existing trickplay files for {ItemId}. Exiting.", video.Id);
|
SaveTilesInfo(video.Id, tilesInfo);
|
||||||
return;
|
_logger.LogInformation("Finished creation of trickplay files for {0}", mediaPath);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// Extract images
|
|
||||||
// Note: Media sources under parent items exist as their own video/item as well. Only use this video stream for trickplay.
|
|
||||||
var mediaSource = video.GetMediaSources(false).Find(source => Guid.Parse(source.Id).Equals(video.Id));
|
|
||||||
|
|
||||||
if (mediaSource is null)
|
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Found no matching media source for item {ItemId}", video.Id);
|
throw new InvalidOperationException("Null trickplay tiles info from CreateTiles.");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var mediaPath = mediaSource.Path;
|
|
||||||
var mediaStream = mediaSource.VideoStream;
|
|
||||||
var container = mediaSource.Container;
|
|
||||||
|
|
||||||
_logger.LogInformation("Creating trickplay files at {Width} width, for {Path} [ID: {ItemId}]", width, mediaPath, video.Id);
|
|
||||||
imgTempDir = await _mediaEncoder.ExtractVideoImagesOnIntervalAccelerated(
|
|
||||||
mediaPath,
|
|
||||||
container,
|
|
||||||
mediaSource,
|
|
||||||
mediaStream,
|
|
||||||
width,
|
|
||||||
TimeSpan.FromMilliseconds(options.Interval),
|
|
||||||
options.EnableHwAcceleration,
|
|
||||||
options.ProcessThreads,
|
|
||||||
options.Qscale,
|
|
||||||
options.ProcessPriority,
|
|
||||||
_encodingHelper,
|
|
||||||
cancellationToken).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(imgTempDir) || !Directory.Exists(imgTempDir))
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Null or invalid directory from media encoder.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var images = _fileSystem.GetFiles(imgTempDir, new string[] { ".jpg" }, false, false)
|
|
||||||
.Where(img => string.Equals(img.Extension, ".jpg", StringComparison.Ordinal))
|
|
||||||
.OrderBy(i => i.FullName)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
// Create tiles
|
|
||||||
var tilesTempDir = Path.Combine(imgTempDir, Guid.NewGuid().ToString("N"));
|
|
||||||
var tilesInfo = CreateTiles(images, width, options, tilesTempDir, outputDir);
|
|
||||||
|
|
||||||
// Save tiles info
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (tilesInfo is not null)
|
|
||||||
{
|
|
||||||
SaveTilesInfo(video.Id, tilesInfo);
|
|
||||||
_logger.LogInformation("Finished creation of trickplay files for {0}", mediaPath);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Null trickplay tiles info from CreateTiles.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "Error while saving trickplay tiles info.");
|
|
||||||
|
|
||||||
// Make sure no files stay in metadata folders on failure
|
|
||||||
// if tiles info wasn't saved.
|
|
||||||
Directory.Delete(outputDir, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error creating trickplay images.");
|
_logger.LogError(ex, "Error while saving trickplay tiles info.");
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_resourcePool.Release();
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(imgTempDir))
|
// Make sure no files stay in metadata folders on failure
|
||||||
{
|
// if tiles info wasn't saved.
|
||||||
Directory.Delete(imgTempDir, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private TrickplayTilesInfo CreateTiles(List<FileSystemMetadata> images, int width, TrickplayOptions options, string workDir, string outputDir)
|
|
||||||
{
|
|
||||||
if (images.Count == 0)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Can't create trickplay from 0 images.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Directory.CreateDirectory(workDir);
|
|
||||||
|
|
||||||
var tilesInfo = new TrickplayTilesInfo
|
|
||||||
{
|
|
||||||
Width = width,
|
|
||||||
Interval = options.Interval,
|
|
||||||
TileWidth = options.TileWidth,
|
|
||||||
TileHeight = options.TileHeight,
|
|
||||||
TileCount = 0,
|
|
||||||
Bandwidth = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
var firstImg = SKBitmap.Decode(images[0].FullName);
|
|
||||||
if (firstImg == null)
|
|
||||||
{
|
|
||||||
throw new InvalidDataException("Could not decode image data.");
|
|
||||||
}
|
|
||||||
|
|
||||||
tilesInfo.Height = firstImg.Height;
|
|
||||||
if (tilesInfo.Width != firstImg.Width)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Image width does not match config width.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generate grids of trickplay image tiles
|
|
||||||
*/
|
|
||||||
var imgNo = 0;
|
|
||||||
var i = 0;
|
|
||||||
while (i < images.Count)
|
|
||||||
{
|
|
||||||
var tileGrid = new SKBitmap(tilesInfo.Width * tilesInfo.TileWidth, tilesInfo.Height * tilesInfo.TileHeight);
|
|
||||||
|
|
||||||
using (var canvas = new SKCanvas(tileGrid))
|
|
||||||
{
|
|
||||||
for (var y = 0; y < tilesInfo.TileHeight; y++)
|
|
||||||
{
|
|
||||||
for (var x = 0; x < tilesInfo.TileWidth; x++)
|
|
||||||
{
|
|
||||||
if (i >= images.Count)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
var img = SKBitmap.Decode(images[i].FullName);
|
|
||||||
if (img == null)
|
|
||||||
{
|
|
||||||
throw new InvalidDataException("Could not decode image data.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tilesInfo.Width != img.Width)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Image width does not match config width.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tilesInfo.Height != img.Height)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Image height does not match first image height.");
|
|
||||||
}
|
|
||||||
|
|
||||||
canvas.DrawBitmap(img, x * tilesInfo.Width, y * tilesInfo.Height);
|
|
||||||
tilesInfo.TileCount++;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output each tile grid to singular file
|
|
||||||
var tileGridPath = Path.Combine(workDir, $"{imgNo}.jpg");
|
|
||||||
using (var stream = File.OpenWrite(tileGridPath))
|
|
||||||
{
|
|
||||||
tileGrid.Encode(stream, SKEncodedImageFormat.Jpeg, options.JpegQuality);
|
|
||||||
}
|
|
||||||
|
|
||||||
var bitrate = (int)Math.Ceiling((decimal)new FileInfo(tileGridPath).Length * 8 / tilesInfo.TileWidth / tilesInfo.TileHeight / (tilesInfo.Interval / 1000));
|
|
||||||
tilesInfo.Bandwidth = Math.Max(tilesInfo.Bandwidth, bitrate);
|
|
||||||
|
|
||||||
imgNo++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Move trickplay tiles to output directory
|
|
||||||
*/
|
|
||||||
Directory.CreateDirectory(outputDir);
|
|
||||||
|
|
||||||
// Replace existing tile grids if they already exist
|
|
||||||
if (Directory.Exists(outputDir))
|
|
||||||
{
|
|
||||||
Directory.Delete(outputDir, true);
|
Directory.Delete(outputDir, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
MoveDirectory(workDir, outputDir);
|
|
||||||
|
|
||||||
return tilesInfo;
|
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
private bool CanGenerateTrickplay(Video video, int interval)
|
|
||||||
{
|
{
|
||||||
var videoType = video.VideoType;
|
_logger.LogError(ex, "Error creating trickplay images.");
|
||||||
if (videoType == VideoType.Iso || videoType == VideoType.Dvd || videoType == VideoType.BluRay)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (video.IsPlaceHolder)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (video.IsShortcut)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!video.IsCompleteMedia)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value < TimeSpan.FromMilliseconds(interval).Ticks)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var libraryOptions = _libraryManager.GetLibraryOptions(video);
|
|
||||||
if (libraryOptions is not null)
|
|
||||||
{
|
|
||||||
if (!libraryOptions.EnableTrickplayImageExtraction)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Can't extract images if there are no video streams
|
|
||||||
return video.GetMediaStreams().Count > 0;
|
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
/// <inheritdoc />
|
|
||||||
public Dictionary<int, TrickplayTilesInfo> GetTilesResolutions(Guid itemId)
|
|
||||||
{
|
{
|
||||||
return _itemRepo.GetTilesResolutions(itemId);
|
_resourcePool.Release();
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
if (!string.IsNullOrEmpty(imgTempDir))
|
||||||
public void SaveTilesInfo(Guid itemId, TrickplayTilesInfo tilesInfo)
|
|
||||||
{
|
|
||||||
_itemRepo.SaveTilesInfo(itemId, tilesInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Dictionary<Guid, Dictionary<int, TrickplayTilesInfo>> GetTrickplayManifest(BaseItem item)
|
|
||||||
{
|
|
||||||
return _itemRepo.GetTrickplayManifest(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string GetTrickplayTilePath(BaseItem item, int width, int index)
|
|
||||||
{
|
|
||||||
return Path.Combine(GetTrickplayDirectory(item, width), index + ".jpg");
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetTrickplayDirectory(BaseItem item, int? width = null)
|
|
||||||
{
|
|
||||||
var path = Path.Combine(item.GetInternalMetadataPath(), "trickplay");
|
|
||||||
|
|
||||||
return width.HasValue ? Path.Combine(path, width.Value.ToString(CultureInfo.InvariantCulture)) : path;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void MoveDirectory(string source, string destination)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
Directory.Move(source, destination);
|
Directory.Delete(imgTempDir, true);
|
||||||
}
|
|
||||||
catch (IOException)
|
|
||||||
{
|
|
||||||
// Cross device move requires a copy
|
|
||||||
Directory.CreateDirectory(destination);
|
|
||||||
foreach (string file in Directory.GetFiles(source))
|
|
||||||
{
|
|
||||||
File.Copy(file, Path.Join(destination, Path.GetFileName(file)), true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Directory.Delete(source, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TrickplayTilesInfo CreateTiles(List<FileSystemMetadata> images, int width, TrickplayOptions options, string workDir, string outputDir)
|
||||||
|
{
|
||||||
|
if (images.Count == 0)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Can't create trickplay from 0 images.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Directory.CreateDirectory(workDir);
|
||||||
|
|
||||||
|
var tilesInfo = new TrickplayTilesInfo
|
||||||
|
{
|
||||||
|
Width = width,
|
||||||
|
Interval = options.Interval,
|
||||||
|
TileWidth = options.TileWidth,
|
||||||
|
TileHeight = options.TileHeight,
|
||||||
|
TileCount = 0,
|
||||||
|
Bandwidth = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
var firstImg = SKBitmap.Decode(images[0].FullName);
|
||||||
|
if (firstImg == null)
|
||||||
|
{
|
||||||
|
throw new InvalidDataException("Could not decode image data.");
|
||||||
|
}
|
||||||
|
|
||||||
|
tilesInfo.Height = firstImg.Height;
|
||||||
|
if (tilesInfo.Width != firstImg.Width)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Image width does not match config width.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generate grids of trickplay image tiles
|
||||||
|
*/
|
||||||
|
var imgNo = 0;
|
||||||
|
var i = 0;
|
||||||
|
while (i < images.Count)
|
||||||
|
{
|
||||||
|
var tileGrid = new SKBitmap(tilesInfo.Width * tilesInfo.TileWidth, tilesInfo.Height * tilesInfo.TileHeight);
|
||||||
|
|
||||||
|
using (var canvas = new SKCanvas(tileGrid))
|
||||||
|
{
|
||||||
|
for (var y = 0; y < tilesInfo.TileHeight; y++)
|
||||||
|
{
|
||||||
|
for (var x = 0; x < tilesInfo.TileWidth; x++)
|
||||||
|
{
|
||||||
|
if (i >= images.Count)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var img = SKBitmap.Decode(images[i].FullName);
|
||||||
|
if (img == null)
|
||||||
|
{
|
||||||
|
throw new InvalidDataException("Could not decode image data.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tilesInfo.Width != img.Width)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Image width does not match config width.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tilesInfo.Height != img.Height)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Image height does not match first image height.");
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.DrawBitmap(img, x * tilesInfo.Width, y * tilesInfo.Height);
|
||||||
|
tilesInfo.TileCount++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output each tile grid to singular file
|
||||||
|
var tileGridPath = Path.Combine(workDir, $"{imgNo}.jpg");
|
||||||
|
using (var stream = File.OpenWrite(tileGridPath))
|
||||||
|
{
|
||||||
|
tileGrid.Encode(stream, SKEncodedImageFormat.Jpeg, options.JpegQuality);
|
||||||
|
}
|
||||||
|
|
||||||
|
var bitrate = (int)Math.Ceiling((decimal)new FileInfo(tileGridPath).Length * 8 / tilesInfo.TileWidth / tilesInfo.TileHeight / (tilesInfo.Interval / 1000));
|
||||||
|
tilesInfo.Bandwidth = Math.Max(tilesInfo.Bandwidth, bitrate);
|
||||||
|
|
||||||
|
imgNo++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Move trickplay tiles to output directory
|
||||||
|
*/
|
||||||
|
Directory.CreateDirectory(outputDir);
|
||||||
|
|
||||||
|
// Replace existing tile grids if they already exist
|
||||||
|
if (Directory.Exists(outputDir))
|
||||||
|
{
|
||||||
|
Directory.Delete(outputDir, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveDirectory(workDir, outputDir);
|
||||||
|
|
||||||
|
return tilesInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool CanGenerateTrickplay(Video video, int interval)
|
||||||
|
{
|
||||||
|
var videoType = video.VideoType;
|
||||||
|
if (videoType == VideoType.Iso || videoType == VideoType.Dvd || videoType == VideoType.BluRay)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (video.IsPlaceHolder)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (video.IsShortcut)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!video.IsCompleteMedia)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value < TimeSpan.FromMilliseconds(interval).Ticks)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var libraryOptions = _libraryManager.GetLibraryOptions(video);
|
||||||
|
if (libraryOptions is not null)
|
||||||
|
{
|
||||||
|
if (!libraryOptions.EnableTrickplayImageExtraction)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can't extract images if there are no video streams
|
||||||
|
return video.GetMediaStreams().Count > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Dictionary<int, TrickplayTilesInfo> GetTilesResolutions(Guid itemId)
|
||||||
|
{
|
||||||
|
return _itemRepo.GetTilesResolutions(itemId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void SaveTilesInfo(Guid itemId, TrickplayTilesInfo tilesInfo)
|
||||||
|
{
|
||||||
|
_itemRepo.SaveTilesInfo(itemId, tilesInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Dictionary<Guid, Dictionary<int, TrickplayTilesInfo>> GetTrickplayManifest(BaseItem item)
|
||||||
|
{
|
||||||
|
return _itemRepo.GetTrickplayManifest(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string GetTrickplayTilePath(BaseItem item, int width, int index)
|
||||||
|
{
|
||||||
|
return Path.Combine(GetTrickplayDirectory(item, width), index + ".jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetTrickplayDirectory(BaseItem item, int? width = null)
|
||||||
|
{
|
||||||
|
var path = Path.Combine(item.GetInternalMetadataPath(), "trickplay");
|
||||||
|
|
||||||
|
return width.HasValue ? Path.Combine(path, width.Value.ToString(CultureInfo.InvariantCulture)) : path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void MoveDirectory(string source, string destination)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.Move(source, destination);
|
||||||
|
}
|
||||||
|
catch (IOException)
|
||||||
|
{
|
||||||
|
// Cross device move requires a copy
|
||||||
|
Directory.CreateDirectory(destination);
|
||||||
|
foreach (string file in Directory.GetFiles(source))
|
||||||
|
{
|
||||||
|
File.Copy(file, Path.Join(destination, Path.GetFileName(file)), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Directory.Delete(source, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,118 +10,117 @@ using MediaBrowser.Controller.Trickplay;
|
||||||
using MediaBrowser.Model.Configuration;
|
using MediaBrowser.Model.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.Trickplay
|
namespace MediaBrowser.Providers.Trickplay;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class TrickplayProvider. Provides images and metadata for trickplay
|
||||||
|
/// scrubbing previews.
|
||||||
|
/// </summary>
|
||||||
|
public class TrickplayProvider : ICustomMetadataProvider<Episode>,
|
||||||
|
ICustomMetadataProvider<MusicVideo>,
|
||||||
|
ICustomMetadataProvider<Movie>,
|
||||||
|
ICustomMetadataProvider<Trailer>,
|
||||||
|
ICustomMetadataProvider<Video>,
|
||||||
|
IHasItemChangeMonitor,
|
||||||
|
IHasOrder,
|
||||||
|
IForcedProvider
|
||||||
{
|
{
|
||||||
|
private readonly ILogger<TrickplayProvider> _logger;
|
||||||
|
private readonly IServerConfigurationManager _config;
|
||||||
|
private readonly ITrickplayManager _trickplayManager;
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class TrickplayProvider. Provides images and metadata for trickplay
|
/// Initializes a new instance of the <see cref="TrickplayProvider"/> class.
|
||||||
/// scrubbing previews.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TrickplayProvider : ICustomMetadataProvider<Episode>,
|
/// <param name="logger">The logger.</param>
|
||||||
ICustomMetadataProvider<MusicVideo>,
|
/// <param name="config">The configuration manager.</param>
|
||||||
ICustomMetadataProvider<Movie>,
|
/// <param name="trickplayManager">The trickplay manager.</param>
|
||||||
ICustomMetadataProvider<Trailer>,
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
ICustomMetadataProvider<Video>,
|
public TrickplayProvider(
|
||||||
IHasItemChangeMonitor,
|
ILogger<TrickplayProvider> logger,
|
||||||
IHasOrder,
|
IServerConfigurationManager config,
|
||||||
IForcedProvider
|
ITrickplayManager trickplayManager,
|
||||||
|
ILibraryManager libraryManager)
|
||||||
{
|
{
|
||||||
private readonly ILogger<TrickplayProvider> _logger;
|
_logger = logger;
|
||||||
private readonly IServerConfigurationManager _config;
|
_config = config;
|
||||||
private readonly ITrickplayManager _trickplayManager;
|
_trickplayManager = trickplayManager;
|
||||||
private readonly ILibraryManager _libraryManager;
|
_libraryManager = libraryManager;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc />
|
||||||
/// Initializes a new instance of the <see cref="TrickplayProvider"/> class.
|
public string Name => "Trickplay Provider";
|
||||||
/// </summary>
|
|
||||||
/// <param name="logger">The logger.</param>
|
/// <inheritdoc />
|
||||||
/// <param name="config">The configuration manager.</param>
|
public int Order => 100;
|
||||||
/// <param name="trickplayManager">The trickplay manager.</param>
|
|
||||||
/// <param name="libraryManager">The library manager.</param>
|
/// <inheritdoc />
|
||||||
public TrickplayProvider(
|
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
|
||||||
ILogger<TrickplayProvider> logger,
|
{
|
||||||
IServerConfigurationManager config,
|
if (item.IsFileProtocol)
|
||||||
ITrickplayManager trickplayManager,
|
|
||||||
ILibraryManager libraryManager)
|
|
||||||
{
|
{
|
||||||
_logger = logger;
|
var file = directoryService.GetFile(item.Path);
|
||||||
_config = config;
|
if (file is not null && item.DateModified != file.LastWriteTimeUtc)
|
||||||
_trickplayManager = trickplayManager;
|
|
||||||
_libraryManager = libraryManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string Name => "Trickplay Provider";
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public int Order => 100;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
|
|
||||||
{
|
|
||||||
if (item.IsFileProtocol)
|
|
||||||
{
|
{
|
||||||
var file = directoryService.GetFile(item.Path);
|
return true;
|
||||||
if (file is not null && item.DateModified != file.LastWriteTimeUtc)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
return false;
|
||||||
public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return FetchInternal(item, options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return FetchInternal(item, options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return FetchInternal(item, options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return FetchInternal(item, options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return FetchInternal(item, options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<ItemUpdateType> FetchInternal(Video video, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var libraryOptions = _libraryManager.GetLibraryOptions(video);
|
||||||
|
bool? enableDuringScan = libraryOptions?.ExtractTrickplayImagesDuringLibraryScan;
|
||||||
|
bool replace = options.ReplaceAllImages;
|
||||||
|
|
||||||
|
if (options.IsAutomated && !enableDuringScan.GetValueOrDefault(false))
|
||||||
{
|
{
|
||||||
return FetchInternal(item, options, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return FetchInternal(item, options, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return FetchInternal(item, options, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return FetchInternal(item, options, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return FetchInternal(item, options, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<ItemUpdateType> FetchInternal(Video video, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var libraryOptions = _libraryManager.GetLibraryOptions(video);
|
|
||||||
bool? enableDuringScan = libraryOptions?.ExtractTrickplayImagesDuringLibraryScan;
|
|
||||||
bool replace = options.ReplaceAllImages;
|
|
||||||
|
|
||||||
if (options.IsAutomated && !enableDuringScan.GetValueOrDefault(false))
|
|
||||||
{
|
|
||||||
return ItemUpdateType.None;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_config.Configuration.TrickplayOptions.ScanBehavior == TrickplayScanBehavior.Blocking)
|
|
||||||
{
|
|
||||||
await _trickplayManager.RefreshTrickplayData(video, replace, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_ = _trickplayManager.RefreshTrickplayData(video, replace, cancellationToken).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// The core doesn't need to trigger any save operations over this
|
|
||||||
return ItemUpdateType.None;
|
return ItemUpdateType.None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_config.Configuration.TrickplayOptions.ScanBehavior == TrickplayScanBehavior.Blocking)
|
||||||
|
{
|
||||||
|
await _trickplayManager.RefreshTrickplayDataAsync(video, replace, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_ = _trickplayManager.RefreshTrickplayDataAsync(video, replace, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The core doesn't need to trigger any save operations over this
|
||||||
|
return ItemUpdateType.None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue