aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Jellyfin.Extensions/Jellyfin.Extensions.csproj4
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs96
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs36
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Extractors/FfProbeKeyframeExtractor.cs58
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Extractors/IKeyframeExtractor.cs24
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Extractors/MatroskaKeyframeExtractor.cs48
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj35
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs56
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs204
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs14
-rw-r--r--src/Jellyfin.MediaEncoding.Hls/ScheduledTasks/KeyframeExtractionScheduledTask.cs111
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs94
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs17
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj33
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs30
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs177
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs30
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs87
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs28
-rw-r--r--src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs35
20 files changed, 1217 insertions, 0 deletions
diff --git a/src/Jellyfin.Extensions/Jellyfin.Extensions.csproj b/src/Jellyfin.Extensions/Jellyfin.Extensions.csproj
index 90d2a0da6..37baff5ae 100644
--- a/src/Jellyfin.Extensions/Jellyfin.Extensions.csproj
+++ b/src/Jellyfin.Extensions/Jellyfin.Extensions.csproj
@@ -29,6 +29,10 @@
<!-- Code Analyzers-->
<ItemGroup>
+ <PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.3">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
+ </PackageReference>
<PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.376" PrivateAssets="All" />
<PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
diff --git a/src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs b/src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs
new file mode 100644
index 000000000..09816c960
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs
@@ -0,0 +1,96 @@
+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;
+using Microsoft.Extensions.Logging;
+
+namespace Jellyfin.MediaEncoding.Hls.Cache;
+
+/// <inheritdoc />
+public class CacheDecorator : IKeyframeExtractor
+{
+ private readonly IKeyframeExtractor _keyframeExtractor;
+ private readonly ILogger<CacheDecorator> _logger;
+ private readonly string _keyframeExtractorName;
+ private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
+ private readonly string _keyframeCachePath;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="CacheDecorator"/> class.
+ /// </summary>
+ /// <param name="applicationPaths">An instance of the <see cref="IApplicationPaths"/> interface.</param>
+ /// <param name="keyframeExtractor">An instance of the <see cref="IKeyframeExtractor"/> interface.</param>
+ /// <param name="logger">An instance of the <see cref="ILogger{CacheDecorator}"/> interface.</param>
+ public CacheDecorator(IApplicationPaths applicationPaths, IKeyframeExtractor keyframeExtractor, ILogger<CacheDecorator> logger)
+ {
+ ArgumentNullException.ThrowIfNull(applicationPaths);
+ ArgumentNullException.ThrowIfNull(keyframeExtractor);
+
+ _keyframeExtractor = keyframeExtractor;
+ _logger = logger;
+ _keyframeExtractorName = keyframeExtractor.GetType().Name;
+ // TODO make the dir configurable
+ _keyframeCachePath = Path.Combine(applicationPaths.DataPath, "keyframes");
+ }
+
+ /// <inheritdoc />
+ public bool IsMetadataBased => _keyframeExtractor.IsMetadataBased;
+
+ /// <inheritdoc />
+ 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))
+ {
+ _logger.LogDebug("Failed to extract keyframes using {ExtractorName}", _keyframeExtractorName);
+ return false;
+ }
+
+ _logger.LogDebug("Successfully extracted keyframes using {ExtractorName}", _keyframeExtractorName);
+ 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<char> 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<KeyframeData>(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
new file mode 100644
index 000000000..8ed4edcea
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Hls/Extensions/MediaEncodingHlsServiceCollectionExtensions.cs
@@ -0,0 +1,36 @@
+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;
+
+/// <summary>
+/// Extensions for the <see cref="IServiceCollection"/> interface.
+/// </summary>
+public static class MediaEncodingHlsServiceCollectionExtensions
+{
+ /// <summary>
+ /// Adds the hls playlist generators to the <see cref="IServiceCollection"/>.
+ /// </summary>
+ /// <param name="serviceCollection">An instance of the <see cref="IServiceCollection"/> interface.</param>
+ /// <returns>The updated service collection.</returns>
+ public static IServiceCollection AddHlsPlaylistGenerator(this IServiceCollection serviceCollection)
+ {
+ serviceCollection.AddSingletonWithDecorator(typeof(FfProbeKeyframeExtractor));
+ serviceCollection.AddSingletonWithDecorator(typeof(MatroskaKeyframeExtractor));
+ serviceCollection.AddSingleton<IDynamicHlsPlaylistGenerator, DynamicHlsPlaylistGenerator>();
+ return serviceCollection;
+ }
+
+ private static void AddSingletonWithDecorator(this IServiceCollection serviceCollection, Type type)
+ {
+ serviceCollection.AddSingleton<IKeyframeExtractor>(serviceProvider =>
+ {
+ var extractor = ActivatorUtilities.CreateInstance(serviceProvider, type);
+ var decorator = ActivatorUtilities.CreateInstance<CacheDecorator>(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;
+
+/// <inheritdoc />
+public class FfProbeKeyframeExtractor : IKeyframeExtractor
+{
+ private readonly IMediaEncoder _mediaEncoder;
+ private readonly NamingOptions _namingOptions;
+ private readonly ILogger<FfProbeKeyframeExtractor> _logger;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="FfProbeKeyframeExtractor"/> class.
+ /// </summary>
+ /// <param name="mediaEncoder">An instance of the <see cref="IMediaEncoder"/> interface.</param>
+ /// <param name="namingOptions">An instance of <see cref="NamingOptions"/>.</param>
+ /// <param name="logger">An instance of the <see cref="ILogger{FfprobeKeyframeExtractor}"/> interface.</param>
+ public FfProbeKeyframeExtractor(IMediaEncoder mediaEncoder, NamingOptions namingOptions, ILogger<FfProbeKeyframeExtractor> logger)
+ {
+ _mediaEncoder = mediaEncoder;
+ _namingOptions = namingOptions;
+ _logger = logger;
+ }
+
+ /// <inheritdoc />
+ public bool IsMetadataBased => false;
+
+ /// <inheritdoc />
+ 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;
+
+/// <summary>
+/// Keyframe extractor.
+/// </summary>
+public interface IKeyframeExtractor
+{
+ /// <summary>
+ /// Gets a value indicating whether the extractor is based on container metadata.
+ /// </summary>
+ bool IsMetadataBased { get; }
+
+ /// <summary>
+ /// Attempt to extract keyframes.
+ /// </summary>
+ /// <param name="filePath">The path to the file.</param>
+ /// <param name="keyframeData">The keyframes.</param>
+ /// <returns>A value indicating whether the keyframe extraction was successful.</returns>
+ 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..4bc537c0e
--- /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;
+
+/// <inheritdoc />
+public class MatroskaKeyframeExtractor : IKeyframeExtractor
+{
+ private readonly ILogger<MatroskaKeyframeExtractor> _logger;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MatroskaKeyframeExtractor"/> class.
+ /// </summary>
+ /// <param name="logger">An instance of the <see cref="ILogger{MatroskaKeyframeExtractor}"/> interface.</param>
+ public MatroskaKeyframeExtractor(ILogger<MatroskaKeyframeExtractor> logger)
+ {
+ _logger = logger;
+ }
+
+ /// <inheritdoc />
+ public bool IsMetadataBased => true;
+
+ /// <inheritdoc />
+ 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
new file mode 100644
index 000000000..56f973a21
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Hls/Jellyfin.MediaEncoding.Hls.csproj
@@ -0,0 +1,35 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net6.0</TargetFramework>
+ <GenerateDocumentationFile>true</GenerateDocumentationFile>
+ </PropertyGroup>
+
+ <!-- Code Analyzers-->
+ <ItemGroup>
+ <PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.3">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
+ </PackageReference>
+ <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
+ <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.376" PrivateAssets="All" />
+ <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="../../MediaBrowser.Common/MediaBrowser.Common.csproj" />
+ <ProjectReference Include="../../MediaBrowser.Controller/MediaBrowser.Controller.csproj" />
+ <ProjectReference Include="../Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
+ <_Parameter1>Jellyfin.MediaEncoding.Hls.Tests</_Parameter1>
+ </AssemblyAttribute>
+ </ItemGroup>
+
+</Project>
diff --git a/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs
new file mode 100644
index 000000000..ac28ca26a
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/CreateMainPlaylistRequest.cs
@@ -0,0 +1,56 @@
+namespace Jellyfin.MediaEncoding.Hls.Playlist;
+
+/// <summary>
+/// Request class for the <see cref="IDynamicHlsPlaylistGenerator.CreateMainPlaylist(CreateMainPlaylistRequest)"/> method.
+/// </summary>
+public class CreateMainPlaylistRequest
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="CreateMainPlaylistRequest"/> class.
+ /// </summary>
+ /// <param name="filePath">The absolute file path to the file.</param>
+ /// <param name="desiredSegmentLengthMs">The desired segment length in milliseconds.</param>
+ /// <param name="totalRuntimeTicks">The total duration of the file in ticks.</param>
+ /// <param name="segmentContainer">The desired segment container eg. "ts".</param>
+ /// <param name="endpointPrefix">The URI prefix for the relative URL in the playlist.</param>
+ /// <param name="queryString">The desired query string to append (must start with ?).</param>
+ 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;
+ }
+
+ /// <summary>
+ /// Gets the file path.
+ /// </summary>
+ public string FilePath { get; }
+
+ /// <summary>
+ /// Gets the desired segment length in milliseconds.
+ /// </summary>
+ public int DesiredSegmentLengthMs { get; }
+
+ /// <summary>
+ /// Gets the total runtime in ticks.
+ /// </summary>
+ public long TotalRuntimeTicks { get; }
+
+ /// <summary>
+ /// Gets the segment container.
+ /// </summary>
+ public string SegmentContainer { get; }
+
+ /// <summary>
+ /// Gets the endpoint prefix for the URL.
+ /// </summary>
+ public string EndpointPrefix { get; }
+
+ /// <summary>
+ /// Gets the query string.
+ /// </summary>
+ 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..5cdacaf31
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/DynamicHlsPlaylistGenerator.cs
@@ -0,0 +1,204 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Globalization;
+using System.IO;
+using System.Linq;
+using System.Text;
+using Jellyfin.MediaEncoding.Hls.Extractors;
+using Jellyfin.MediaEncoding.Keyframes;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.MediaEncoding;
+
+namespace Jellyfin.MediaEncoding.Hls.Playlist;
+
+/// <inheritdoc />
+public class DynamicHlsPlaylistGenerator : IDynamicHlsPlaylistGenerator
+{
+ private readonly IServerConfigurationManager _serverConfigurationManager;
+ private readonly IKeyframeExtractor[] _extractors;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="DynamicHlsPlaylistGenerator"/> class.
+ /// </summary>
+ /// <param name="serverConfigurationManager">An instance of the see <see cref="IServerConfigurationManager"/> interface.</param>
+ /// <param name="extractors">An instance of <see cref="IEnumerable{IKeyframeExtractor}"/>.</param>
+ public DynamicHlsPlaylistGenerator(IServerConfigurationManager serverConfigurationManager, IEnumerable<IKeyframeExtractor> extractors)
+ {
+ _serverConfigurationManager = serverConfigurationManager;
+ _extractors = extractors.Where(e => e.IsMetadataBased).ToArray();
+ }
+
+ /// <inheritdoc />
+ public string CreateMainPlaylist(CreateMainPlaylistRequest request)
+ {
+ IReadOnlyList<double> segments;
+ if (TryExtractKeyframes(request.FilePath, out var keyframeData))
+ {
+ segments = ComputeSegments(keyframeData, request.DesiredSegmentLengthMs);
+ }
+ else
+ {
+ segments = ComputeEqualLengthSegments(request.DesiredSegmentLengthMs, request.TotalRuntimeTicks);
+ }
+
+ var segmentExtension = EncodingHelper.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();
+ }
+
+ private bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData)
+ {
+ keyframeData = null;
+ if (!IsExtractionAllowedForFile(filePath, _serverConfigurationManager.GetEncodingOptions().AllowOnDemandMetadataBasedKeyframeExtractionForExtensions))
+ {
+ return false;
+ }
+
+ var len = _extractors.Length;
+ for (var i = 0; i < len; i++)
+ {
+ var extractor = _extractors[i];
+ if (!extractor.TryExtractKeyframes(filePath, out var result))
+ {
+ continue;
+ }
+
+ keyframeData = result;
+ return true;
+ }
+
+ return false;
+ }
+
+ internal static bool IsExtractionAllowedForFile(ReadOnlySpan<char> filePath, string[] allowedExtensions)
+ {
+ var extension = Path.GetExtension(filePath);
+ if (extension.IsEmpty)
+ {
+ return false;
+ }
+
+ // Remove the leading dot
+ var extensionWithoutDot = extension[1..];
+ for (var i = 0; i < allowedExtensions.Length; i++)
+ {
+ var allowedExtension = allowedExtensions[i].AsSpan().TrimStart('.');
+ if (extensionWithoutDot.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ internal static IReadOnlyList<double> ComputeSegments(KeyframeData keyframeData, int desiredSegmentLengthMs)
+ {
+ if (keyframeData.KeyframeTicks.Count > 0 && keyframeData.TotalDuration < keyframeData.KeyframeTicks[^1])
+ {
+ throw new ArgumentException("Invalid duration in keyframe data", nameof(keyframeData));
+ }
+
+ long lastKeyframe = 0;
+ var result = new List<double>();
+ // 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)
+ {
+ 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
new file mode 100644
index 000000000..2626cb2dd
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Hls/Playlist/IDynamicHlsPlaylistGenerator.cs
@@ -0,0 +1,14 @@
+namespace Jellyfin.MediaEncoding.Hls.Playlist;
+
+/// <summary>
+/// Generator for dynamic HLS playlists where the segment lengths aren't known in advance.
+/// </summary>
+public interface IDynamicHlsPlaylistGenerator
+{
+ /// <summary>
+ /// Creates the main playlist containing the main video or audio stream.
+ /// </summary>
+ /// <param name="request">An instance of the <see cref="CreateMainPlaylistRequest"/> class.</param>
+ /// <returns>The playlist as a formatted string.</returns>
+ 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..03acc6911
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Hls/ScheduledTasks/KeyframeExtractionScheduledTask.cs
@@ -0,0 +1,111 @@
+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;
+
+/// <inheritdoc />
+public class KeyframeExtractionScheduledTask : IScheduledTask
+{
+ private const int Pagesize = 1000;
+
+ private readonly ILocalizationManager _localizationManager;
+ private readonly ILibraryManager _libraryManager;
+ private readonly IKeyframeExtractor[] _keyframeExtractors;
+ private static readonly BaseItemKind[] _itemTypes = { BaseItemKind.Episode, BaseItemKind.Movie };
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="KeyframeExtractionScheduledTask"/> class.
+ /// </summary>
+ /// <param name="localizationManager">An instance of the <see cref="ILocalizationManager"/> interface.</param>
+ /// <param name="libraryManager">An instance of the <see cref="ILibraryManager"/> interface.</param>
+ /// <param name="keyframeExtractors">The keyframe extractors.</param>
+ public KeyframeExtractionScheduledTask(ILocalizationManager localizationManager, ILibraryManager libraryManager, IEnumerable<IKeyframeExtractor> keyframeExtractors)
+ {
+ _localizationManager = localizationManager;
+ _libraryManager = libraryManager;
+ _keyframeExtractors = keyframeExtractors.OrderByDescending(e => e.IsMetadataBased).ToArray();
+ }
+
+ /// <inheritdoc />
+ public string Name => "Keyframe Extractor";
+
+ /// <inheritdoc />
+ public string Key => "KeyframeExtraction";
+
+ /// <inheritdoc />
+ public string Description => "Extracts keyframes from video files to create more precise HLS playlists. This task may run for a long time.";
+
+ /// <inheritdoc />
+ public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory");
+
+ /// <inheritdoc />
+ public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
+ {
+ var query = new InternalItemsQuery
+ {
+ MediaTypes = new[] { MediaType.Video },
+ IsVirtualItem = false,
+ IncludeItemTypes = _itemTypes,
+ DtoOptions = new DtoOptions(true),
+ SourceTypes = new[] { SourceType.Library },
+ Recursive = true,
+ Limit = Pagesize
+ };
+
+ var numberOfVideos = _libraryManager.GetCount(query);
+
+ var startIndex = 0;
+ var numComplete = 0;
+
+ while (startIndex < numberOfVideos)
+ {
+ query.StartIndex = startIndex;
+
+ var videos = _libraryManager.GetItemList(query);
+ var currentPageCount = videos.Count;
+ // TODO parallelize with Parallel.ForEach?
+ for (var i = 0; i < currentPageCount; i++)
+ {
+ var video = videos[i];
+ // Only local files supported
+ if (video.IsFileProtocol && File.Exists(video.Path))
+ {
+ 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;
+ }
+ }
+ }
+
+ // Update progress
+ numComplete++;
+ double percent = (double)numComplete / numberOfVideos;
+ progress.Report(100 * percent);
+ }
+
+ startIndex += Pagesize;
+ }
+
+ progress.Report(100);
+ return Task.CompletedTask;
+ }
+
+ /// <inheritdoc />
+ public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => Enumerable.Empty<TaskTriggerInfo>();
+}
diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs
new file mode 100644
index 000000000..320604e10
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs
@@ -0,0 +1,94 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Globalization;
+using System.IO;
+
+namespace Jellyfin.MediaEncoding.Keyframes.FfProbe;
+
+/// <summary>
+/// FfProbe based keyframe extractor.
+/// </summary>
+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}\"";
+
+ /// <summary>
+ /// Extracts the keyframes using the ffprobe executable at the specified path.
+ /// </summary>
+ /// <param name="ffProbePath">The path to the ffprobe executable.</param>
+ /// <param name="filePath">The file path.</param>
+ /// <returns>An instance of <see cref="KeyframeData"/>.</returns>
+ public static KeyframeData GetKeyframeData(string ffProbePath, string filePath)
+ {
+ using var process = new Process
+ {
+ StartInfo = new ProcessStartInfo
+ {
+ FileName = ffProbePath,
+ Arguments = string.Format(CultureInfo.InvariantCulture, DefaultArguments, filePath),
+
+ CreateNoWindow = true,
+ UseShellExecute = false,
+ RedirectStandardOutput = true,
+
+ WindowStyle = ProcessWindowStyle.Hidden,
+ ErrorDialog = false,
+ },
+ EnableRaisingEvents = true
+ };
+
+ process.Start();
+
+ return ParseStream(process.StandardOutput);
+ }
+
+ internal static KeyframeData ParseStream(StreamReader reader)
+ {
+ var keyframes = new List<long>();
+ double streamDuration = 0;
+ double formatDuration = 0;
+
+ while (!reader.EndOfStream)
+ {
+ 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;
+ }
+ }
+ }
+
+ // 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
new file mode 100644
index 000000000..aaaca6fe1
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/FfTool/FfToolKeyframeExtractor.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace Jellyfin.MediaEncoding.Keyframes.FfTool;
+
+/// <summary>
+/// FfTool based keyframe extractor.
+/// </summary>
+public static class FfToolKeyframeExtractor
+{
+ /// <summary>
+ /// Extracts the keyframes using the fftool executable at the specified path.
+ /// </summary>
+ /// <param name="ffToolPath">The path to the fftool executable.</param>
+ /// <param name="filePath">The file path.</param>
+ /// <returns>An instance of <see cref="KeyframeData"/>.</returns>
+ public static KeyframeData GetKeyframeData(string ffToolPath, 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..5ec09c768
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj
@@ -0,0 +1,33 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net6.0</TargetFramework>
+ <GenerateDocumentationFile>true</GenerateDocumentationFile>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="NEbml" Version="0.11.0" />
+ </ItemGroup>
+
+ <!-- Code Analyzers-->
+ <ItemGroup>
+ <PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.3">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
+ </PackageReference>
+ <PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
+ <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.376" PrivateAssets="All" />
+ <PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
+ <_Parameter1>Jellyfin.MediaEncoding.Keyframes.Tests</_Parameter1>
+ </AssemblyAttribute>
+ </ItemGroup>
+
+</Project>
diff --git a/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs
new file mode 100644
index 000000000..06f9180e7
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/KeyframeData.cs
@@ -0,0 +1,30 @@
+using System.Collections.Generic;
+
+namespace Jellyfin.MediaEncoding.Keyframes;
+
+/// <summary>
+/// Keyframe information for a specific file.
+/// </summary>
+public class KeyframeData
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="KeyframeData"/> class.
+ /// </summary>
+ /// <param name="totalDuration">The total duration of the video stream in ticks.</param>
+ /// <param name="keyframeTicks">The video keyframes in ticks.</param>
+ public KeyframeData(long totalDuration, IReadOnlyList<long> keyframeTicks)
+ {
+ TotalDuration = totalDuration;
+ KeyframeTicks = keyframeTicks;
+ }
+
+ /// <summary>
+ /// Gets the total duration of the stream in ticks.
+ /// </summary>
+ public long TotalDuration { get; }
+
+ /// <summary>
+ /// Gets the keyframes in ticks.
+ /// </summary>
+ public IReadOnlyList<long> KeyframeTicks { get; }
+}
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..fd170864b
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Extensions/EbmlReaderExtensions.cs
@@ -0,0 +1,177 @@
+using System;
+using System.Buffers.Binary;
+using Jellyfin.MediaEncoding.Keyframes.Matroska.Models;
+using NEbml.Core;
+
+namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions;
+
+/// <summary>
+/// Extension methods for the <see cref="EbmlReader"/> class.
+/// </summary>
+internal static class EbmlReaderExtensions
+{
+ /// <summary>
+ /// Traverses the current container to find the element with <paramref name="identifier"/> identifier.
+ /// </summary>
+ /// <param name="reader">An instance of <see cref="EbmlReader"/>.</param>
+ /// <param name="identifier">The element identifier.</param>
+ /// <returns>A value indicating whether the element was found.</returns>
+ internal static bool FindElement(this EbmlReader reader, ulong identifier)
+ {
+ while (reader.ReadNext())
+ {
+ if (reader.ElementId.EncodedValue == identifier)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /// <summary>
+ /// Reads the current position in the file as an unsigned integer converted from binary.
+ /// </summary>
+ /// <param name="reader">An instance of <see cref="EbmlReader"/>.</param>
+ /// <returns>The unsigned integer.</returns>
+ internal static uint ReadUIntFromBinary(this EbmlReader reader)
+ {
+ var buffer = new byte[4];
+ reader.ReadBinary(buffer, 0, 4);
+ return BinaryPrimitives.ReadUInt32BigEndian(buffer);
+ }
+
+ /// <summary>
+ /// Reads from the start of the file to retrieve the SeekHead segment.
+ /// </summary>
+ /// <param name="reader">An instance of <see cref="EbmlReader"/>.</param>
+ /// <returns>Instance of <see cref="SeekHead"/>.</returns>
+ 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. SeekHead referencing another SeekHead is not supported");
+ }
+
+ return new SeekHead(infoPosition.Value, tracksPosition.Value, cuesPosition.Value);
+ }
+
+ /// <summary>
+ /// Reads from SegmentContainer to retrieve the Info segment.
+ /// </summary>
+ /// <param name="reader">An instance of <see cref="EbmlReader"/>.</param>
+ /// <param name="position">The position of the info segment relative to the Segment container.</param>
+ /// <returns>Instance of <see cref="Info"/>.</returns>
+ 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);
+ }
+
+ /// <summary>
+ /// Enters the Tracks segment and reads all tracks to find the specified type.
+ /// </summary>
+ /// <param name="reader">Instance of <see cref="EbmlReader"/>.</param>
+ /// <param name="tracksPosition">The relative position of the tracks segment.</param>
+ /// <param name="type">The track type identifier.</param>
+ /// <returns>The first track number with the specified type.</returns>
+ /// <exception cref="InvalidOperationException">Stream type is not found.</exception>
+ 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..0d5c2f34f
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaConstants.cs
@@ -0,0 +1,30 @@
+namespace Jellyfin.MediaEncoding.Keyframes.Matroska;
+
+/// <summary>
+/// Constants for the Matroska identifiers.
+/// </summary>
+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..501b2bb17
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs
@@ -0,0 +1,87 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions;
+using Jellyfin.MediaEncoding.Keyframes.Matroska.Models;
+using NEbml.Core;
+
+namespace Jellyfin.MediaEncoding.Keyframes.Matroska;
+
+/// <summary>
+/// The keyframe extractor for the matroska container.
+/// </summary>
+public static class MatroskaKeyframeExtractor
+{
+ /// <summary>
+ /// Extracts the keyframes in ticks (scaled using the container timestamp scale) from the matroska container.
+ /// </summary>
+ /// <param name="filePath">The file path.</param>
+ /// <returns>An instance of <see cref="KeyframeData"/>.</returns>
+ public static KeyframeData GetKeyframeData(string filePath)
+ {
+ using var stream = File.OpenRead(filePath);
+ using var reader = new EbmlReader(stream);
+
+ var seekHead = reader.ReadSeekHead();
+ // External lib does not support seeking backwards (yet)
+ Info info;
+ ulong videoTrackNumber;
+ if (seekHead.InfoPosition < seekHead.TracksPosition)
+ {
+ info = reader.ReadInfo(seekHead.InfoPosition);
+ videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo);
+ }
+ else
+ {
+ videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo);
+ info = reader.ReadInfo(seekHead.InfoPosition);
+ }
+
+ var keyframes = new List<long>();
+ 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(ScaleToTicks(cueTime, info.TimestampScale));
+ }
+
+ reader.LeaveContainer();
+ }
+
+ reader.LeaveContainer();
+
+ 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
new file mode 100644
index 000000000..415d6da00
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/Info.cs
@@ -0,0 +1,28 @@
+namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models;
+
+/// <summary>
+/// The matroska Info segment.
+/// </summary>
+internal class Info
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="Info"/> class.
+ /// </summary>
+ /// <param name="timestampScale">The timestamp scale in nanoseconds.</param>
+ /// <param name="duration">The duration of the entire file.</param>
+ public Info(long timestampScale, double? duration)
+ {
+ TimestampScale = timestampScale;
+ Duration = duration;
+ }
+
+ /// <summary>
+ /// Gets the timestamp scale in nanoseconds.
+ /// </summary>
+ public long TimestampScale { get; }
+
+ /// <summary>
+ /// Gets the total duration of the file.
+ /// </summary>
+ 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..95e4fd882
--- /dev/null
+++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/Models/SeekHead.cs
@@ -0,0 +1,35 @@
+namespace Jellyfin.MediaEncoding.Keyframes.Matroska.Models;
+
+/// <summary>
+/// The matroska SeekHead segment. All positions are relative to the Segment container.
+/// </summary>
+internal class SeekHead
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SeekHead"/> class.
+ /// </summary>
+ /// <param name="infoPosition">The relative file position of the info segment.</param>
+ /// <param name="tracksPosition">The relative file position of the tracks segment.</param>
+ /// <param name="cuesPosition">The relative file position of the cues segment.</param>
+ public SeekHead(long infoPosition, long tracksPosition, long cuesPosition)
+ {
+ InfoPosition = infoPosition;
+ TracksPosition = tracksPosition;
+ CuesPosition = cuesPosition;
+ }
+
+ /// <summary>
+ /// Gets relative file position of the info segment.
+ /// </summary>
+ public long InfoPosition { get; }
+
+ /// <summary>
+ /// Gets the relative file position of the tracks segment.
+ /// </summary>
+ public long TracksPosition { get; }
+
+ /// <summary>
+ /// Gets the relative file position of the cues segment.
+ /// </summary>
+ public long CuesPosition { get; }
+}