From 9c15f96e12a0d48a70cbca8380bf78a4f2512b03 Mon Sep 17 00:00:00 2001 From: cvium Date: Thu, 23 Sep 2021 15:29:12 +0200 Subject: [PATCH] Add first draft of keyframe extraction for Matroska --- .../Controllers/DynamicHlsController.cs | 149 +++--------- Jellyfin.Api/Jellyfin.Api.csproj | 1 + Jellyfin.Server/Jellyfin.Server.csproj | 1 + Jellyfin.Server/Startup.cs | 3 + Jellyfin.sln | 14 ++ .../MediaEncoding/IMediaEncoder.cs | 6 + .../Encoder/MediaEncoder.cs | 3 + .../Configuration/EncodingOptions.cs | 5 + ...aEncodingHlsServiceCollectionExtensions.cs | 21 ++ .../Jellyfin.MediaEncoding.Hls.csproj | 22 ++ .../Playlist/CreateMainPlaylistRequest.cs | 57 +++++ .../Playlist/DynamicHlsPlaylistGenerator.cs | 227 ++++++++++++++++++ .../Playlist/IDynamicHlsPlaylistGenerator.cs | 15 ++ .../FfProbe/FfProbeKeyframeExtractor.cs | 10 + .../FfTool/FfToolKeyframeExtractor.cs | 10 + .../Jellyfin.MediaEncoding.Keyframes.csproj | 24 ++ .../KeyframeData.cs | 28 +++ .../KeyframeExtractor.cs | 56 +++++ .../Extensions/EbmlReaderExtensions.cs | 181 ++++++++++++++ .../Matroska/MatroskaConstants.cs | 31 +++ .../Matroska/MatroskaKeyframeExtractor.cs | 76 ++++++ .../Matroska/Models/Info.cs | 29 +++ .../Matroska/Models/SeekHead.cs | 36 +++ 23 files changed, 893 insertions(+), 112 deletions(-) create mode 100644 src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs create mode 100644 src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj create mode 100644 src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs create mode 100644 src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs create mode 100644 src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs create mode 100644 src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs diff --git a/Jellyfin.Api/Controllers/DynamicHlsController.cs b/Jellyfin.Api/Controllers/DynamicHlsController.cs index a54003357..a6d982552 100644 --- a/Jellyfin.Api/Controllers/DynamicHlsController.cs +++ b/Jellyfin.Api/Controllers/DynamicHlsController.cs @@ -13,6 +13,7 @@ using Jellyfin.Api.Constants; using Jellyfin.Api.Helpers; using Jellyfin.Api.Models.PlaybackDtos; using Jellyfin.Api.Models.StreamingDtos; +using Jellyfin.MediaEncoding.Hls.Playlist; using MediaBrowser.Common.Configuration; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Devices; @@ -28,7 +29,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Microsoft.Net.Http.Headers; namespace Jellyfin.Api.Controllers { @@ -54,6 +54,7 @@ namespace Jellyfin.Api.Controllers private readonly TranscodingJobHelper _transcodingJobHelper; private readonly ILogger _logger; private readonly EncodingHelper _encodingHelper; + private readonly IDynamicHlsPlaylistGenerator _dynamicHlsPlaylistGenerator; private readonly DynamicHlsHelper _dynamicHlsHelper; private readonly EncodingOptions _encodingOptions; @@ -73,6 +74,7 @@ namespace Jellyfin.Api.Controllers /// Instance of the interface. /// Instance of . /// Instance of . + /// Instance of . public DynamicHlsController( ILibraryManager libraryManager, IUserManager userManager, @@ -86,7 +88,8 @@ namespace Jellyfin.Api.Controllers TranscodingJobHelper transcodingJobHelper, ILogger logger, DynamicHlsHelper dynamicHlsHelper, - EncodingHelper encodingHelper) + EncodingHelper encodingHelper, + IDynamicHlsPlaylistGenerator dynamicHlsPlaylistGenerator) { _libraryManager = libraryManager; _userManager = userManager; @@ -101,6 +104,7 @@ namespace Jellyfin.Api.Controllers _logger = logger; _dynamicHlsHelper = dynamicHlsHelper; _encodingHelper = encodingHelper; + _dynamicHlsPlaylistGenerator = dynamicHlsPlaylistGenerator; _encodingOptions = serverConfigurationManager.GetEncodingOptions(); } @@ -772,13 +776,15 @@ namespace Jellyfin.Api.Controllers /// The playlist id. /// The segment id. /// The video container. Possible values are: ts, webm, asf, wmv, ogv, mp4, m4v, mkv, mpeg, mpg, avi, 3gp, wmv, wtv, m2ts, mov, iso, flv. + /// The position of the requested segment in ticks. + /// The length of the requested segment in ticks. /// Optional. If true, the original file will be streamed statically without any encoding. Use either no url extension or the original file extension. true/false. /// The streaming parameters. /// The tag. /// Optional. The dlna device profile id to utilize. /// The play session id. /// The segment container. - /// The segment lenght. + /// The desired segment length. /// The minimum number of segments. /// The media version id, if playing an alternate version. /// The device id of the client requesting. Used to stop encoding processes when needed. @@ -830,6 +836,8 @@ namespace Jellyfin.Api.Controllers [FromRoute, Required] string playlistId, [FromRoute, Required] int segmentId, [FromRoute, Required] string container, + [FromQuery, Required] long runtimeTicks, + [FromQuery, Required] long actualSegmentLengthTicks, [FromQuery] bool? @static, [FromQuery] string? @params, [FromQuery] string? tag, @@ -881,6 +889,8 @@ namespace Jellyfin.Api.Controllers var streamingRequest = new VideoRequestDto { Id = itemId, + CurrentRuntimeTicks = runtimeTicks, + ActualSegmentLengthTicks = actualSegmentLengthTicks, Container = container, Static = @static ?? false, Params = @params, @@ -942,6 +952,8 @@ namespace Jellyfin.Api.Controllers /// The playlist id. /// The segment id. /// The video container. Possible values are: ts, webm, asf, wmv, ogv, mp4, m4v, mkv, mpeg, mpg, avi, 3gp, wmv, wtv, m2ts, mov, iso, flv. + /// The position of the requested segment in ticks. + /// The length of the requested segment in ticks. /// Optional. If true, the original file will be streamed statically without any encoding. Use either no url extension or the original file extension. true/false. /// The streaming parameters. /// The tag. @@ -1001,6 +1013,8 @@ namespace Jellyfin.Api.Controllers [FromRoute, Required] string playlistId, [FromRoute, Required] int segmentId, [FromRoute, Required] string container, + [FromQuery, Required] long runtimeTicks, + [FromQuery, Required] long actualSegmentLengthTicks, [FromQuery] bool? @static, [FromQuery] string? @params, [FromQuery] string? tag, @@ -1054,6 +1068,8 @@ namespace Jellyfin.Api.Controllers { Id = itemId, Container = container, + CurrentRuntimeTicks = runtimeTicks, + ActualSegmentLengthTicks = actualSegmentLengthTicks, Static = @static ?? false, Params = @params, Tag = tag, @@ -1126,60 +1142,16 @@ namespace Jellyfin.Api.Controllers cancellationTokenSource.Token) .ConfigureAwait(false); - Response.Headers.Add(HeaderNames.Expires, "0"); + var request = new CreateMainPlaylistRequest( + state.MediaPath, + state.SegmentLength * 1000, + state.RunTimeTicks ?? 0, + state.Request.SegmentContainer ?? string.Empty, + "hls1/main/", + Request.QueryString.ToString()); + var playlist = _dynamicHlsPlaylistGenerator.CreateMainPlaylist(request); - var segmentLengths = GetSegmentLengths(state); - - var segmentContainer = state.Request.SegmentContainer ?? "ts"; - - // http://ffmpeg.org/ffmpeg-all.html#toc-hls-2 - var isHlsInFmp4 = string.Equals(segmentContainer, "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(segmentLengths.Length > 0 ? segmentLengths.Max() : state.SegmentLength)) - .AppendLine() - .AppendLine("#EXT-X-MEDIA-SEQUENCE:0"); - - var index = 0; - var segmentExtension = GetSegmentFileExtension(streamingRequest.SegmentContainer); - var queryString = Request.QueryString; - - if (isHlsInFmp4) - { - builder.Append("#EXT-X-MAP:URI=\"") - .Append("hls1/") - .Append(name) - .Append("/-1") - .Append(segmentExtension) - .Append(queryString) - .Append('"') - .AppendLine(); - } - - foreach (var length in segmentLengths) - { - builder.Append("#EXTINF:") - .Append(length.ToString("0.0000", CultureInfo.InvariantCulture)) - .AppendLine(", nodesc") - .Append("hls1/") - .Append(name) - .Append('/') - .Append(index++) - .Append(segmentExtension) - .Append(queryString) - .AppendLine(); - } - - builder.AppendLine("#EXT-X-ENDLIST"); - return new FileContentResult(Encoding.UTF8.GetBytes(builder.ToString()), MimeTypes.GetMimeType("playlist.m3u8")); + return new FileContentResult(Encoding.UTF8.GetBytes(playlist), MimeTypes.GetMimeType("playlist.m3u8")); } private async Task GetDynamicSegment(StreamingRequestDto streamingRequest, int segmentId) @@ -1280,7 +1252,7 @@ namespace Jellyfin.Api.Controllers DeleteLastFile(playlistPath, segmentExtension, 0); } - streamingRequest.StartTimeTicks = GetStartPositionTicks(state, segmentId); + streamingRequest.StartTimeTicks = streamingRequest.CurrentRuntimeTicks; state.WaitForPath = segmentPath; job = await _transcodingJobHelper.StartFfMpeg( @@ -1634,7 +1606,7 @@ namespace Jellyfin.Api.Controllers { // Transcoding job is over, so assume all existing files are ready _logger.LogDebug("serving up {0} as transcode is over", segmentPath); - return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob); + return GetSegmentResult(state, segmentPath, transcodingJob); } var currentTranscodingIndex = GetCurrentTranscodingIndex(playlistPath, segmentExtension); @@ -1643,7 +1615,7 @@ namespace Jellyfin.Api.Controllers if (segmentIndex < currentTranscodingIndex) { _logger.LogDebug("serving up {0} as transcode index {1} is past requested point {2}", segmentPath, currentTranscodingIndex, segmentIndex); - return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob); + return GetSegmentResult(state, segmentPath, transcodingJob); } } @@ -1658,8 +1630,8 @@ namespace Jellyfin.Api.Controllers { if (transcodingJob.HasExited || System.IO.File.Exists(nextSegmentPath)) { - _logger.LogDebug("serving up {0} as it deemed ready", segmentPath); - return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob); + _logger.LogDebug("Serving up {SegmentPath} as it deemed ready", segmentPath); + return GetSegmentResult(state, segmentPath, transcodingJob); } } else @@ -1690,16 +1662,16 @@ namespace Jellyfin.Api.Controllers _logger.LogWarning("cannot serve {0} as it doesn't exist and no transcode is running", segmentPath); } - return GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob); + return GetSegmentResult(state, segmentPath, transcodingJob); } - private ActionResult GetSegmentResult(StreamState state, string segmentPath, int index, TranscodingJobDto? transcodingJob) + private ActionResult GetSegmentResult(StreamState state, string segmentPath, TranscodingJobDto? transcodingJob) { - var segmentEndingPositionTicks = GetEndPositionTicks(state, index); + var segmentEndingPositionTicks = state.Request.CurrentRuntimeTicks + state.Request.ActualSegmentLengthTicks; Response.OnCompleted(() => { - _logger.LogDebug("finished serving {0}", segmentPath); + _logger.LogDebug("Finished serving {SegmentPath}", segmentPath); if (transcodingJob != null) { transcodingJob.DownloadPositionTicks = Math.Max(transcodingJob.DownloadPositionTicks ?? segmentEndingPositionTicks, segmentEndingPositionTicks); @@ -1712,29 +1684,6 @@ namespace Jellyfin.Api.Controllers return FileStreamResponseHelpers.GetStaticFileResult(segmentPath, MimeTypes.GetMimeType(segmentPath)!, false, HttpContext); } - private long GetEndPositionTicks(StreamState state, int requestedIndex) - { - double startSeconds = 0; - var lengths = GetSegmentLengths(state); - - if (requestedIndex >= lengths.Length) - { - var msg = string.Format( - CultureInfo.InvariantCulture, - "Invalid segment index requested: {0} - Segment count: {1}", - requestedIndex, - lengths.Length); - throw new ArgumentException(msg); - } - - for (var i = 0; i <= requestedIndex; i++) - { - startSeconds += lengths[i]; - } - - return TimeSpan.FromSeconds(startSeconds).Ticks; - } - private int? GetCurrentTranscodingIndex(string playlist, string segmentExtension) { var job = _transcodingJobHelper.GetTranscodingJob(playlist, TranscodingJobType); @@ -1813,29 +1762,5 @@ namespace Jellyfin.Api.Controllers _logger.LogError(ex, "Error deleting partial stream file(s) {path}", path); } } - - private long GetStartPositionTicks(StreamState state, int requestedIndex) - { - double startSeconds = 0; - var lengths = GetSegmentLengths(state); - - if (requestedIndex >= lengths.Length) - { - var msg = string.Format( - CultureInfo.InvariantCulture, - "Invalid segment index requested: {0} - Segment count: {1}", - requestedIndex, - lengths.Length); - throw new ArgumentException(msg); - } - - for (var i = 0; i < requestedIndex; i++) - { - startSeconds += lengths[i]; - } - - var position = TimeSpan.FromSeconds(startSeconds).Ticks; - return position; - } } } diff --git a/Jellyfin.Api/Jellyfin.Api.csproj b/Jellyfin.Api/Jellyfin.Api.csproj index 7f4eb0378..b2fcaca59 100644 --- a/Jellyfin.Api/Jellyfin.Api.csproj +++ b/Jellyfin.Api/Jellyfin.Api.csproj @@ -23,6 +23,7 @@ + diff --git a/Jellyfin.Server/Jellyfin.Server.csproj b/Jellyfin.Server/Jellyfin.Server.csproj index fc935cecb..a291399ba 100644 --- a/Jellyfin.Server/Jellyfin.Server.csproj +++ b/Jellyfin.Server/Jellyfin.Server.csproj @@ -52,6 +52,7 @@ + diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index 8085c2630..8eb5f2196 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -4,6 +4,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Net.Mime; using System.Text; +using Jellyfin.MediaEncoding.Hls.Extensions; using Jellyfin.Networking.Configuration; using Jellyfin.Server.Extensions; using Jellyfin.Server.Implementations; @@ -104,6 +105,8 @@ namespace Jellyfin.Server services.AddHealthChecks() .AddDbContextCheck(); + + services.AddHlsPlaylistGenerator(); } /// diff --git a/Jellyfin.sln b/Jellyfin.sln index 4626601c3..68570a214 100644 --- a/Jellyfin.sln +++ b/Jellyfin.sln @@ -89,6 +89,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Extensions", "src\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Extensions.Tests", "tests\Jellyfin.Extensions.Tests\Jellyfin.Extensions.Tests.csproj", "{332A5C7A-F907-47CA-910E-BE6F7371B9E0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.MediaEncoding.Keyframes", "src\Jellyfin.MediaEncoding.Keyframes\Jellyfin.MediaEncoding.Keyframes.csproj", "{06535CA1-4097-4360-85EB-5FB875D53239}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.MediaEncoding.Hls", "src\Jellyfin.MediaEncoding.Hls\Jellyfin.MediaEncoding.Hls.csproj", "{DA9FD356-4894-4830-B208-D6BCE3E65B11}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -243,6 +247,14 @@ Global {332A5C7A-F907-47CA-910E-BE6F7371B9E0}.Debug|Any CPU.Build.0 = Debug|Any CPU {332A5C7A-F907-47CA-910E-BE6F7371B9E0}.Release|Any CPU.ActiveCfg = Release|Any CPU {332A5C7A-F907-47CA-910E-BE6F7371B9E0}.Release|Any CPU.Build.0 = Release|Any CPU + {06535CA1-4097-4360-85EB-5FB875D53239}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {06535CA1-4097-4360-85EB-5FB875D53239}.Debug|Any CPU.Build.0 = Debug|Any CPU + {06535CA1-4097-4360-85EB-5FB875D53239}.Release|Any CPU.ActiveCfg = Release|Any CPU + {06535CA1-4097-4360-85EB-5FB875D53239}.Release|Any CPU.Build.0 = Release|Any CPU + {DA9FD356-4894-4830-B208-D6BCE3E65B11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA9FD356-4894-4830-B208-D6BCE3E65B11}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA9FD356-4894-4830-B208-D6BCE3E65B11}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA9FD356-4894-4830-B208-D6BCE3E65B11}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -263,6 +275,8 @@ Global {A964008C-2136-4716-B6CB-B3426C22320A} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6} {750B8757-BE3D-4F8C-941A-FBAD94904ADA} = {C9F0AB5D-F4D7-40C8-A353-3305C86D6D4C} {332A5C7A-F907-47CA-910E-BE6F7371B9E0} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6} + {06535CA1-4097-4360-85EB-5FB875D53239} = {C9F0AB5D-F4D7-40C8-A353-3305C86D6D4C} + {DA9FD356-4894-4830-B208-D6BCE3E65B11} = {C9F0AB5D-F4D7-40C8-A353-3305C86D6D4C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3448830C-EBDC-426C-85CD-7BBB9651A7FE} diff --git a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs index c5522bc3c..1f4e08222 100644 --- a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs +++ b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs @@ -24,6 +24,12 @@ namespace MediaBrowser.Controller.MediaEncoding /// The encoder path. string EncoderPath { get; } + /// + /// Gets the probe path. + /// + /// The probe path. + string ProbePath { get; } + /// /// Whether given encoder codec is supported. /// diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 4cbd1bbc8..eed50fe0b 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -91,6 +91,9 @@ namespace MediaBrowser.MediaEncoding.Encoder /// public string EncoderPath => _ffmpegPath; + /// + public string ProbePath => _ffprobePath; + /// /// Run at startup or if the user removes a Custom path from transcode page. /// Sets global variables FFmpegPath. diff --git a/MediaBrowser.Model/Configuration/EncodingOptions.cs b/MediaBrowser.Model/Configuration/EncodingOptions.cs index 365bbeef6..b546e329d 100644 --- a/MediaBrowser.Model/Configuration/EncodingOptions.cs +++ b/MediaBrowser.Model/Configuration/EncodingOptions.cs @@ -1,3 +1,5 @@ +using System; + #nullable disable #pragma warning disable CS1591 @@ -37,6 +39,7 @@ namespace MediaBrowser.Model.Configuration EnableHardwareEncoding = true; AllowHevcEncoding = false; EnableSubtitleExtraction = true; + AllowAutomaticKeyframeExtractionForExtensions = Array.Empty(); HardwareDecodingCodecs = new string[] { "h264", "vc1" }; } @@ -111,5 +114,7 @@ namespace MediaBrowser.Model.Configuration public bool EnableSubtitleExtraction { get; set; } public string[] HardwareDecodingCodecs { get; set; } + + public string[] AllowAutomaticKeyframeExtractionForExtensions { get; set; } } } diff --git a/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs b/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs new file mode 100644 index 000000000..327898366 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs @@ -0,0 +1,21 @@ +using Jellyfin.MediaEncoding.Hls.Playlist; +using Microsoft.Extensions.DependencyInjection; + +namespace Jellyfin.MediaEncoding.Hls.Extensions +{ + /// + /// Extensions for the interface. + /// + public static class MediaEncodingHlsServiceCollectionExtensions + { + /// + /// Adds the hls playlist generators to the . + /// + /// An instance of the interface. + /// The updated service collection. + public static IServiceCollection AddHlsPlaylistGenerator(this IServiceCollection serviceCollection) + { + return serviceCollection.AddSingleton(); + } + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj b/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj new file mode 100644 index 000000000..89cd1378b --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj @@ -0,0 +1,22 @@ + + + + net5.0 + + + + + + + + + + + + + + ..\..\..\..\..\..\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + + diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs new file mode 100644 index 000000000..d6db1ca6e --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs @@ -0,0 +1,57 @@ +namespace Jellyfin.MediaEncoding.Hls.Playlist +{ + /// + /// Request class for the method. + /// + public class CreateMainPlaylistRequest + { + /// + /// 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; } + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs new file mode 100644 index 000000000..8c16545df --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs @@ -0,0 +1,227 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.Json; +using Jellyfin.Extensions.Json; +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 +{ + /// + public class DynamicHlsPlaylistGenerator : IDynamicHlsPlaylistGenerator + { + private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; + private readonly IServerConfigurationManager _serverConfigurationManager; + private readonly IMediaEncoder _mediaEncoder; + private readonly IApplicationPaths _applicationPaths; + private readonly KeyframeExtractor _keyframeExtractor; + private const string DefaultContainerExtension = ".ts"; + + /// + /// 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) + { + _serverConfigurationManager = serverConfigurationManager; + _mediaEncoder = mediaEncoder; + _applicationPaths = applicationPaths; + _keyframeExtractor = new KeyframeExtractor(loggerFactory.CreateLogger()); + } + + private string KeyframeCachePath => Path.Combine(_applicationPaths.DataPath, "keyframes"); + + /// + public string CreateMainPlaylist(CreateMainPlaylistRequest request) + { + IReadOnlyList segments; + if (IsExtractionAllowed(request.FilePath)) + { + segments = ComputeSegments(request.FilePath, 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(); + } + + double currentRuntimeInSeconds = 0; + foreach (var length in segments) + { + builder.Append("#EXTINF:") + .Append(length.ToString("0.0000", CultureInfo.InvariantCulture)) + .AppendLine(", nodesc") + .Append(request.EndpointPrefix) + .Append(index++) + .Append(segmentExtension) + .Append(request.QueryString) + .Append("&runtimeTicks=") + .Append(TimeSpan.FromSeconds(currentRuntimeInSeconds).Ticks) + .Append("&actualSegmentLengthTicks=") + .Append(TimeSpan.FromSeconds(length).Ticks) + .AppendLine(); + + currentRuntimeInSeconds += length; + } + + builder.AppendLine("#EXT-X-ENDLIST"); + + return builder.ToString(); + } + + private IReadOnlyList ComputeSegments(string filePath, int desiredSegmentLengthMs) + { + KeyframeData keyframeData; + var cachePath = GetCachePath(filePath); + if (TryReadFromCache(cachePath, out var cachedResult)) + { + keyframeData = cachedResult; + } + else + { + keyframeData = _keyframeExtractor.GetKeyframeData(filePath, _mediaEncoder.ProbePath, string.Empty); + CacheResult(cachePath, keyframeData); + } + + 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; + } + + private void CacheResult(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 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; + } + + private bool IsExtractionAllowed(ReadOnlySpan filePath) + { + // Remove the leading dot + var extension = Path.GetExtension(filePath)[1..]; + var allowedExtensions = _serverConfigurationManager.GetEncodingOptions().AllowAutomaticKeyframeExtractionForExtensions; + for (var i = 0; i < allowedExtensions.Length; i++) + { + var allowedExtension = allowedExtensions[i]; + if (extension.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + + return false; + } + + private static double[] ComputeEqualLengthSegments(long desiredSegmentLengthMs, long totalRuntimeTicks) + { + var segmentLengthTicks = TimeSpan.FromMilliseconds(desiredSegmentLengthMs).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] = desiredSegmentLengthMs; + } + + if (remainingTicks != 0) + { + segments[^1] = TimeSpan.FromTicks(remainingTicks).TotalSeconds; + } + + return segments; + } + + // TODO copied from DynamicHlsController + private static string GetSegmentFileExtension(string segmentContainer) + { + if (!string.IsNullOrWhiteSpace(segmentContainer)) + { + return "." + segmentContainer; + } + + return DefaultContainerExtension; + } + } +} diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs new file mode 100644 index 000000000..534f15a90 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs @@ -0,0 +1,15 @@ +namespace Jellyfin.MediaEncoding.Hls.Playlist +{ + /// + /// Generator for dynamic HLS playlists where the segment lengths aren't known in advance. + /// + public interface IDynamicHlsPlaylistGenerator + { + /// + /// Creates the main playlist containing the main video or audio stream. + /// + /// An instance of the class. + /// + string CreateMainPlaylist(CreateMainPlaylistRequest request); + } +} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs new file mode 100644 index 000000000..249608ef9 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs @@ -0,0 +1,10 @@ +using System; + +namespace Jellyfin.MediaEncoding.Keyframes.FfProbe +{ + public static class FfProbeKeyframeExtractor + { + // TODO + public static KeyframeData GetKeyframeData(string ffProbePath, string filePath) => throw new NotImplementedException(); + } +} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs new file mode 100644 index 000000000..89c149ff4 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs @@ -0,0 +1,10 @@ +using System; + +namespace Jellyfin.MediaEncoding.Keyframes.FfTool +{ + public static class FfToolKeyframeExtractor + { + // TODO + public static KeyframeData GetKeyframeData(string ffProbePath, string filePath) => throw new NotImplementedException(); + } +} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj b/src/Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj new file mode 100644 index 000000000..7a984658b --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj @@ -0,0 +1,24 @@ + + + + net5.0 + Jellyfin.MediaEncoding.Keyframes + + + + + + + + + + + + + + + ..\..\..\..\..\..\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.Extensions.Logging.Abstractions.dll + + + + diff --git a/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs new file mode 100644 index 000000000..3122f827c --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; + +namespace Jellyfin.MediaEncoding.Keyframes +{ + public class KeyframeData + { + /// + /// 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; } + } +} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs new file mode 100644 index 000000000..2ee6b43e6 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs @@ -0,0 +1,56 @@ +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. + /// + public KeyframeData GetKeyframeData(string filePath, string ffProbePath, string ffToolPath) + { + var extension = Path.GetExtension(filePath); + if (string.Equals(extension, ".mkv", StringComparison.OrdinalIgnoreCase)) + { + try + { + return MatroskaKeyframeExtractor.GetKeyframeData(filePath); + } + catch (InvalidOperationException ex) + { + _logger.LogError(ex, "{MatroskaKeyframeExtractor} failed to extract keyframes", nameof(MatroskaKeyframeExtractor)); + } + } + + if (!string.IsNullOrEmpty(ffToolPath)) + { + return FfToolKeyframeExtractor.GetKeyframeData(ffToolPath, filePath); + } + + return FfProbeKeyframeExtractor.GetKeyframeData(ffProbePath, filePath); + } + } +} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs new file mode 100644 index 000000000..0de0f996c --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs @@ -0,0 +1,181 @@ +using System; +using Jellyfin.MediaEncoding.Keyframes.Matroska.Models; +using NEbml.Core; + +namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions +{ + /// + /// Extension methods for the class. + /// + internal static class EbmlReaderExtensions + { + /// + /// 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()) + { + if (reader.ElementId.EncodedValue == identifier) + { + 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) + { + var buffer = new byte[4]; + reader.ReadBinary(buffer, 0, 4); + if (BitConverter.IsLittleEndian) + { + Array.Reverse(buffer); + } + + return BitConverter.ToUInt32(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) + { + throw new InvalidOperationException("File position must be at 0"); + } + + // Skip the header + if (!reader.FindElement(MatroskaConstants.SegmentContainer)) + { + throw new InvalidOperationException("Expected a segment container"); + } + + 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)) + { + 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) + { + break; + } + } + + reader.LeaveContainer(); + + 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 . + /// 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.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; + } + } + + 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 new file mode 100644 index 000000000..d18418d45 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs @@ -0,0 +1,31 @@ +namespace Jellyfin.MediaEncoding.Keyframes.Matroska +{ + /// + /// Constants for the Matroska identifiers. + /// + public static class MatroskaConstants + { + internal const ulong SegmentContainer = 0x18538067; + + 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 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 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 new file mode 100644 index 000000000..10d017d2a --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions; +using NEbml.Core; + +namespace Jellyfin.MediaEncoding.Keyframes.Matroska +{ + /// + /// The keyframe extractor for the matroska container. + /// + public static class MatroskaKeyframeExtractor + { + /// + /// 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)) + { + 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(ScaleToNanoseconds(cueTime, info.TimestampScale)); + } + + reader.LeaveContainer(); + } + + reader.LeaveContainer(); + + var result = new KeyframeData(ScaleToNanoseconds(info.Duration ?? 0, info.TimestampScale), keyframes); + return result; + } + + private static long ScaleToNanoseconds(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 ScaleToNanoseconds(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 new file mode 100644 index 000000000..02c6741ec --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs @@ -0,0 +1,29 @@ +namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models +{ + /// + /// The matroska Info segment. + /// + internal class Info + { + /// + /// 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; } + } +} diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs new file mode 100644 index 000000000..d9e346c03 --- /dev/null +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs @@ -0,0 +1,36 @@ +namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models +{ + /// + /// The matroska SeekHead segment. All positions are relative to the Segment container. + /// + internal class SeekHead + { + /// + /// 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; } + } +}