Merge pull request #6600 from cvium/keyframe_extraction_v1

This commit is contained in:
Cody Robibero 2022-01-20 08:54:40 -07:00 committed by GitHub
commit 34ee6d82fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 20854 additions and 119 deletions

View File

@ -47,6 +47,7 @@ using Emby.Server.Implementations.TV;
using Emby.Server.Implementations.Udp;
using Emby.Server.Implementations.Updates;
using Jellyfin.Api.Helpers;
using Jellyfin.MediaEncoding.Hls.Playlist;
using Jellyfin.Networking.Configuration;
using Jellyfin.Networking.Manager;
using MediaBrowser.Common;
@ -999,6 +1000,9 @@ namespace Emby.Server.Implementations
// Network
yield return typeof(NetworkManager).Assembly;
// Hls
yield return typeof(DynamicHlsPlaylistGenerator).Assembly;
foreach (var i in GetAssembliesWithPartsInternal())
{
yield return i;

View File

@ -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
{
@ -55,6 +55,7 @@ namespace Jellyfin.Api.Controllers
private readonly TranscodingJobHelper _transcodingJobHelper;
private readonly ILogger<DynamicHlsController> _logger;
private readonly EncodingHelper _encodingHelper;
private readonly IDynamicHlsPlaylistGenerator _dynamicHlsPlaylistGenerator;
private readonly DynamicHlsHelper _dynamicHlsHelper;
private readonly EncodingOptions _encodingOptions;
@ -74,6 +75,7 @@ namespace Jellyfin.Api.Controllers
/// <param name="logger">Instance of the <see cref="ILogger{DynamicHlsController}"/> interface.</param>
/// <param name="dynamicHlsHelper">Instance of <see cref="DynamicHlsHelper"/>.</param>
/// <param name="encodingHelper">Instance of <see cref="EncodingHelper"/>.</param>
/// <param name="dynamicHlsPlaylistGenerator">Instance of <see cref="IDynamicHlsPlaylistGenerator"/>.</param>
public DynamicHlsController(
ILibraryManager libraryManager,
IUserManager userManager,
@ -87,7 +89,8 @@ namespace Jellyfin.Api.Controllers
TranscodingJobHelper transcodingJobHelper,
ILogger<DynamicHlsController> logger,
DynamicHlsHelper dynamicHlsHelper,
EncodingHelper encodingHelper)
EncodingHelper encodingHelper,
IDynamicHlsPlaylistGenerator dynamicHlsPlaylistGenerator)
{
_libraryManager = libraryManager;
_userManager = userManager;
@ -102,6 +105,7 @@ namespace Jellyfin.Api.Controllers
_logger = logger;
_dynamicHlsHelper = dynamicHlsHelper;
_encodingHelper = encodingHelper;
_dynamicHlsPlaylistGenerator = dynamicHlsPlaylistGenerator;
_encodingOptions = serverConfigurationManager.GetEncodingOptions();
}
@ -856,7 +860,7 @@ namespace Jellyfin.Api.Controllers
StreamOptions = streamOptions
};
return await GetVariantPlaylistInternal(streamingRequest, "main", cancellationTokenSource)
return await GetVariantPlaylistInternal(streamingRequest, cancellationTokenSource)
.ConfigureAwait(false);
}
@ -1021,7 +1025,7 @@ namespace Jellyfin.Api.Controllers
StreamOptions = streamOptions
};
return await GetVariantPlaylistInternal(streamingRequest, "main", cancellationTokenSource)
return await GetVariantPlaylistInternal(streamingRequest, cancellationTokenSource)
.ConfigureAwait(false);
}
@ -1032,13 +1036,15 @@ namespace Jellyfin.Api.Controllers
/// <param name="playlistId">The playlist id.</param>
/// <param name="segmentId">The segment id.</param>
/// <param name="container">The video container. Possible values are: ts, webm, asf, wmv, ogv, mp4, m4v, mkv, mpeg, mpg, avi, 3gp, wmv, wtv, m2ts, mov, iso, flv. </param>
/// <param name="runtimeTicks">The position of the requested segment in ticks.</param>
/// <param name="actualSegmentLengthTicks">The length of the requested segment in ticks.</param>
/// <param name="static">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.</param>
/// <param name="params">The streaming parameters.</param>
/// <param name="tag">The tag.</param>
/// <param name="deviceProfileId">Optional. The dlna device profile id to utilize.</param>
/// <param name="playSessionId">The play session id.</param>
/// <param name="segmentContainer">The segment container.</param>
/// <param name="segmentLength">The segment lenght.</param>
/// <param name="segmentLength">The desired segment length.</param>
/// <param name="minSegments">The minimum number of segments.</param>
/// <param name="mediaSourceId">The media version id, if playing an alternate version.</param>
/// <param name="deviceId">The device id of the client requesting. Used to stop encoding processes when needed.</param>
@ -1092,6 +1098,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,
@ -1145,6 +1153,8 @@ namespace Jellyfin.Api.Controllers
var streamingRequest = new VideoRequestDto
{
Id = itemId,
CurrentRuntimeTicks = runtimeTicks,
ActualSegmentLengthTicks = actualSegmentLengthTicks,
Container = container,
Static = @static ?? false,
Params = @params,
@ -1208,6 +1218,8 @@ namespace Jellyfin.Api.Controllers
/// <param name="playlistId">The playlist id.</param>
/// <param name="segmentId">The segment id.</param>
/// <param name="container">The video container. Possible values are: ts, webm, asf, wmv, ogv, mp4, m4v, mkv, mpeg, mpg, avi, 3gp, wmv, wtv, m2ts, mov, iso, flv. </param>
/// <param name="runtimeTicks">The position of the requested segment in ticks.</param>
/// <param name="actualSegmentLengthTicks">The length of the requested segment in ticks.</param>
/// <param name="static">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.</param>
/// <param name="params">The streaming parameters.</param>
/// <param name="tag">The tag.</param>
@ -1267,6 +1279,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,
@ -1320,6 +1334,8 @@ namespace Jellyfin.Api.Controllers
{
Id = itemId,
Container = container,
CurrentRuntimeTicks = runtimeTicks,
ActualSegmentLengthTicks = actualSegmentLengthTicks,
Static = @static ?? false,
Params = @params,
Tag = tag,
@ -1373,7 +1389,7 @@ namespace Jellyfin.Api.Controllers
.ConfigureAwait(false);
}
private async Task<ActionResult> GetVariantPlaylistInternal(StreamingRequestDto streamingRequest, string name, CancellationTokenSource cancellationTokenSource)
private async Task<ActionResult> GetVariantPlaylistInternal(StreamingRequestDto streamingRequest, CancellationTokenSource cancellationTokenSource)
{
using var state = await StreamingHelpers.GetStreamingState(
streamingRequest,
@ -1392,60 +1408,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 = EncodingHelper.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<ActionResult> GetDynamicSegment(StreamingRequestDto streamingRequest, int segmentId)
@ -1546,7 +1518,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(
@ -1897,7 +1869,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);
@ -1906,7 +1878,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);
}
}
@ -1921,8 +1893,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
@ -1953,16 +1925,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);
@ -1975,29 +1947,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);
@ -2076,29 +2025,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;
}
}
}

View File

@ -26,6 +26,7 @@
<ItemGroup>
<ProjectReference Include="..\Emby.Dlna\Emby.Dlna.csproj" />
<ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj" />
<ProjectReference Include="..\src\Jellyfin.MediaEncoding.Hls\Jellyfin.MediaEncoding.Hls.csproj" />
</ItemGroup>
<!-- Code Analyzers-->

View File

@ -41,5 +41,15 @@ namespace Jellyfin.Api.Models.StreamingDtos
/// Gets or sets the min segments.
/// </summary>
public int? MinSegments { get; set; }
/// <summary>
/// Gets or sets the position of the requested segment in ticks.
/// </summary>
public long CurrentRuntimeTicks { get; set; }
/// <summary>
/// Gets or sets the actual segment length in ticks.
/// </summary>
public long ActualSegmentLengthTicks { get; set; }
}
}

View File

@ -56,6 +56,7 @@
<ProjectReference Include="..\Emby.Server.Implementations\Emby.Server.Implementations.csproj" />
<ProjectReference Include="..\Jellyfin.Drawing.Skia\Jellyfin.Drawing.Skia.csproj" />
<ProjectReference Include="..\Jellyfin.Server.Implementations\Jellyfin.Server.Implementations.csproj" />
<ProjectReference Include="..\src\Jellyfin.MediaEncoding.Hls\Jellyfin.MediaEncoding.Hls.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -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<JellyfinDb>();
services.AddHlsPlaylistGenerator();
}
/// <summary>

View File

@ -89,6 +89,14 @@ 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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.MediaEncoding.Hls.Tests", "tests\Jellyfin.MediaEncoding.Hls.Tests\Jellyfin.MediaEncoding.Hls.Tests.csproj", "{FE47334C-EFDE-4519-BD50-F24430FF360B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.MediaEncoding.Keyframes.Tests", "tests\Jellyfin.MediaEncoding.Keyframes.Tests\Jellyfin.MediaEncoding.Keyframes.Tests.csproj", "{24960660-DE6C-47BF-AEEF-CEE8F19FE6C2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -219,10 +227,6 @@ Global
{42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42816EA8-4511-4CBF-A9C7-7791D5DDDAE6}.Release|Any CPU.Build.0 = Release|Any CPU
{25E40B0B-7C89-4230-8911-CBBBCE83FC5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25E40B0B-7C89-4230-8911-CBBBCE83FC5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25E40B0B-7C89-4230-8911-CBBBCE83FC5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25E40B0B-7C89-4230-8911-CBBBCE83FC5B}.Release|Any CPU.Build.0 = Release|Any CPU
{3ADBCD8C-C0F2-4956-8FDC-35D686B74CF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3ADBCD8C-C0F2-4956-8FDC-35D686B74CF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3ADBCD8C-C0F2-4956-8FDC-35D686B74CF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -243,6 +247,22 @@ 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 = Debug|Any CPU
{06535CA1-4097-4360-85EB-5FB875D53239}.Release|Any CPU.Build.0 = Debug|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 = Debug|Any CPU
{DA9FD356-4894-4830-B208-D6BCE3E65B11}.Release|Any CPU.Build.0 = Debug|Any CPU
{FE47334C-EFDE-4519-BD50-F24430FF360B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE47334C-EFDE-4519-BD50-F24430FF360B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE47334C-EFDE-4519-BD50-F24430FF360B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE47334C-EFDE-4519-BD50-F24430FF360B}.Release|Any CPU.Build.0 = Release|Any CPU
{24960660-DE6C-47BF-AEEF-CEE8F19FE6C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24960660-DE6C-47BF-AEEF-CEE8F19FE6C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24960660-DE6C-47BF-AEEF-CEE8F19FE6C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24960660-DE6C-47BF-AEEF-CEE8F19FE6C2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -263,6 +283,10 @@ 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}
{FE47334C-EFDE-4519-BD50-F24430FF360B} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
{24960660-DE6C-47BF-AEEF-CEE8F19FE6C2} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3448830C-EBDC-426C-85CD-7BBB9651A7FE}

View File

@ -25,6 +25,12 @@ namespace MediaBrowser.Controller.MediaEncoding
/// <value>The encoder path.</value>
string EncoderPath { get; }
/// <summary>
/// Gets the probe path.
/// </summary>
/// <value>The probe path.</value>
string ProbePath { get; }
/// <summary>
/// Gets the version of encoder.
/// </summary>

View File

@ -92,6 +92,9 @@ namespace MediaBrowser.MediaEncoding.Encoder
/// <inheritdoc />
public string EncoderPath => _ffmpegPath;
/// <inheritdoc />
public string ProbePath => _ffprobePath;
public Version EncoderVersion => _ffmpegVersion;
public bool IsVaapiDeviceAmd => _isVaapiDeviceAmd;

View File

@ -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;
AllowOnDemandMetadataBasedKeyframeExtractionForExtensions = Array.Empty<string>();
HardwareDecodingCodecs = new string[] { "h264", "vc1" };
}
@ -115,5 +118,7 @@ namespace MediaBrowser.Model.Configuration
public bool EnableSubtitleExtraction { get; set; }
public string[] HardwareDecodingCodecs { get; set; }
public string[] AllowOnDemandMetadataBasedKeyframeExtractionForExtensions { get; set; }
}
}

View File

@ -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;
}
}

View File

@ -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;
});
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<!-- Code Analyzers-->
<ItemGroup>
<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>

View File

@ -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; }
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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>();
}

View File

@ -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);
}
}

View File

@ -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();
}

View File

@ -0,0 +1,29 @@
<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="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>

View File

@ -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; }
}

View File

@ -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");
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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; }
}

View File

@ -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; }
}

View File

@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<CodeAnalysisRuleSet>../jellyfin-tests.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<!-- Code Analyzers -->
<ItemGroup>
<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="..\..\src\Jellyfin.MediaEncoding.Hls\Jellyfin.MediaEncoding.Hls.csproj" />
<ProjectReference Include="..\..\src\Jellyfin.MediaEncoding.Keyframes\Jellyfin.MediaEncoding.Keyframes.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,101 @@
using System;
using Jellyfin.MediaEncoding.Hls.Playlist;
using Jellyfin.MediaEncoding.Keyframes;
using Xunit;
namespace Jellyfin.MediaEncoding.Hls.Tests.Playlist
{
public class DynamicHlsPlaylistGeneratorTests
{
[Theory]
[MemberData(nameof(ComputeSegments_Valid_Success_Data))]
public void ComputeSegments_Valid_Success(KeyframeData keyframeData, int desiredSegmentLengthMs, double[] segments)
{
Assert.Equal(segments, DynamicHlsPlaylistGenerator.ComputeSegments(keyframeData, desiredSegmentLengthMs));
}
[Fact]
public void ComputeSegments_InvalidDuration_ThrowsArgumentException()
{
var keyframeData = new KeyframeData(0, new[] { MsToTicks(10000) });
Assert.Throws<ArgumentException>(() => DynamicHlsPlaylistGenerator.ComputeSegments(keyframeData, 6000));
}
[Theory]
[MemberData(nameof(ComputeEqualLengthSegments_Valid_Success_Data))]
public void ComputeEqualLengthSegments_Valid_Success(int desiredSegmentLengthMs, long totalRuntimeTicks, double[] segments)
{
Assert.Equal(segments, DynamicHlsPlaylistGenerator.ComputeEqualLengthSegments(desiredSegmentLengthMs, totalRuntimeTicks));
}
[Theory]
[InlineData(0, 1000000)]
[InlineData(1000, 0)]
public void ComputeEqualLengthSegments_Invalid_ThrowsInvalidOperationException(int desiredSegmentLengthMs, long totalRuntimeTicks)
{
Assert.Throws<InvalidOperationException>(() => DynamicHlsPlaylistGenerator.ComputeEqualLengthSegments(desiredSegmentLengthMs, totalRuntimeTicks));
}
[Theory]
[InlineData("testfile.mkv", new string[0], false)]
[InlineData("testfile.flv", new[] { ".mp4", ".mkv", ".ts" }, false)]
[InlineData("testfile.flv", new[] { ".mp4", ".mkv", ".ts", ".flv" }, true)]
[InlineData("/some/arbitrarily/long/path/testfile.mkv", new[] { "mkv" }, true)]
public void IsExtractionAllowedForFile_Valid_Success(string filePath, string[] allowedExtensions, bool isAllowed)
{
Assert.Equal(isAllowed, DynamicHlsPlaylistGenerator.IsExtractionAllowedForFile(filePath, allowedExtensions));
}
[Theory]
[InlineData("testfile", new[] { ".mp4" })]
public void IsExtractionAllowedForFile_Invalid_ReturnsFalse(string filePath, string[] allowedExtensions)
{
Assert.False(DynamicHlsPlaylistGenerator.IsExtractionAllowedForFile(filePath, allowedExtensions));
}
private static TheoryData<int, long, double[]> ComputeEqualLengthSegments_Valid_Success_Data()
{
var data = new TheoryData<int, long, double[]>
{
{ 6000, MsToTicks(13000), new[] { 6.0, 6.0, 1.0 } },
{ 3000, MsToTicks(15000), new[] { 3.0, 3.0, 3.0, 3.0, 3.0 } },
{ 6000, MsToTicks(25000), new[] { 6.0, 6.0, 6.0, 6.0, 1.0 } },
{ 6000, MsToTicks(20123), new[] { 6.0, 6.0, 6.0, 2.123 } },
{ 6000, MsToTicks(1234), new[] { 1.234 } }
};
return data;
}
private static TheoryData<KeyframeData, int, double[]> ComputeSegments_Valid_Success_Data()
{
var data = new TheoryData<KeyframeData, int, double[]>
{
{
new KeyframeData(MsToTicks(35000), new[] { 0, MsToTicks(10427), MsToTicks(20854), MsToTicks(31240) }),
6000,
new[] { 10.427, 10.427, 10.386, 3.760 }
},
{
new KeyframeData(MsToTicks(10000), new[] { 0, MsToTicks(1000), MsToTicks(2000), MsToTicks(3000), MsToTicks(4000), MsToTicks(5000) }),
2000,
new[] { 2.0, 2.0, 6.0 }
},
{
new KeyframeData(MsToTicks(10000), new[] { 0L }),
6000,
new[] { 10.0 }
},
{
new KeyframeData(MsToTicks(10000), Array.Empty<long>()),
6000,
new[] { 10.0 }
}
};
return data;
}
private static long MsToTicks(int value) => TimeSpan.FromMilliseconds(value).Ticks;
}
}

View File

@ -0,0 +1,28 @@
using System.IO;
using System.Text.Json;
using Xunit;
namespace Jellyfin.MediaEncoding.Keyframes.FfProbe
{
public class FfProbeKeyframeExtractorTests
{
[Theory]
[InlineData("keyframes.txt", "keyframes_result.json")]
[InlineData("keyframes_streamduration.txt", "keyframes_streamduration_result.json")]
public void ParseStream_Valid_Success(string testDataFileName, string resultFileName)
{
var testDataPath = Path.Combine("FfProbe/Test Data", testDataFileName);
var resultPath = Path.Combine("FfProbe/Test Data", resultFileName);
using var resultFileStream = File.OpenRead(resultPath);
var expectedResult = JsonSerializer.Deserialize<KeyframeData>(resultFileStream)!;
using var fileStream = File.OpenRead(testDataPath);
using var streamReader = new StreamReader(fileStream);
var result = FfProbeKeyframeExtractor.ParseStream(streamReader);
Assert.Equal(expectedResult.TotalDuration, result.TotalDuration);
Assert.Equal(expectedResult.KeyframeTicks, result.KeyframeTicks);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
{"TotalDuration":7063360000,"KeyframeTicks":[0,103850000,133880000,145150000,165580000,186440000,196450000,209790000,314060000,326990000,396230000,407070000,432520000,476310000,523020000,535540000,550550000,631050000,646480000,665670000,686520000,732400000,772020000,796210000,856690000,887970000,903820000,934270000,983070000,1056060000,1087750000,1187850000,1222050000,1251250000,1265430000,1305470000,1333830000,1345510000,1356770000,1368450000,1427260000,1460630000,1500670000,1540710000,1584500000,1607020000,1627880000,1639550000,1672090000,1685020000,1789290000,1883130000,1909820000,1931510000,1996580000,2017020000,2035370000,2051220000,2065400000,2085000000,2109190000,2120870000,2168420000,2253920000,2295210000,2374460000,2478730000,2582160000,2607190000,2697280000,2783610000,2825320000,2899560000,2929590000,2979230000,3017600000,3048880000,3073490000,3117700000,3141050000,3158160000,3200700000,3279530000,3299960000,3312890000,3332910000,3369200000,3379630000,3438440000,3459290000,3490990000,3533110000,3562730000,3600260000,3624040000,3672000000,3722050000,3753330000,3771270000,3875540000,3957290000,4016100000,4100350000,4114530000,4124540000,4157900000,4180430000,4200450000,4222550000,4252160000,4295960000,4309720000,4328070000,4340590000,4371450000,4400230000,4426920000,4489490000,4512010000,4531190000,4569570000,4599600000,4635460000,4660070000,4680930000,4729310000,4757670000,4777690000,4808550000,4824400000,4851100000,4864440000,4905320000,4955370000,4970380000,5074650000,5095090000,5109270000,5186010000,5204370000,5227720000,5242740000,5266930000,5342000000,5433760000,5447110000,5470470000,5520520000,5550550000,5565140000,5611020000,5642300000,5668160000,5711120000,5743240000,5762420000,5797460000,5817480000,5839170000,5855850000,5870870000,5904230000,5969300000,6056880000,6104850000,6152400000,6256250000,6295870000,6310050000,6325900000,6341750000,6356770000,6385960000,6426840000,6454780000,6469800000,6514420000,6549460000,6574480000,6602010000,6619530000,6654560000,6667080000,6690430000,6724630000,6762170000,6812220000,6849760000,6875200000,6912740000,6983230000,6994900000,7024930000]}

View File

@ -0,0 +1 @@
{"TotalDuration":1000000000,"KeyframeTicks":[0,103850000,133880000,145150000,165580000,186440000,196450000,209790000,314060000,326990000,396230000,407070000,432520000,476310000,523020000,535540000,550550000,631050000,646480000,665670000,686520000,732400000,772020000,796210000,856690000,887970000,903820000,934270000,983070000]}

View File

@ -0,0 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<CodeAnalysisRuleSet>../jellyfin-tests.ruleset</CodeAnalysisRuleSet>
<RootNamespace>Jellyfin.MediaEncoding.Keyframes</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<!-- Code Analyzers -->
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<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="../../src/Jellyfin.MediaEncoding.Keyframes/Jellyfin.MediaEncoding.Keyframes.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="FfProbe/Test Data/keyframes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FfProbe/Test Data/keyframes_result.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FfProbe/Test Data/keyframes_streamduration.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FfProbe/Test Data/keyframes_streamduration_result.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>