From 6ffa9539bbfbfb1090b02cebc8a28283a8c69041 Mon Sep 17 00:00:00 2001 From: cvium Date: Tue, 11 Jan 2022 23:30:30 +0100 Subject: [PATCH] Refactor and add scheduled task --- .../ApplicationHost.cs | 4 + .../Controllers/DynamicHlsController.cs | 6 +- .../Configuration/EncodingOptions.cs | 4 +- .../Cache/CacheDecorator.cs | 87 ++++ ...aEncodingHlsServiceCollectionExtensions.cs | 39 +- .../Extractors/FfProbeKeyframeExtractor.cs | 58 +++ .../Extractors/IKeyframeExtractor.cs | 24 ++ .../Extractors/MatroskaKeyframeExtractor.cs | 48 +++ .../Jellyfin.MediaEncoding.Hls.csproj | 2 +- .../Playlist/CreateMainPlaylistRequest.cs | 101 +++-- .../Playlist/DynamicHlsPlaylistGenerator.cs | 373 +++++++----------- .../Playlist/IDynamicHlsPlaylistGenerator.cs | 21 +- .../KeyframeExtractionScheduledTask.cs | 92 +++++ .../FfProbe/FfProbeKeyframeExtractor.cs | 141 ++++--- .../FfTool/FfToolKeyframeExtractor.cs | 23 +- .../KeyframeData.cs | 45 ++- .../KeyframeExtractor.cs | 69 ---- .../Extensions/EbmlReaderExtensions.cs | 267 +++++++------ .../Matroska/MatroskaConstants.cs | 47 ++- .../Matroska/MatroskaKeyframeExtractor.cs | 105 +++-- .../Matroska/Models/Info.cs | 45 ++- .../Matroska/Models/SeekHead.cs | 59 ++- .../DynamicHlsPlaylistGeneratorTests.cs | 6 +- .../FfProbe/FfProbeKeyframeExtractorTests.cs | 2 +- 24 files changed, 924 insertions(+), 744 deletions(-) create mode 100644 src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs create mode 100644 src/Jellyfin.MediaEncoding.Hls/Extractors/FfProbeKeyframeExtractor.cs create mode 100644 src/Jellyfin.MediaEncoding.Hls/Extractors/IKeyframeExtractor.cs create mode 100644 src/Jellyfin.MediaEncoding.Hls/Extractors/MatroskaKeyframeExtractor.cs create mode 100644 src/Jellyfin.MediaEncoding.Hls/ScheduledTasks/KeyframeExtractionScheduledTask.cs delete mode 100644 src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 8ed51a194..571404040 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -47,6 +47,7 @@ using Emby.Server.Implementations.TV; using Emby.Server.Implementations.Udp; using Emby.Server.Implementations.Updates; using Jellyfin.Api.Helpers; +using Jellyfin.MediaEncoding.Hls.Playlist; using Jellyfin.Networking.Configuration; using Jellyfin.Networking.Manager; using MediaBrowser.Common; @@ -999,6 +1000,9 @@ namespace Emby.Server.Implementations // Network yield return typeof(NetworkManager).Assembly; + // Hls + yield return typeof(DynamicHlsPlaylistGenerator).Assembly; + foreach (var i in GetAssembliesWithPartsInternal()) { yield return i; diff --git a/Jellyfin.Api/Controllers/DynamicHlsController.cs b/Jellyfin.Api/Controllers/DynamicHlsController.cs index 674daa8d1..77a1170b1 100644 --- a/Jellyfin.Api/Controllers/DynamicHlsController.cs +++ b/Jellyfin.Api/Controllers/DynamicHlsController.cs @@ -848,7 +848,7 @@ namespace Jellyfin.Api.Controllers StreamOptions = streamOptions }; - return await GetVariantPlaylistInternal(streamingRequest, "main", cancellationTokenSource) + return await GetVariantPlaylistInternal(streamingRequest, cancellationTokenSource) .ConfigureAwait(false); } @@ -1013,7 +1013,7 @@ namespace Jellyfin.Api.Controllers StreamOptions = streamOptions }; - return await GetVariantPlaylistInternal(streamingRequest, "main", cancellationTokenSource) + return await GetVariantPlaylistInternal(streamingRequest, cancellationTokenSource) .ConfigureAwait(false); } @@ -1371,7 +1371,7 @@ namespace Jellyfin.Api.Controllers .ConfigureAwait(false); } - private async Task GetVariantPlaylistInternal(StreamingRequestDto streamingRequest, string name, CancellationTokenSource cancellationTokenSource) + private async Task GetVariantPlaylistInternal(StreamingRequestDto streamingRequest, CancellationTokenSource cancellationTokenSource) { using var state = await StreamingHelpers.GetStreamingState( streamingRequest, diff --git a/MediaBrowser.Model/Configuration/EncodingOptions.cs b/MediaBrowser.Model/Configuration/EncodingOptions.cs index 08f52a0c1..51917b50e 100644 --- a/MediaBrowser.Model/Configuration/EncodingOptions.cs +++ b/MediaBrowser.Model/Configuration/EncodingOptions.cs @@ -39,7 +39,7 @@ namespace MediaBrowser.Model.Configuration EnableHardwareEncoding = true; AllowHevcEncoding = false; EnableSubtitleExtraction = true; - AllowAutomaticKeyframeExtractionForExtensions = Array.Empty(); + AllowOnDemandMetadataBasedKeyframeExtractionForExtensions = Array.Empty(); HardwareDecodingCodecs = new string[] { "h264", "vc1" }; } @@ -119,6 +119,6 @@ namespace MediaBrowser.Model.Configuration public string[] HardwareDecodingCodecs { get; set; } - public string[] AllowAutomaticKeyframeExtractionForExtensions { get; set; } + public string[] AllowOnDemandMetadataBasedKeyframeExtractionForExtensions { get; set; } } } diff --git a/src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs b/src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs new file mode 100644 index 000000000..f5f79ddc5 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs @@ -0,0 +1,87 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.IO; +using System.Text.Json; +using Jellyfin.Extensions.Json; +using Jellyfin.MediaEncoding.Hls.Extractors; +using Jellyfin.MediaEncoding.Keyframes; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; + +namespace Jellyfin.MediaEncoding.Hls.Cache; + +/// +public class CacheDecorator : IKeyframeExtractor +{ + private readonly IKeyframeExtractor _keyframeExtractor; + private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; + private readonly string _keyframeCachePath; + + /// + /// Initializes a new instance of the class. + /// + /// An instance of the interface. + /// An instance of the interface. + public CacheDecorator(IApplicationPaths applicationPaths, IKeyframeExtractor keyframeExtractor) + { + _keyframeExtractor = keyframeExtractor; + ArgumentNullException.ThrowIfNull(applicationPaths); + + // TODO make the dir configurable + _keyframeCachePath = Path.Combine(applicationPaths.DataPath, "keyframes"); + } + + /// + public bool IsMetadataBased => _keyframeExtractor.IsMetadataBased; + + /// + public bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData) + { + keyframeData = null; + var cachePath = GetCachePath(_keyframeCachePath, filePath); + if (TryReadFromCache(cachePath, out var cachedResult)) + { + keyframeData = cachedResult; + return true; + } + + if (!_keyframeExtractor.TryExtractKeyframes(filePath, out var result)) + { + return false; + } + + keyframeData = result; + SaveToCache(cachePath, keyframeData); + return true; + } + + private static void SaveToCache(string cachePath, KeyframeData keyframeData) + { + var json = JsonSerializer.Serialize(keyframeData, _jsonOptions); + Directory.CreateDirectory(Path.GetDirectoryName(cachePath) ?? throw new ArgumentException($"Provided path ({cachePath}) is not valid.", nameof(cachePath))); + File.WriteAllText(cachePath, json); + } + + private static string GetCachePath(string keyframeCachePath, string filePath) + { + var lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath); + ReadOnlySpan filename = (filePath + "_" + lastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5() + ".json"; + var prefix = filename[..1]; + + return Path.Join(keyframeCachePath, prefix, filename); + } + + private static bool TryReadFromCache(string cachePath, [NotNullWhen(true)] out KeyframeData? cachedResult) + { + if (File.Exists(cachePath)) + { + var bytes = File.ReadAllBytes(cachePath); + cachedResult = JsonSerializer.Deserialize(bytes, _jsonOptions); + return cachedResult != null; + } + + cachedResult = null; + return false; + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs b/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs index 327898366..8ed4edcea 100644 --- a/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs +++ b/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs @@ -1,21 +1,36 @@ -using Jellyfin.MediaEncoding.Hls.Playlist; +using System; +using Jellyfin.MediaEncoding.Hls.Cache; +using Jellyfin.MediaEncoding.Hls.Extractors; +using Jellyfin.MediaEncoding.Hls.Playlist; using Microsoft.Extensions.DependencyInjection; -namespace Jellyfin.MediaEncoding.Hls.Extensions +namespace Jellyfin.MediaEncoding.Hls.Extensions; + +/// +/// Extensions for the interface. +/// +public static class MediaEncodingHlsServiceCollectionExtensions { /// - /// Extensions for the interface. + /// Adds the hls playlist generators to the . /// - public static class MediaEncodingHlsServiceCollectionExtensions + /// An instance of the interface. + /// The updated service collection. + public static IServiceCollection AddHlsPlaylistGenerator(this IServiceCollection serviceCollection) { - /// - /// Adds the hls playlist generators to the . - /// - /// An instance of the interface. - /// The updated service collection. - public static IServiceCollection AddHlsPlaylistGenerator(this IServiceCollection serviceCollection) + serviceCollection.AddSingletonWithDecorator(typeof(FfProbeKeyframeExtractor)); + serviceCollection.AddSingletonWithDecorator(typeof(MatroskaKeyframeExtractor)); + serviceCollection.AddSingleton(); + return serviceCollection; + } + + private static void AddSingletonWithDecorator(this IServiceCollection serviceCollection, Type type) + { + serviceCollection.AddSingleton(serviceProvider => { - return serviceCollection.AddSingleton(); - } + var extractor = ActivatorUtilities.CreateInstance(serviceProvider, type); + var decorator = ActivatorUtilities.CreateInstance(serviceProvider, extractor); + return decorator; + }); } } diff --git a/src/Jellyfin.MediaEncoding.Hls/Extractors/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Hls/Extractors/FfProbeKeyframeExtractor.cs new file mode 100644 index 000000000..f86599a23 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Extractors/FfProbeKeyframeExtractor.cs @@ -0,0 +1,58 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using Emby.Naming.Common; +using Jellyfin.Extensions; +using Jellyfin.MediaEncoding.Keyframes; +using MediaBrowser.Controller.MediaEncoding; +using Microsoft.Extensions.Logging; +using Extractor = Jellyfin.MediaEncoding.Keyframes.FfProbe.FfProbeKeyframeExtractor; + +namespace Jellyfin.MediaEncoding.Hls.Extractors; + +/// +public class FfProbeKeyframeExtractor : IKeyframeExtractor +{ + private readonly IMediaEncoder _mediaEncoder; + private readonly NamingOptions _namingOptions; + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// An instance of the interface. + /// An instance of . + /// An instance of the interface. + public FfProbeKeyframeExtractor(IMediaEncoder mediaEncoder, NamingOptions namingOptions, ILogger logger) + { + _mediaEncoder = mediaEncoder; + _namingOptions = namingOptions; + _logger = logger; + } + + /// + public bool IsMetadataBased => false; + + /// + public bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData) + { + if (!_namingOptions.VideoFileExtensions.Contains(Path.GetExtension(filePath.AsSpan()), StringComparison.OrdinalIgnoreCase)) + { + keyframeData = null; + return false; + } + + try + { + keyframeData = Extractor.GetKeyframeData(_mediaEncoder.ProbePath, filePath); + return keyframeData.KeyframeTicks.Count > 0; + } + catch (Exception ex) + { + _logger.LogError(ex, "Extracting keyframes from {FilePath} using ffprobe failed", filePath); + } + + keyframeData = null; + return false; + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Extractors/IKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Hls/Extractors/IKeyframeExtractor.cs new file mode 100644 index 000000000..497210f41 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Extractors/IKeyframeExtractor.cs @@ -0,0 +1,24 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using Jellyfin.MediaEncoding.Keyframes; + +namespace Jellyfin.MediaEncoding.Hls.Extractors; + +/// +/// Keyframe extractor. +/// +public interface IKeyframeExtractor +{ + /// + /// Gets a value indicating whether the extractor is based on container metadata. + /// + bool IsMetadataBased { get; } + + /// + /// Attempt to extract keyframes. + /// + /// The path to the file. + /// The keyframes. + /// A value indicating whether the keyframe extraction was successful. + bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData); +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Extractors/MatroskaKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Hls/Extractors/MatroskaKeyframeExtractor.cs new file mode 100644 index 000000000..ee370fb01 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Extractors/MatroskaKeyframeExtractor.cs @@ -0,0 +1,48 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using Jellyfin.MediaEncoding.Keyframes; +using Microsoft.Extensions.Logging; +using Extractor = Jellyfin.MediaEncoding.Keyframes.Matroska.MatroskaKeyframeExtractor; + +namespace Jellyfin.MediaEncoding.Hls.Extractors; + +/// +public class MatroskaKeyframeExtractor : IKeyframeExtractor +{ + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// An instance of the interface. + public MatroskaKeyframeExtractor(ILogger logger) + { + _logger = logger; + } + + /// + public bool IsMetadataBased => true; + + /// + public bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData) + { + if (filePath.AsSpan().EndsWith(".mkv", StringComparison.OrdinalIgnoreCase)) + { + keyframeData = null; + return false; + } + + try + { + keyframeData = Extractor.GetKeyframeData(filePath); + return keyframeData.KeyframeTicks.Count > 0; + } + catch (Exception ex) + { + _logger.LogError(ex, "Extracting keyframes from {FilePath} using matroska metadata failed", filePath); + } + + keyframeData = null; + return false; + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj b/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj index f5b7fb378..3c6fcc5ad 100644 --- a/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj +++ b/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs index d6db1ca6e..ac28ca26a 100644 --- a/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs @@ -1,57 +1,56 @@ -namespace Jellyfin.MediaEncoding.Hls.Playlist +namespace Jellyfin.MediaEncoding.Hls.Playlist; + +/// +/// Request class for the method. +/// +public class CreateMainPlaylistRequest { /// - /// Request class for the method. + /// Initializes a new instance of the class. /// - public class CreateMainPlaylistRequest + /// The absolute file path to the file. + /// The desired segment length in milliseconds. + /// The total duration of the file in ticks. + /// The desired segment container eg. "ts". + /// The URI prefix for the relative URL in the playlist. + /// The desired query string to append (must start with ?). + public CreateMainPlaylistRequest(string filePath, int desiredSegmentLengthMs, long totalRuntimeTicks, string segmentContainer, string endpointPrefix, string queryString) { - /// - /// Initializes a new instance of the class. - /// - /// The absolute file path to the file. - /// The desired segment length in milliseconds. - /// The total duration of the file in ticks. - /// The desired segment container eg. "ts". - /// The URI prefix for the relative URL in the playlist. - /// The desired query string to append (must start with ?). - public CreateMainPlaylistRequest(string filePath, int desiredSegmentLengthMs, long totalRuntimeTicks, string segmentContainer, string endpointPrefix, string queryString) - { - FilePath = filePath; - DesiredSegmentLengthMs = desiredSegmentLengthMs; - TotalRuntimeTicks = totalRuntimeTicks; - SegmentContainer = segmentContainer; - EndpointPrefix = endpointPrefix; - QueryString = queryString; - } - - /// - /// Gets the file path. - /// - public string FilePath { get; } - - /// - /// Gets the desired segment length in milliseconds. - /// - public int DesiredSegmentLengthMs { get; } - - /// - /// Gets the total runtime in ticks. - /// - public long TotalRuntimeTicks { get; } - - /// - /// Gets the segment container. - /// - public string SegmentContainer { get; } - - /// - /// Gets the endpoint prefix for the URL. - /// - public string EndpointPrefix { get; } - - /// - /// Gets the query string. - /// - public string QueryString { get; } + FilePath = filePath; + DesiredSegmentLengthMs = desiredSegmentLengthMs; + TotalRuntimeTicks = totalRuntimeTicks; + SegmentContainer = segmentContainer; + EndpointPrefix = endpointPrefix; + QueryString = queryString; } + + /// + /// Gets the file path. + /// + public string FilePath { get; } + + /// + /// Gets the desired segment length in milliseconds. + /// + public int DesiredSegmentLengthMs { get; } + + /// + /// Gets the total runtime in ticks. + /// + public long TotalRuntimeTicks { get; } + + /// + /// Gets the segment container. + /// + public string SegmentContainer { get; } + + /// + /// Gets the endpoint prefix for the URL. + /// + public string EndpointPrefix { get; } + + /// + /// Gets the query string. + /// + public string QueryString { get; } } diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs index cbc62eb59..5cdacaf31 100644 --- a/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs @@ -5,269 +5,200 @@ using System.Globalization; using System.IO; using System.Linq; using System.Text; -using System.Text.Json; -using Jellyfin.Extensions.Json; +using Jellyfin.MediaEncoding.Hls.Extractors; using Jellyfin.MediaEncoding.Keyframes; using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.MediaEncoding; -using Microsoft.Extensions.Logging; -namespace Jellyfin.MediaEncoding.Hls.Playlist +namespace Jellyfin.MediaEncoding.Hls.Playlist; + +/// +public class DynamicHlsPlaylistGenerator : IDynamicHlsPlaylistGenerator { - /// - public class DynamicHlsPlaylistGenerator : IDynamicHlsPlaylistGenerator + private readonly IServerConfigurationManager _serverConfigurationManager; + private readonly IKeyframeExtractor[] _extractors; + + /// + /// Initializes a new instance of the class. + /// + /// An instance of the see interface. + /// An instance of . + public DynamicHlsPlaylistGenerator(IServerConfigurationManager serverConfigurationManager, IEnumerable extractors) { - private const string DefaultContainerExtension = ".ts"; + _serverConfigurationManager = serverConfigurationManager; + _extractors = extractors.Where(e => e.IsMetadataBased).ToArray(); + } - private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; - private readonly IServerConfigurationManager _serverConfigurationManager; - private readonly IMediaEncoder _mediaEncoder; - private readonly IApplicationPaths _applicationPaths; - private readonly KeyframeExtractor _keyframeExtractor; - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// An instance of the see interface. - /// An instance of the see interface. - /// An instance of the interface. - /// An instance of the see interface. - public DynamicHlsPlaylistGenerator(IServerConfigurationManager serverConfigurationManager, IMediaEncoder mediaEncoder, IApplicationPaths applicationPaths, ILoggerFactory loggerFactory) + /// + public string CreateMainPlaylist(CreateMainPlaylistRequest request) + { + IReadOnlyList segments; + if (TryExtractKeyframes(request.FilePath, out var keyframeData)) { - _serverConfigurationManager = serverConfigurationManager; - _mediaEncoder = mediaEncoder; - _applicationPaths = applicationPaths; - _keyframeExtractor = new KeyframeExtractor(loggerFactory.CreateLogger()); - _logger = loggerFactory.CreateLogger(); + segments = ComputeSegments(keyframeData, request.DesiredSegmentLengthMs); + } + else + { + segments = ComputeEqualLengthSegments(request.DesiredSegmentLengthMs, request.TotalRuntimeTicks); } - private string KeyframeCachePath => Path.Combine(_applicationPaths.DataPath, "keyframes"); + var segmentExtension = EncodingHelper.GetSegmentFileExtension(request.SegmentContainer); - /// - public string CreateMainPlaylist(CreateMainPlaylistRequest request) + // http://ffmpeg.org/ffmpeg-all.html#toc-hls-2 + var isHlsInFmp4 = string.Equals(segmentExtension, "mp4", StringComparison.OrdinalIgnoreCase); + var hlsVersion = isHlsInFmp4 ? "7" : "3"; + + var builder = new StringBuilder(128); + + builder.AppendLine("#EXTM3U") + .AppendLine("#EXT-X-PLAYLIST-TYPE:VOD") + .Append("#EXT-X-VERSION:") + .Append(hlsVersion) + .AppendLine() + .Append("#EXT-X-TARGETDURATION:") + .Append(Math.Ceiling(segments.Count > 0 ? segments.Max() : request.DesiredSegmentLengthMs)) + .AppendLine() + .AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); + + var index = 0; + + if (isHlsInFmp4) { - IReadOnlyList segments; - if (TryExtractKeyframes(request.FilePath, out var keyframeData)) - { - segments = ComputeSegments(keyframeData, request.DesiredSegmentLengthMs); - } - else - { - segments = ComputeEqualLengthSegments(request.DesiredSegmentLengthMs, request.TotalRuntimeTicks); - } - - var segmentExtension = GetSegmentFileExtension(request.SegmentContainer); - - // http://ffmpeg.org/ffmpeg-all.html#toc-hls-2 - var isHlsInFmp4 = string.Equals(segmentExtension, "mp4", StringComparison.OrdinalIgnoreCase); - var hlsVersion = isHlsInFmp4 ? "7" : "3"; - - var builder = new StringBuilder(128); - - builder.AppendLine("#EXTM3U") - .AppendLine("#EXT-X-PLAYLIST-TYPE:VOD") - .Append("#EXT-X-VERSION:") - .Append(hlsVersion) - .AppendLine() - .Append("#EXT-X-TARGETDURATION:") - .Append(Math.Ceiling(segments.Count > 0 ? segments.Max() : request.DesiredSegmentLengthMs)) - .AppendLine() - .AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); - - var index = 0; - - if (isHlsInFmp4) - { - builder.Append("#EXT-X-MAP:URI=\"") - .Append(request.EndpointPrefix) - .Append("-1") - .Append(segmentExtension) - .Append(request.QueryString) - .Append('"') - .AppendLine(); - } - - long currentRuntimeInSeconds = 0; - foreach (var length in segments) - { - // Manually convert to ticks to avoid precision loss when converting double - var lengthTicks = Convert.ToInt64(length * TimeSpan.TicksPerSecond); - builder.Append("#EXTINF:") - .Append(length.ToString("0.000000", CultureInfo.InvariantCulture)) - .AppendLine(", nodesc") - .Append(request.EndpointPrefix) - .Append(index++) - .Append(segmentExtension) - .Append(request.QueryString) - .Append("&runtimeTicks=") - .Append(currentRuntimeInSeconds) - .Append("&actualSegmentLengthTicks=") - .Append(lengthTicks) - .AppendLine(); - - currentRuntimeInSeconds += lengthTicks; - } - - builder.AppendLine("#EXT-X-ENDLIST"); - - return builder.ToString(); + builder.Append("#EXT-X-MAP:URI=\"") + .Append(request.EndpointPrefix) + .Append("-1") + .Append(segmentExtension) + .Append(request.QueryString) + .Append('"') + .AppendLine(); } - private bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData) + long currentRuntimeInSeconds = 0; + foreach (var length in segments) { - keyframeData = null; - if (!IsExtractionAllowedForFile(filePath, _serverConfigurationManager.GetEncodingOptions().AllowAutomaticKeyframeExtractionForExtensions)) - { - return false; - } + // Manually convert to ticks to avoid precision loss when converting double + var lengthTicks = Convert.ToInt64(length * TimeSpan.TicksPerSecond); + builder.Append("#EXTINF:") + .Append(length.ToString("0.000000", CultureInfo.InvariantCulture)) + .AppendLine(", nodesc") + .Append(request.EndpointPrefix) + .Append(index++) + .Append(segmentExtension) + .Append(request.QueryString) + .Append("&runtimeTicks=") + .Append(currentRuntimeInSeconds) + .Append("&actualSegmentLengthTicks=") + .Append(lengthTicks) + .AppendLine(); - var succeeded = false; - var cachePath = GetCachePath(filePath); - if (TryReadFromCache(cachePath, out var cachedResult)) - { - keyframeData = cachedResult; - } - else - { - try - { - keyframeData = _keyframeExtractor.GetKeyframeData(filePath, _mediaEncoder.ProbePath, string.Empty); - } - catch (Exception ex) - { - _logger.LogError(ex, "Keyframe extraction failed for path {FilePath}", filePath); - return false; - } - - succeeded = keyframeData.KeyframeTicks.Count > 0; - if (succeeded) - { - CacheResult(cachePath, keyframeData); - } - } - - return succeeded; + currentRuntimeInSeconds += lengthTicks; } - private void CacheResult(string cachePath, KeyframeData keyframeData) + builder.AppendLine("#EXT-X-ENDLIST"); + + return builder.ToString(); + } + + private bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData) + { + keyframeData = null; + if (!IsExtractionAllowedForFile(filePath, _serverConfigurationManager.GetEncodingOptions().AllowOnDemandMetadataBasedKeyframeExtractionForExtensions)) { - var json = JsonSerializer.Serialize(keyframeData, _jsonOptions); - Directory.CreateDirectory(Path.GetDirectoryName(cachePath) ?? throw new ArgumentException($"Provided path ({cachePath}) is not valid.", nameof(cachePath))); - File.WriteAllText(cachePath, json); - } - - private string GetCachePath(string filePath) - { - var lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath); - ReadOnlySpan filename = (filePath + "_" + lastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5() + ".json"; - var prefix = filename.Slice(0, 1); - - return Path.Join(KeyframeCachePath, prefix, filename); - } - - private bool TryReadFromCache(string cachePath, [NotNullWhen(true)] out KeyframeData? cachedResult) - { - if (File.Exists(cachePath)) - { - var bytes = File.ReadAllBytes(cachePath); - cachedResult = JsonSerializer.Deserialize(bytes, _jsonOptions); - return cachedResult != null; - } - - cachedResult = null; return false; } - internal static bool IsExtractionAllowedForFile(ReadOnlySpan filePath, string[] allowedExtensions) + var len = _extractors.Length; + for (var i = 0; i < len; i++) { - var extension = Path.GetExtension(filePath); - if (extension.IsEmpty) + var extractor = _extractors[i]; + if (!extractor.TryExtractKeyframes(filePath, out var result)) { - return false; + continue; } - // Remove the leading dot - var extensionWithoutDot = extension[1..]; - for (var i = 0; i < allowedExtensions.Length; i++) - { - var allowedExtension = allowedExtensions[i]; - if (extensionWithoutDot.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase)) - { - return true; - } - } + keyframeData = result; + return true; + } + return false; + } + + internal static bool IsExtractionAllowedForFile(ReadOnlySpan filePath, string[] allowedExtensions) + { + var extension = Path.GetExtension(filePath); + if (extension.IsEmpty) + { return false; } - internal static IReadOnlyList ComputeSegments(KeyframeData keyframeData, int desiredSegmentLengthMs) + // Remove the leading dot + var extensionWithoutDot = extension[1..]; + for (var i = 0; i < allowedExtensions.Length; i++) { - if (keyframeData.KeyframeTicks.Count > 0 && keyframeData.TotalDuration < keyframeData.KeyframeTicks[^1]) + var allowedExtension = allowedExtensions[i].AsSpan().TrimStart('.'); + if (extensionWithoutDot.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase)) { - throw new ArgumentException("Invalid duration in keyframe data", nameof(keyframeData)); + return true; } - - long lastKeyframe = 0; - var result = new List(); - // Scale the segment length to ticks to match the keyframes - var desiredSegmentLengthTicks = TimeSpan.FromMilliseconds(desiredSegmentLengthMs).Ticks; - var desiredCutTime = desiredSegmentLengthTicks; - for (var j = 0; j < keyframeData.KeyframeTicks.Count; j++) - { - var keyframe = keyframeData.KeyframeTicks[j]; - if (keyframe >= desiredCutTime) - { - var currentSegmentLength = keyframe - lastKeyframe; - result.Add(TimeSpan.FromTicks(currentSegmentLength).TotalSeconds); - lastKeyframe = keyframe; - desiredCutTime += desiredSegmentLengthTicks; - } - } - - result.Add(TimeSpan.FromTicks(keyframeData.TotalDuration - lastKeyframe).TotalSeconds); - return result; } - internal static double[] ComputeEqualLengthSegments(int desiredSegmentLengthMs, long totalRuntimeTicks) + return false; + } + + internal static IReadOnlyList ComputeSegments(KeyframeData keyframeData, int desiredSegmentLengthMs) + { + if (keyframeData.KeyframeTicks.Count > 0 && keyframeData.TotalDuration < keyframeData.KeyframeTicks[^1]) { - if (desiredSegmentLengthMs == 0 || totalRuntimeTicks == 0) - { - throw new InvalidOperationException($"Invalid segment length ({desiredSegmentLengthMs}) or runtime ticks ({totalRuntimeTicks})"); - } - - var desiredSegmentLength = TimeSpan.FromMilliseconds(desiredSegmentLengthMs); - - var segmentLengthTicks = desiredSegmentLength.Ticks; - var wholeSegments = totalRuntimeTicks / segmentLengthTicks; - var remainingTicks = totalRuntimeTicks % segmentLengthTicks; - - var segmentsLen = wholeSegments + (remainingTicks == 0 ? 0 : 1); - var segments = new double[segmentsLen]; - for (int i = 0; i < wholeSegments; i++) - { - segments[i] = desiredSegmentLength.TotalSeconds; - } - - if (remainingTicks != 0) - { - segments[^1] = TimeSpan.FromTicks(remainingTicks).TotalSeconds; - } - - return segments; + throw new ArgumentException("Invalid duration in keyframe data", nameof(keyframeData)); } - // TODO copied from DynamicHlsController - private static string GetSegmentFileExtension(string segmentContainer) + long lastKeyframe = 0; + var result = new List(); + // Scale the segment length to ticks to match the keyframes + var desiredSegmentLengthTicks = TimeSpan.FromMilliseconds(desiredSegmentLengthMs).Ticks; + var desiredCutTime = desiredSegmentLengthTicks; + for (var j = 0; j < keyframeData.KeyframeTicks.Count; j++) { - if (!string.IsNullOrWhiteSpace(segmentContainer)) + var keyframe = keyframeData.KeyframeTicks[j]; + if (keyframe >= desiredCutTime) { - return "." + segmentContainer; + var currentSegmentLength = keyframe - lastKeyframe; + result.Add(TimeSpan.FromTicks(currentSegmentLength).TotalSeconds); + lastKeyframe = keyframe; + desiredCutTime += desiredSegmentLengthTicks; } - - return DefaultContainerExtension; } + + result.Add(TimeSpan.FromTicks(keyframeData.TotalDuration - lastKeyframe).TotalSeconds); + return result; + } + + internal static double[] ComputeEqualLengthSegments(int desiredSegmentLengthMs, long totalRuntimeTicks) + { + if (desiredSegmentLengthMs == 0 || totalRuntimeTicks == 0) + { + throw new InvalidOperationException($"Invalid segment length ({desiredSegmentLengthMs}) or runtime ticks ({totalRuntimeTicks})"); + } + + var desiredSegmentLength = TimeSpan.FromMilliseconds(desiredSegmentLengthMs); + + var segmentLengthTicks = desiredSegmentLength.Ticks; + var wholeSegments = totalRuntimeTicks / segmentLengthTicks; + var remainingTicks = totalRuntimeTicks % segmentLengthTicks; + + var segmentsLen = wholeSegments + (remainingTicks == 0 ? 0 : 1); + var segments = new double[segmentsLen]; + for (int i = 0; i < wholeSegments; i++) + { + segments[i] = desiredSegmentLength.TotalSeconds; + } + + if (remainingTicks != 0) + { + segments[^1] = TimeSpan.FromTicks(remainingTicks).TotalSeconds; + } + + return segments; } } diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs index 7e766b9a2..2626cb2dd 100644 --- a/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs @@ -1,15 +1,14 @@ -namespace Jellyfin.MediaEncoding.Hls.Playlist +namespace Jellyfin.MediaEncoding.Hls.Playlist; + +/// +/// Generator for dynamic HLS playlists where the segment lengths aren't known in advance. +/// +public interface IDynamicHlsPlaylistGenerator { /// - /// Generator for dynamic HLS playlists where the segment lengths aren't known in advance. + /// Creates the main playlist containing the main video or audio stream. /// - public interface IDynamicHlsPlaylistGenerator - { - /// - /// Creates the main playlist containing the main video or audio stream. - /// - /// An instance of the class. - /// The playlist as a formatted string. - string CreateMainPlaylist(CreateMainPlaylistRequest request); - } + /// An instance of the class. + /// The playlist as a formatted string. + string CreateMainPlaylist(CreateMainPlaylistRequest request); } diff --git a/src/Jellyfin.MediaEncoding.Hls/ScheduledTasks/KeyframeExtractionScheduledTask.cs b/src/Jellyfin.MediaEncoding.Hls/ScheduledTasks/KeyframeExtractionScheduledTask.cs new file mode 100644 index 000000000..0e5f04ece --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/ScheduledTasks/KeyframeExtractionScheduledTask.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Jellyfin.Data.Enums; +using Jellyfin.MediaEncoding.Hls.Extractors; +using MediaBrowser.Controller.Dto; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Globalization; +using MediaBrowser.Model.Tasks; + +namespace Jellyfin.MediaEncoding.Hls.ScheduledTasks; + +/// +public class KeyframeExtractionScheduledTask : IScheduledTask +{ + private readonly ILocalizationManager _localizationManager; + private readonly ILibraryManager _libraryManager; + private readonly IKeyframeExtractor[] _keyframeExtractors; + private static readonly BaseItemKind[] _itemTypes = { BaseItemKind.Episode, BaseItemKind.Movie }; + + /// + /// Initializes a new instance of the class. + /// + /// An instance of the interface. + /// An instance of the interface. + /// The keyframe extractors. + public KeyframeExtractionScheduledTask(ILocalizationManager localizationManager, ILibraryManager libraryManager, IEnumerable keyframeExtractors) + { + _localizationManager = localizationManager; + _libraryManager = libraryManager; + _keyframeExtractors = keyframeExtractors.ToArray(); + } + + /// + public string Name => "Keyframe Extractor"; + + /// + public string Key => "KeyframeExtraction"; + + /// + public string Description => "Extracts keyframes from video files to create more precise HLS playlists"; + + /// + public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory"); + + /// + public Task Execute(CancellationToken cancellationToken, IProgress progress) + { + var query = new InternalItemsQuery + { + MediaTypes = new[] { MediaType.Video }, + IsVirtualItem = false, + IncludeItemTypes = _itemTypes, + DtoOptions = new DtoOptions(true), + SourceTypes = new[] { SourceType.Library }, + Recursive = true + }; + + var videos = _libraryManager.GetItemList(query); + + // TODO parallelize with Parallel.ForEach? + for (var i = 0; i < videos.Count; i++) + { + var video = videos[i]; + // Only local files supported + if (!video.IsFileProtocol || !File.Exists(video.Path)) + { + continue; + } + + for (var j = 0; j < _keyframeExtractors.Length; j++) + { + var extractor = _keyframeExtractors[j]; + // The cache decorator will make sure to save them in the data dir + if (extractor.TryExtractKeyframes(video.Path, out _)) + { + break; + } + } + } + + return Task.CompletedTask; + } + + /// + public IEnumerable GetDefaultTriggers() => Enumerable.Empty(); +} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs index 351d880fe..320604e10 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs @@ -4,92 +4,91 @@ using System.Diagnostics; using System.Globalization; using System.IO; -namespace Jellyfin.MediaEncoding.Keyframes.FfProbe +namespace Jellyfin.MediaEncoding.Keyframes.FfProbe; + +/// +/// FfProbe based keyframe extractor. +/// +public static class FfProbeKeyframeExtractor { + private const string DefaultArguments = "-v error -skip_frame nokey -show_entries format=duration -show_entries stream=duration -show_entries packet=pts_time,flags -select_streams v -of csv \"{0}\""; + /// - /// FfProbe based keyframe extractor. + /// Extracts the keyframes using the ffprobe executable at the specified path. /// - public static class FfProbeKeyframeExtractor + /// The path to the ffprobe executable. + /// The file path. + /// An instance of . + public static KeyframeData GetKeyframeData(string ffProbePath, string filePath) { - private const string DefaultArguments = "-v error -skip_frame nokey -show_entries format=duration -show_entries stream=duration -show_entries packet=pts_time,flags -select_streams v -of csv \"{0}\""; - - /// - /// Extracts the keyframes using the ffprobe executable at the specified path. - /// - /// The path to the ffprobe executable. - /// The file path. - /// An instance of . - public static KeyframeData GetKeyframeData(string ffProbePath, string filePath) + using var process = new Process { - using var process = new Process + StartInfo = new ProcessStartInfo { - StartInfo = new ProcessStartInfo - { - FileName = ffProbePath, - Arguments = string.Format(CultureInfo.InvariantCulture, DefaultArguments, filePath), + FileName = ffProbePath, + Arguments = string.Format(CultureInfo.InvariantCulture, DefaultArguments, filePath), - CreateNoWindow = true, - UseShellExecute = false, - RedirectStandardOutput = true, + CreateNoWindow = true, + UseShellExecute = false, + RedirectStandardOutput = true, - WindowStyle = ProcessWindowStyle.Hidden, - ErrorDialog = false, - }, - EnableRaisingEvents = true - }; + WindowStyle = ProcessWindowStyle.Hidden, + ErrorDialog = false, + }, + EnableRaisingEvents = true + }; - process.Start(); + process.Start(); - return ParseStream(process.StandardOutput); - } + return ParseStream(process.StandardOutput); + } - internal static KeyframeData ParseStream(StreamReader reader) + internal static KeyframeData ParseStream(StreamReader reader) + { + var keyframes = new List(); + double streamDuration = 0; + double formatDuration = 0; + + while (!reader.EndOfStream) { - var keyframes = new List(); - double streamDuration = 0; - double formatDuration = 0; - - while (!reader.EndOfStream) + var line = reader.ReadLine().AsSpan(); + if (line.IsEmpty) { - var line = reader.ReadLine().AsSpan(); - if (line.IsEmpty) - { - continue; - } - - var firstComma = line.IndexOf(','); - var lineType = line[..firstComma]; - var rest = line[(firstComma + 1)..]; - if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase)) - { - if (rest.EndsWith(",K_")) - { - // Trim the flags from the packet line. Example line: packet,7169.079000,K_ - var keyframe = double.Parse(rest[..^3], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture); - // Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise down to 1 ms when converting double. - keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond)); - } - } - else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase)) - { - if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult)) - { - streamDuration = streamDurationResult; - } - } - else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase)) - { - if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult)) - { - formatDuration = formatDurationResult; - } - } + continue; } - // Prefer the stream duration as it should be more accurate - var duration = streamDuration > 0 ? streamDuration : formatDuration; - - return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes); + var firstComma = line.IndexOf(','); + var lineType = line[..firstComma]; + var rest = line[(firstComma + 1)..]; + if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase)) + { + if (rest.EndsWith(",K_")) + { + // Trim the flags from the packet line. Example line: packet,7169.079000,K_ + var keyframe = double.Parse(rest[..^3], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture); + // Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise down to 1 ms when converting double. + keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond)); + } + } + else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase)) + { + if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult)) + { + streamDuration = streamDurationResult; + } + } + else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase)) + { + if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult)) + { + formatDuration = formatDurationResult; + } + } } + + // Prefer the stream duration as it should be more accurate + var duration = streamDuration > 0 ? streamDuration : formatDuration; + + return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes); } } diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs index fdd5dc577..aaaca6fe1 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs @@ -1,18 +1,17 @@ using System; -namespace Jellyfin.MediaEncoding.Keyframes.FfTool +namespace Jellyfin.MediaEncoding.Keyframes.FfTool; + +/// +/// FfTool based keyframe extractor. +/// +public static class FfToolKeyframeExtractor { /// - /// FfTool based keyframe extractor. + /// Extracts the keyframes using the fftool executable at the specified path. /// - public static class FfToolKeyframeExtractor - { - /// - /// Extracts the keyframes using the fftool executable at the specified path. - /// - /// The path to the fftool executable. - /// The file path. - /// An instance of . - public static KeyframeData GetKeyframeData(string ffToolPath, string filePath) => throw new NotImplementedException(); - } + /// The path to the fftool executable. + /// The file path. + /// An instance of . + public static KeyframeData GetKeyframeData(string ffToolPath, string filePath) => throw new NotImplementedException(); } diff --git a/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs index 1683cd22a..06f9180e7 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs @@ -1,31 +1,30 @@ using System.Collections.Generic; -namespace Jellyfin.MediaEncoding.Keyframes +namespace Jellyfin.MediaEncoding.Keyframes; + +/// +/// Keyframe information for a specific file. +/// +public class KeyframeData { /// - /// Keyframe information for a specific file. + /// Initializes a new instance of the class. /// - public class KeyframeData + /// The total duration of the video stream in ticks. + /// The video keyframes in ticks. + public KeyframeData(long totalDuration, IReadOnlyList keyframeTicks) { - /// - /// Initializes a new instance of the class. - /// - /// The total duration of the video stream in ticks. - /// The video keyframes in ticks. - public KeyframeData(long totalDuration, IReadOnlyList keyframeTicks) - { - TotalDuration = totalDuration; - KeyframeTicks = keyframeTicks; - } - - /// - /// Gets the total duration of the stream in ticks. - /// - public long TotalDuration { get; } - - /// - /// Gets the keyframes in ticks. - /// - public IReadOnlyList KeyframeTicks { get; } + TotalDuration = totalDuration; + KeyframeTicks = keyframeTicks; } + + /// + /// Gets the total duration of the stream in ticks. + /// + public long TotalDuration { get; } + + /// + /// Gets the keyframes in ticks. + /// + public IReadOnlyList KeyframeTicks { get; } } diff --git a/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs deleted file mode 100644 index 5304a55f8..000000000 --- a/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.IO; -using Jellyfin.MediaEncoding.Keyframes.FfProbe; -using Jellyfin.MediaEncoding.Keyframes.FfTool; -using Jellyfin.MediaEncoding.Keyframes.Matroska; -using Microsoft.Extensions.Logging; - -namespace Jellyfin.MediaEncoding.Keyframes -{ - /// - /// Manager class for the set of keyframe extractors. - /// - public class KeyframeExtractor - { - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// An instance of the interface. - public KeyframeExtractor(ILogger logger) - { - _logger = logger; - } - - /// - /// Extracts the keyframe positions from a video file. - /// - /// Absolute file path to the media file. - /// Absolute file path to the ffprobe executable. - /// Absolute file path to the fftool executable. - /// An instance of . - public KeyframeData GetKeyframeData(string filePath, string ffProbePath, string ffToolPath) - { - var extension = Path.GetExtension(filePath.AsSpan()); - if (extension.Equals(".mkv", StringComparison.OrdinalIgnoreCase)) - { - try - { - return MatroskaKeyframeExtractor.GetKeyframeData(filePath); - } - catch (Exception ex) - { - _logger.LogError(ex, "{ExtractorType} failed to extract keyframes", nameof(MatroskaKeyframeExtractor)); - } - } - - try - { - return FfToolKeyframeExtractor.GetKeyframeData(ffToolPath, filePath); - } - catch (Exception ex) - { - _logger.LogError(ex, "{ExtractorType} failed to extract keyframes", nameof(FfToolKeyframeExtractor)); - } - - try - { - return FfProbeKeyframeExtractor.GetKeyframeData(ffProbePath, filePath); - } - catch (Exception ex) - { - _logger.LogError(ex, "{ExtractorType} failed to extract keyframes", nameof(FfProbeKeyframeExtractor)); - } - - return new KeyframeData(0, Array.Empty()); - } - } -} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs index 75d5aafe0..e068cac84 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs @@ -3,176 +3,175 @@ using System.Buffers.Binary; using Jellyfin.MediaEncoding.Keyframes.Matroska.Models; using NEbml.Core; -namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions +namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions; + +/// +/// Extension methods for the class. +/// +internal static class EbmlReaderExtensions { /// - /// Extension methods for the class. + /// Traverses the current container to find the element with identifier. /// - internal static class EbmlReaderExtensions + /// An instance of . + /// The element identifier. + /// A value indicating whether the element was found. + internal static bool FindElement(this EbmlReader reader, ulong identifier) { - /// - /// Traverses the current container to find the element with identifier. - /// - /// An instance of . - /// The element identifier. - /// A value indicating whether the element was found. - internal static bool FindElement(this EbmlReader reader, ulong identifier) + while (reader.ReadNext()) { - while (reader.ReadNext()) + if (reader.ElementId.EncodedValue == identifier) { - if (reader.ElementId.EncodedValue == identifier) - { - return true; - } + return true; } - - return false; } - /// - /// Reads the current position in the file as an unsigned integer converted from binary. - /// - /// An instance of . - /// The unsigned integer. - internal static uint ReadUIntFromBinary(this EbmlReader reader) + return false; + } + + /// + /// Reads the current position in the file as an unsigned integer converted from binary. + /// + /// An instance of . + /// The unsigned integer. + internal static uint ReadUIntFromBinary(this EbmlReader reader) + { + var buffer = new byte[4]; + reader.ReadBinary(buffer, 0, 4); + return BinaryPrimitives.ReadUInt32BigEndian(buffer); + } + + /// + /// Reads from the start of the file to retrieve the SeekHead segment. + /// + /// An instance of . + /// Instance of . + internal static SeekHead ReadSeekHead(this EbmlReader reader) + { + reader = reader ?? throw new ArgumentNullException(nameof(reader)); + + if (reader.ElementPosition != 0) { - var buffer = new byte[4]; - reader.ReadBinary(buffer, 0, 4); - return BinaryPrimitives.ReadUInt32BigEndian(buffer); + throw new InvalidOperationException("File position must be at 0"); } - /// - /// Reads from the start of the file to retrieve the SeekHead segment. - /// - /// An instance of . - /// Instance of . - internal static SeekHead ReadSeekHead(this EbmlReader reader) + // Skip the header + if (!reader.FindElement(MatroskaConstants.SegmentContainer)) { - reader = reader ?? throw new ArgumentNullException(nameof(reader)); + throw new InvalidOperationException("Expected a segment container"); + } - if (reader.ElementPosition != 0) - { - throw new InvalidOperationException("File position must be at 0"); - } + reader.EnterContainer(); - // Skip the header - if (!reader.FindElement(MatroskaConstants.SegmentContainer)) - { - throw new InvalidOperationException("Expected a segment container"); - } + long? tracksPosition = null; + long? cuesPosition = null; + long? infoPosition = null; + // The first element should be a SeekHead otherwise we'll have to search manually + if (!reader.FindElement(MatroskaConstants.SeekHead)) + { + throw new InvalidOperationException("Expected a SeekHead"); + } + reader.EnterContainer(); + while (reader.FindElement(MatroskaConstants.Seek)) + { reader.EnterContainer(); - - long? tracksPosition = null; - long? cuesPosition = null; - long? infoPosition = null; - // The first element should be a SeekHead otherwise we'll have to search manually - if (!reader.FindElement(MatroskaConstants.SeekHead)) + reader.ReadNext(); + var type = (ulong)reader.ReadUIntFromBinary(); + switch (type) { - throw new InvalidOperationException("Expected a SeekHead"); - } - - reader.EnterContainer(); - while (reader.FindElement(MatroskaConstants.Seek)) - { - reader.EnterContainer(); - reader.ReadNext(); - var type = (ulong)reader.ReadUIntFromBinary(); - switch (type) - { - case MatroskaConstants.Tracks: - reader.ReadNext(); - tracksPosition = (long)reader.ReadUInt(); - break; - case MatroskaConstants.Cues: - reader.ReadNext(); - cuesPosition = (long)reader.ReadUInt(); - break; - case MatroskaConstants.Info: - reader.ReadNext(); - infoPosition = (long)reader.ReadUInt(); - break; - } - - reader.LeaveContainer(); - - if (tracksPosition.HasValue && cuesPosition.HasValue && infoPosition.HasValue) - { + case MatroskaConstants.Tracks: + reader.ReadNext(); + tracksPosition = (long)reader.ReadUInt(); + break; + case MatroskaConstants.Cues: + reader.ReadNext(); + cuesPosition = (long)reader.ReadUInt(); + break; + case MatroskaConstants.Info: + reader.ReadNext(); + infoPosition = (long)reader.ReadUInt(); break; - } } reader.LeaveContainer(); - if (!tracksPosition.HasValue || !cuesPosition.HasValue || !infoPosition.HasValue) + if (tracksPosition.HasValue && cuesPosition.HasValue && infoPosition.HasValue) { - throw new InvalidOperationException("SeekHead is missing or does not contain Info, Tracks and Cues positions"); + break; } - - return new SeekHead(infoPosition.Value, tracksPosition.Value, cuesPosition.Value); } - /// - /// Reads from SegmentContainer to retrieve the Info segment. - /// - /// An instance of . - /// The position of the info segment relative to the Segment container. - /// Instance of . - internal static Info ReadInfo(this EbmlReader reader, long position) - { - reader.ReadAt(position); + reader.LeaveContainer(); - double? duration = null; + if (!tracksPosition.HasValue || !cuesPosition.HasValue || !infoPosition.HasValue) + { + throw new InvalidOperationException("SeekHead is missing or does not contain Info, Tracks and Cues positions"); + } + + return new SeekHead(infoPosition.Value, tracksPosition.Value, cuesPosition.Value); + } + + /// + /// Reads from SegmentContainer to retrieve the Info segment. + /// + /// An instance of . + /// The position of the info segment relative to the Segment container. + /// Instance of . + internal static Info ReadInfo(this EbmlReader reader, long position) + { + reader.ReadAt(position); + + double? duration = null; + reader.EnterContainer(); + // Mandatory element + reader.FindElement(MatroskaConstants.TimestampScale); + var timestampScale = reader.ReadUInt(); + + if (reader.FindElement(MatroskaConstants.Duration)) + { + duration = reader.ReadFloat(); + } + + reader.LeaveContainer(); + + return new Info((long)timestampScale, duration); + } + + /// + /// Enters the Tracks segment and reads all tracks to find the specified type. + /// + /// Instance of . + /// The relative position of the tracks segment. + /// The track type identifier. + /// The first track number with the specified type. + /// Stream type is not found. + internal static ulong FindFirstTrackNumberByType(this EbmlReader reader, long tracksPosition, ulong type) + { + reader.ReadAt(tracksPosition); + + reader.EnterContainer(); + while (reader.FindElement(MatroskaConstants.TrackEntry)) + { reader.EnterContainer(); // Mandatory element - reader.FindElement(MatroskaConstants.TimestampScale); - var timestampScale = reader.ReadUInt(); + reader.FindElement(MatroskaConstants.TrackNumber); + var trackNumber = reader.ReadUInt(); - if (reader.FindElement(MatroskaConstants.Duration)) - { - duration = reader.ReadFloat(); - } + // Mandatory element + reader.FindElement(MatroskaConstants.TrackType); + var trackType = reader.ReadUInt(); reader.LeaveContainer(); - - return new Info((long)timestampScale, duration); - } - - /// - /// Enters the Tracks segment and reads all tracks to find the specified type. - /// - /// Instance of . - /// The relative position of the tracks segment. - /// The track type identifier. - /// The first track number with the specified type. - /// Stream type is not found. - internal static ulong FindFirstTrackNumberByType(this EbmlReader reader, long tracksPosition, ulong type) - { - reader.ReadAt(tracksPosition); - - reader.EnterContainer(); - while (reader.FindElement(MatroskaConstants.TrackEntry)) + if (trackType == MatroskaConstants.TrackTypeVideo) { - reader.EnterContainer(); - // Mandatory element - reader.FindElement(MatroskaConstants.TrackNumber); - var trackNumber = reader.ReadUInt(); - - // Mandatory element - reader.FindElement(MatroskaConstants.TrackType); - var trackType = reader.ReadUInt(); - reader.LeaveContainer(); - if (trackType == MatroskaConstants.TrackTypeVideo) - { - reader.LeaveContainer(); - return trackNumber; - } + return trackNumber; } - - reader.LeaveContainer(); - - throw new InvalidOperationException($"No stream with type {type} found"); } + + reader.LeaveContainer(); + + throw new InvalidOperationException($"No stream with type {type} found"); } } diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs index d18418d45..0d5c2f34f 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs @@ -1,31 +1,30 @@ -namespace Jellyfin.MediaEncoding.Keyframes.Matroska +namespace Jellyfin.MediaEncoding.Keyframes.Matroska; + +/// +/// Constants for the Matroska identifiers. +/// +public static class MatroskaConstants { - /// - /// Constants for the Matroska identifiers. - /// - public static class MatroskaConstants - { - internal const ulong SegmentContainer = 0x18538067; + internal const ulong SegmentContainer = 0x18538067; - internal const ulong SeekHead = 0x114D9B74; - internal const ulong Seek = 0x4DBB; + internal const ulong SeekHead = 0x114D9B74; + internal const ulong Seek = 0x4DBB; - internal const ulong Info = 0x1549A966; - internal const ulong TimestampScale = 0x2AD7B1; - internal const ulong Duration = 0x4489; + internal const ulong Info = 0x1549A966; + internal const ulong TimestampScale = 0x2AD7B1; + internal const ulong Duration = 0x4489; - internal const ulong Tracks = 0x1654AE6B; - internal const ulong TrackEntry = 0xAE; - internal const ulong TrackNumber = 0xD7; - internal const ulong TrackType = 0x83; + internal const ulong Tracks = 0x1654AE6B; + internal const ulong TrackEntry = 0xAE; + internal const ulong TrackNumber = 0xD7; + internal const ulong TrackType = 0x83; - internal const ulong TrackTypeVideo = 0x1; - internal const ulong TrackTypeSubtitle = 0x11; + internal const ulong TrackTypeVideo = 0x1; + internal const ulong TrackTypeSubtitle = 0x11; - internal const ulong Cues = 0x1C53BB6B; - internal const ulong CueTime = 0xB3; - internal const ulong CuePoint = 0xBB; - internal const ulong CueTrackPositions = 0xB7; - internal const ulong CuePointTrackNumber = 0xF7; - } + internal const ulong Cues = 0x1C53BB6B; + internal const ulong CueTime = 0xB3; + internal const ulong CuePoint = 0xBB; + internal const ulong CueTrackPositions = 0xB7; + internal const ulong CuePointTrackNumber = 0xF7; } diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs index 6a8a55643..8bb1ff00d 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs @@ -4,73 +4,72 @@ using System.IO; using Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions; using NEbml.Core; -namespace Jellyfin.MediaEncoding.Keyframes.Matroska +namespace Jellyfin.MediaEncoding.Keyframes.Matroska; + +/// +/// The keyframe extractor for the matroska container. +/// +public static class MatroskaKeyframeExtractor { /// - /// The keyframe extractor for the matroska container. + /// Extracts the keyframes in ticks (scaled using the container timestamp scale) from the matroska container. /// - public static class MatroskaKeyframeExtractor + /// The file path. + /// An instance of . + public static KeyframeData GetKeyframeData(string filePath) { - /// - /// Extracts the keyframes in ticks (scaled using the container timestamp scale) from the matroska container. - /// - /// The file path. - /// An instance of . - public static KeyframeData GetKeyframeData(string filePath) + using var stream = File.OpenRead(filePath); + using var reader = new EbmlReader(stream); + + var seekHead = reader.ReadSeekHead(); + var info = reader.ReadInfo(seekHead.InfoPosition); + var videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo); + + var keyframes = new List(); + reader.ReadAt(seekHead.CuesPosition); + reader.EnterContainer(); + + while (reader.FindElement(MatroskaConstants.CuePoint)) { - using var stream = File.OpenRead(filePath); - using var reader = new EbmlReader(stream); - - var seekHead = reader.ReadSeekHead(); - var info = reader.ReadInfo(seekHead.InfoPosition); - var videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo); - - var keyframes = new List(); - reader.ReadAt(seekHead.CuesPosition); reader.EnterContainer(); + ulong? trackNumber = null; + // Mandatory element + reader.FindElement(MatroskaConstants.CueTime); + var cueTime = reader.ReadUInt(); - while (reader.FindElement(MatroskaConstants.CuePoint)) + // Mandatory element + reader.FindElement(MatroskaConstants.CueTrackPositions); + reader.EnterContainer(); + if (reader.FindElement(MatroskaConstants.CuePointTrackNumber)) { - reader.EnterContainer(); - ulong? trackNumber = null; - // Mandatory element - reader.FindElement(MatroskaConstants.CueTime); - var cueTime = reader.ReadUInt(); - - // Mandatory element - reader.FindElement(MatroskaConstants.CueTrackPositions); - reader.EnterContainer(); - if (reader.FindElement(MatroskaConstants.CuePointTrackNumber)) - { - trackNumber = reader.ReadUInt(); - } - - reader.LeaveContainer(); - - if (trackNumber == videoTrackNumber) - { - keyframes.Add(ScaleToTicks(cueTime, info.TimestampScale)); - } - - reader.LeaveContainer(); + trackNumber = reader.ReadUInt(); } reader.LeaveContainer(); - var result = new KeyframeData(ScaleToTicks(info.Duration ?? 0, info.TimestampScale), keyframes); - return result; + if (trackNumber == videoTrackNumber) + { + keyframes.Add(ScaleToTicks(cueTime, info.TimestampScale)); + } + + reader.LeaveContainer(); } - private static long ScaleToTicks(ulong unscaledValue, long timestampScale) - { - // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns - return (long)unscaledValue * timestampScale / 100; - } + reader.LeaveContainer(); - private static long ScaleToTicks(double unscaledValue, long timestampScale) - { - // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns - return Convert.ToInt64(unscaledValue * timestampScale / 100); - } + var result = new KeyframeData(ScaleToTicks(info.Duration ?? 0, info.TimestampScale), keyframes); + return result; + } + + private static long ScaleToTicks(ulong unscaledValue, long timestampScale) + { + // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns + return (long)unscaledValue * timestampScale / 100; + } + + private static long ScaleToTicks(double unscaledValue, long timestampScale) + { + // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns + return Convert.ToInt64(unscaledValue * timestampScale / 100); } } diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs index 02c6741ec..415d6da00 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs @@ -1,29 +1,28 @@ -namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models +namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models; + +/// +/// The matroska Info segment. +/// +internal class Info { /// - /// The matroska Info segment. + /// Initializes a new instance of the class. /// - internal class Info + /// The timestamp scale in nanoseconds. + /// The duration of the entire file. + public Info(long timestampScale, double? duration) { - /// - /// Initializes a new instance of the class. - /// - /// The timestamp scale in nanoseconds. - /// The duration of the entire file. - public Info(long timestampScale, double? duration) - { - TimestampScale = timestampScale; - Duration = duration; - } - - /// - /// Gets the timestamp scale in nanoseconds. - /// - public long TimestampScale { get; } - - /// - /// Gets the total duration of the file. - /// - public double? Duration { get; } + TimestampScale = timestampScale; + Duration = duration; } + + /// + /// Gets the timestamp scale in nanoseconds. + /// + public long TimestampScale { get; } + + /// + /// Gets the total duration of the file. + /// + public double? Duration { get; } } diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs index d9e346c03..95e4fd882 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs @@ -1,36 +1,35 @@ -namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models +namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models; + +/// +/// The matroska SeekHead segment. All positions are relative to the Segment container. +/// +internal class SeekHead { /// - /// The matroska SeekHead segment. All positions are relative to the Segment container. + /// Initializes a new instance of the class. /// - internal class SeekHead + /// The relative file position of the info segment. + /// The relative file position of the tracks segment. + /// The relative file position of the cues segment. + public SeekHead(long infoPosition, long tracksPosition, long cuesPosition) { - /// - /// Initializes a new instance of the class. - /// - /// The relative file position of the info segment. - /// The relative file position of the tracks segment. - /// The relative file position of the cues segment. - public SeekHead(long infoPosition, long tracksPosition, long cuesPosition) - { - InfoPosition = infoPosition; - TracksPosition = tracksPosition; - CuesPosition = cuesPosition; - } - - /// - /// Gets relative file position of the info segment. - /// - public long InfoPosition { get; } - - /// - /// Gets the relative file position of the tracks segment. - /// - public long TracksPosition { get; } - - /// - /// Gets the relative file position of the cues segment. - /// - public long CuesPosition { get; } + InfoPosition = infoPosition; + TracksPosition = tracksPosition; + CuesPosition = cuesPosition; } + + /// + /// Gets relative file position of the info segment. + /// + public long InfoPosition { get; } + + /// + /// Gets the relative file position of the tracks segment. + /// + public long TracksPosition { get; } + + /// + /// Gets the relative file position of the cues segment. + /// + public long CuesPosition { get; } } diff --git a/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs b/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs index 264fb6f4c..79648c4f6 100644 --- a/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs +++ b/tests/Jellyfin.MediaEncoding.Hls.Tests/Playlist/DynamicHlsPlaylistGeneratorTests.cs @@ -38,8 +38,8 @@ namespace Jellyfin.MediaEncoding.Hls.Tests.Playlist [Theory] [InlineData("testfile.mkv", new string[0], false)] - [InlineData("testfile.flv", new[] { "mp4", "mkv", "ts" }, false)] - [InlineData("testfile.flv", new[] { "mp4", "mkv", "ts", "flv" }, true)] + [InlineData("testfile.flv", new[] { ".mp4", ".mkv", ".ts" }, false)] + [InlineData("testfile.flv", new[] { ".mp4", ".mkv", ".ts", ".flv" }, true)] [InlineData("/some/arbitrarily/long/path/testfile.mkv", new[] { "mkv" }, true)] public void IsExtractionAllowedForFile_Valid_Success(string filePath, string[] allowedExtensions, bool isAllowed) { @@ -47,7 +47,7 @@ namespace Jellyfin.MediaEncoding.Hls.Tests.Playlist } [Theory] - [InlineData("testfile", new[] { "mp4" })] + [InlineData("testfile", new[] { ".mp4" })] public void IsExtractionAllowedForFile_Invalid_ReturnsFalse(string filePath, string[] allowedExtensions) { Assert.False(DynamicHlsPlaylistGenerator.IsExtractionAllowedForFile(filePath, allowedExtensions)); diff --git a/tests/Jellyfin.MediaEncoding.Keyframes.Tests/FfProbe/FfProbeKeyframeExtractorTests.cs b/tests/Jellyfin.MediaEncoding.Keyframes.Tests/FfProbe/FfProbeKeyframeExtractorTests.cs index e410fa29f..e5debf8c0 100644 --- a/tests/Jellyfin.MediaEncoding.Keyframes.Tests/FfProbe/FfProbeKeyframeExtractorTests.cs +++ b/tests/Jellyfin.MediaEncoding.Keyframes.Tests/FfProbe/FfProbeKeyframeExtractorTests.cs @@ -13,7 +13,7 @@ namespace Jellyfin.MediaEncoding.Keyframes.FfProbe { var testDataPath = Path.Combine("FfProbe/Test Data", testDataFileName); var resultPath = Path.Combine("FfProbe/Test Data", resultFileName); - var resultFileStream = File.OpenRead(resultPath); + using var resultFileStream = File.OpenRead(resultPath); var expectedResult = JsonSerializer.Deserialize(resultFileStream)!; using var fileStream = File.OpenRead(testDataPath);