mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-02 00:11:55 +01:00
Refactor and add scheduled task
This commit is contained in:
87
src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs
Normal file
87
src/Jellyfin.MediaEncoding.Hls/Cache/CacheDecorator.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Jellyfin.Extensions.Json;
|
||||
using Jellyfin.MediaEncoding.Hls.Extractors;
|
||||
using Jellyfin.MediaEncoding.Keyframes;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
|
||||
namespace Jellyfin.MediaEncoding.Hls.Cache;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class CacheDecorator : IKeyframeExtractor
|
||||
{
|
||||
private readonly IKeyframeExtractor _keyframeExtractor;
|
||||
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>
|
||||
public CacheDecorator(IApplicationPaths applicationPaths, IKeyframeExtractor keyframeExtractor)
|
||||
{
|
||||
_keyframeExtractor = keyframeExtractor;
|
||||
ArgumentNullException.ThrowIfNull(applicationPaths);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
keyframeData = result;
|
||||
SaveToCache(cachePath, keyframeData);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void SaveToCache(string cachePath, KeyframeData keyframeData)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(keyframeData, _jsonOptions);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(cachePath) ?? throw new ArgumentException($"Provided path ({cachePath}) is not valid.", nameof(cachePath)));
|
||||
File.WriteAllText(cachePath, json);
|
||||
}
|
||||
|
||||
private static string GetCachePath(string keyframeCachePath, string filePath)
|
||||
{
|
||||
var lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath);
|
||||
ReadOnlySpan<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;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,36 @@
|
||||
using Jellyfin.MediaEncoding.Hls.Playlist;
|
||||
using System;
|
||||
using Jellyfin.MediaEncoding.Hls.Cache;
|
||||
using Jellyfin.MediaEncoding.Hls.Extractors;
|
||||
using Jellyfin.MediaEncoding.Hls.Playlist;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Jellyfin.MediaEncoding.Hls.Extensions
|
||||
namespace Jellyfin.MediaEncoding.Hls.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for the <see cref="IServiceCollection"/> interface.
|
||||
/// </summary>
|
||||
public static class MediaEncodingHlsServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for the <see cref="IServiceCollection"/> interface.
|
||||
/// Adds the hls playlist generators to the <see cref="IServiceCollection"/>.
|
||||
/// </summary>
|
||||
public static class MediaEncodingHlsServiceCollectionExtensions
|
||||
/// <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)
|
||||
{
|
||||
/// <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 =>
|
||||
{
|
||||
return serviceCollection.AddSingleton<IDynamicHlsPlaylistGenerator, DynamicHlsPlaylistGenerator>();
|
||||
}
|
||||
var extractor = ActivatorUtilities.CreateInstance(serviceProvider, type);
|
||||
var decorator = ActivatorUtilities.CreateInstance<CacheDecorator>(serviceProvider, extractor);
|
||||
return decorator;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,57 +1,56 @@
|
||||
namespace Jellyfin.MediaEncoding.Hls.Playlist
|
||||
namespace Jellyfin.MediaEncoding.Hls.Playlist;
|
||||
|
||||
/// <summary>
|
||||
/// Request class for the <see cref="IDynamicHlsPlaylistGenerator.CreateMainPlaylist(CreateMainPlaylistRequest)"/> method.
|
||||
/// </summary>
|
||||
public class CreateMainPlaylistRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Request class for the <see cref="IDynamicHlsPlaylistGenerator.CreateMainPlaylist(CreateMainPlaylistRequest)"/> method.
|
||||
/// Initializes a new instance of the <see cref="CreateMainPlaylistRequest"/> class.
|
||||
/// </summary>
|
||||
public class CreateMainPlaylistRequest
|
||||
/// <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)
|
||||
{
|
||||
/// <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; }
|
||||
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; }
|
||||
}
|
||||
|
||||
@@ -5,269 +5,200 @@ using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Jellyfin.Extensions.Json;
|
||||
using Jellyfin.MediaEncoding.Hls.Extractors;
|
||||
using Jellyfin.MediaEncoding.Keyframes;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.MediaEncoding;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.MediaEncoding.Hls.Playlist
|
||||
namespace Jellyfin.MediaEncoding.Hls.Playlist;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class DynamicHlsPlaylistGenerator : IDynamicHlsPlaylistGenerator
|
||||
{
|
||||
/// <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)
|
||||
{
|
||||
private const string DefaultContainerExtension = ".ts";
|
||||
_serverConfigurationManager = serverConfigurationManager;
|
||||
_extractors = extractors.Where(e => e.IsMetadataBased).ToArray();
|
||||
}
|
||||
|
||||
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
||||
private readonly IServerConfigurationManager _serverConfigurationManager;
|
||||
private readonly IMediaEncoder _mediaEncoder;
|
||||
private readonly IApplicationPaths _applicationPaths;
|
||||
private readonly KeyframeExtractor _keyframeExtractor;
|
||||
private readonly ILogger<DynamicHlsPlaylistGenerator> _logger;
|
||||
|
||||
/// <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="mediaEncoder">An instance of the see <see cref="IMediaEncoder"/> interface.</param>
|
||||
/// <param name="applicationPaths">An instance of the <see cref="IApplicationPaths"/> interface.</param>
|
||||
/// <param name="loggerFactory">An instance of the see <see cref="ILoggerFactory"/> interface.</param>
|
||||
public DynamicHlsPlaylistGenerator(IServerConfigurationManager serverConfigurationManager, IMediaEncoder mediaEncoder, IApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
|
||||
/// <inheritdoc />
|
||||
public string CreateMainPlaylist(CreateMainPlaylistRequest request)
|
||||
{
|
||||
IReadOnlyList<double> segments;
|
||||
if (TryExtractKeyframes(request.FilePath, out var keyframeData))
|
||||
{
|
||||
_serverConfigurationManager = serverConfigurationManager;
|
||||
_mediaEncoder = mediaEncoder;
|
||||
_applicationPaths = applicationPaths;
|
||||
_keyframeExtractor = new KeyframeExtractor(loggerFactory.CreateLogger<KeyframeExtractor>());
|
||||
_logger = loggerFactory.CreateLogger<DynamicHlsPlaylistGenerator>();
|
||||
segments = ComputeSegments(keyframeData, request.DesiredSegmentLengthMs);
|
||||
}
|
||||
else
|
||||
{
|
||||
segments = ComputeEqualLengthSegments(request.DesiredSegmentLengthMs, request.TotalRuntimeTicks);
|
||||
}
|
||||
|
||||
private string KeyframeCachePath => Path.Combine(_applicationPaths.DataPath, "keyframes");
|
||||
var segmentExtension = EncodingHelper.GetSegmentFileExtension(request.SegmentContainer);
|
||||
|
||||
/// <inheritdoc />
|
||||
public string CreateMainPlaylist(CreateMainPlaylistRequest request)
|
||||
// http://ffmpeg.org/ffmpeg-all.html#toc-hls-2
|
||||
var isHlsInFmp4 = string.Equals(segmentExtension, "mp4", StringComparison.OrdinalIgnoreCase);
|
||||
var hlsVersion = isHlsInFmp4 ? "7" : "3";
|
||||
|
||||
var builder = new StringBuilder(128);
|
||||
|
||||
builder.AppendLine("#EXTM3U")
|
||||
.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD")
|
||||
.Append("#EXT-X-VERSION:")
|
||||
.Append(hlsVersion)
|
||||
.AppendLine()
|
||||
.Append("#EXT-X-TARGETDURATION:")
|
||||
.Append(Math.Ceiling(segments.Count > 0 ? segments.Max() : request.DesiredSegmentLengthMs))
|
||||
.AppendLine()
|
||||
.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
|
||||
|
||||
var index = 0;
|
||||
|
||||
if (isHlsInFmp4)
|
||||
{
|
||||
IReadOnlyList<double> segments;
|
||||
if (TryExtractKeyframes(request.FilePath, out var keyframeData))
|
||||
{
|
||||
segments = ComputeSegments(keyframeData, request.DesiredSegmentLengthMs);
|
||||
}
|
||||
else
|
||||
{
|
||||
segments = ComputeEqualLengthSegments(request.DesiredSegmentLengthMs, request.TotalRuntimeTicks);
|
||||
}
|
||||
|
||||
var segmentExtension = GetSegmentFileExtension(request.SegmentContainer);
|
||||
|
||||
// http://ffmpeg.org/ffmpeg-all.html#toc-hls-2
|
||||
var isHlsInFmp4 = string.Equals(segmentExtension, "mp4", StringComparison.OrdinalIgnoreCase);
|
||||
var hlsVersion = isHlsInFmp4 ? "7" : "3";
|
||||
|
||||
var builder = new StringBuilder(128);
|
||||
|
||||
builder.AppendLine("#EXTM3U")
|
||||
.AppendLine("#EXT-X-PLAYLIST-TYPE:VOD")
|
||||
.Append("#EXT-X-VERSION:")
|
||||
.Append(hlsVersion)
|
||||
.AppendLine()
|
||||
.Append("#EXT-X-TARGETDURATION:")
|
||||
.Append(Math.Ceiling(segments.Count > 0 ? segments.Max() : request.DesiredSegmentLengthMs))
|
||||
.AppendLine()
|
||||
.AppendLine("#EXT-X-MEDIA-SEQUENCE:0");
|
||||
|
||||
var index = 0;
|
||||
|
||||
if (isHlsInFmp4)
|
||||
{
|
||||
builder.Append("#EXT-X-MAP:URI=\"")
|
||||
.Append(request.EndpointPrefix)
|
||||
.Append("-1")
|
||||
.Append(segmentExtension)
|
||||
.Append(request.QueryString)
|
||||
.Append('"')
|
||||
.AppendLine();
|
||||
}
|
||||
|
||||
long currentRuntimeInSeconds = 0;
|
||||
foreach (var length in segments)
|
||||
{
|
||||
// Manually convert to ticks to avoid precision loss when converting double
|
||||
var lengthTicks = Convert.ToInt64(length * TimeSpan.TicksPerSecond);
|
||||
builder.Append("#EXTINF:")
|
||||
.Append(length.ToString("0.000000", CultureInfo.InvariantCulture))
|
||||
.AppendLine(", nodesc")
|
||||
.Append(request.EndpointPrefix)
|
||||
.Append(index++)
|
||||
.Append(segmentExtension)
|
||||
.Append(request.QueryString)
|
||||
.Append("&runtimeTicks=")
|
||||
.Append(currentRuntimeInSeconds)
|
||||
.Append("&actualSegmentLengthTicks=")
|
||||
.Append(lengthTicks)
|
||||
.AppendLine();
|
||||
|
||||
currentRuntimeInSeconds += lengthTicks;
|
||||
}
|
||||
|
||||
builder.AppendLine("#EXT-X-ENDLIST");
|
||||
|
||||
return builder.ToString();
|
||||
builder.Append("#EXT-X-MAP:URI=\"")
|
||||
.Append(request.EndpointPrefix)
|
||||
.Append("-1")
|
||||
.Append(segmentExtension)
|
||||
.Append(request.QueryString)
|
||||
.Append('"')
|
||||
.AppendLine();
|
||||
}
|
||||
|
||||
private bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData)
|
||||
long currentRuntimeInSeconds = 0;
|
||||
foreach (var length in segments)
|
||||
{
|
||||
keyframeData = null;
|
||||
if (!IsExtractionAllowedForFile(filePath, _serverConfigurationManager.GetEncodingOptions().AllowAutomaticKeyframeExtractionForExtensions))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Manually convert to ticks to avoid precision loss when converting double
|
||||
var lengthTicks = Convert.ToInt64(length * TimeSpan.TicksPerSecond);
|
||||
builder.Append("#EXTINF:")
|
||||
.Append(length.ToString("0.000000", CultureInfo.InvariantCulture))
|
||||
.AppendLine(", nodesc")
|
||||
.Append(request.EndpointPrefix)
|
||||
.Append(index++)
|
||||
.Append(segmentExtension)
|
||||
.Append(request.QueryString)
|
||||
.Append("&runtimeTicks=")
|
||||
.Append(currentRuntimeInSeconds)
|
||||
.Append("&actualSegmentLengthTicks=")
|
||||
.Append(lengthTicks)
|
||||
.AppendLine();
|
||||
|
||||
var succeeded = false;
|
||||
var cachePath = GetCachePath(filePath);
|
||||
if (TryReadFromCache(cachePath, out var cachedResult))
|
||||
{
|
||||
keyframeData = cachedResult;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
keyframeData = _keyframeExtractor.GetKeyframeData(filePath, _mediaEncoder.ProbePath, string.Empty);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Keyframe extraction failed for path {FilePath}", filePath);
|
||||
return false;
|
||||
}
|
||||
|
||||
succeeded = keyframeData.KeyframeTicks.Count > 0;
|
||||
if (succeeded)
|
||||
{
|
||||
CacheResult(cachePath, keyframeData);
|
||||
}
|
||||
}
|
||||
|
||||
return succeeded;
|
||||
currentRuntimeInSeconds += lengthTicks;
|
||||
}
|
||||
|
||||
private void CacheResult(string cachePath, KeyframeData keyframeData)
|
||||
builder.AppendLine("#EXT-X-ENDLIST");
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private bool TryExtractKeyframes(string filePath, [NotNullWhen(true)] out KeyframeData? keyframeData)
|
||||
{
|
||||
keyframeData = null;
|
||||
if (!IsExtractionAllowedForFile(filePath, _serverConfigurationManager.GetEncodingOptions().AllowOnDemandMetadataBasedKeyframeExtractionForExtensions))
|
||||
{
|
||||
var json = JsonSerializer.Serialize(keyframeData, _jsonOptions);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(cachePath) ?? throw new ArgumentException($"Provided path ({cachePath}) is not valid.", nameof(cachePath)));
|
||||
File.WriteAllText(cachePath, json);
|
||||
}
|
||||
|
||||
private string GetCachePath(string filePath)
|
||||
{
|
||||
var lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath);
|
||||
ReadOnlySpan<char> filename = (filePath + "_" + lastWriteTimeUtc.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5() + ".json";
|
||||
var prefix = filename.Slice(0, 1);
|
||||
|
||||
return Path.Join(KeyframeCachePath, prefix, filename);
|
||||
}
|
||||
|
||||
private bool TryReadFromCache(string cachePath, [NotNullWhen(true)] out KeyframeData? cachedResult)
|
||||
{
|
||||
if (File.Exists(cachePath))
|
||||
{
|
||||
var bytes = File.ReadAllBytes(cachePath);
|
||||
cachedResult = JsonSerializer.Deserialize<KeyframeData>(bytes, _jsonOptions);
|
||||
return cachedResult != null;
|
||||
}
|
||||
|
||||
cachedResult = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static bool IsExtractionAllowedForFile(ReadOnlySpan<char> filePath, string[] allowedExtensions)
|
||||
var len = _extractors.Length;
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
var extension = Path.GetExtension(filePath);
|
||||
if (extension.IsEmpty)
|
||||
var extractor = _extractors[i];
|
||||
if (!extractor.TryExtractKeyframes(filePath, out var result))
|
||||
{
|
||||
return false;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Remove the leading dot
|
||||
var extensionWithoutDot = extension[1..];
|
||||
for (var i = 0; i < allowedExtensions.Length; i++)
|
||||
{
|
||||
var allowedExtension = allowedExtensions[i];
|
||||
if (extensionWithoutDot.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
keyframeData = result;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static bool IsExtractionAllowedForFile(ReadOnlySpan<char> filePath, string[] allowedExtensions)
|
||||
{
|
||||
var extension = Path.GetExtension(filePath);
|
||||
if (extension.IsEmpty)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static IReadOnlyList<double> ComputeSegments(KeyframeData keyframeData, int desiredSegmentLengthMs)
|
||||
// Remove the leading dot
|
||||
var extensionWithoutDot = extension[1..];
|
||||
for (var i = 0; i < allowedExtensions.Length; i++)
|
||||
{
|
||||
if (keyframeData.KeyframeTicks.Count > 0 && keyframeData.TotalDuration < keyframeData.KeyframeTicks[^1])
|
||||
var allowedExtension = allowedExtensions[i].AsSpan().TrimStart('.');
|
||||
if (extensionWithoutDot.Equals(allowedExtension, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new ArgumentException("Invalid duration in keyframe data", nameof(keyframeData));
|
||||
return true;
|
||||
}
|
||||
|
||||
long lastKeyframe = 0;
|
||||
var result = new List<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)
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static IReadOnlyList<double> ComputeSegments(KeyframeData keyframeData, int desiredSegmentLengthMs)
|
||||
{
|
||||
if (keyframeData.KeyframeTicks.Count > 0 && keyframeData.TotalDuration < keyframeData.KeyframeTicks[^1])
|
||||
{
|
||||
if (desiredSegmentLengthMs == 0 || totalRuntimeTicks == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Invalid segment length ({desiredSegmentLengthMs}) or runtime ticks ({totalRuntimeTicks})");
|
||||
}
|
||||
|
||||
var desiredSegmentLength = TimeSpan.FromMilliseconds(desiredSegmentLengthMs);
|
||||
|
||||
var segmentLengthTicks = desiredSegmentLength.Ticks;
|
||||
var wholeSegments = totalRuntimeTicks / segmentLengthTicks;
|
||||
var remainingTicks = totalRuntimeTicks % segmentLengthTicks;
|
||||
|
||||
var segmentsLen = wholeSegments + (remainingTicks == 0 ? 0 : 1);
|
||||
var segments = new double[segmentsLen];
|
||||
for (int i = 0; i < wholeSegments; i++)
|
||||
{
|
||||
segments[i] = desiredSegmentLength.TotalSeconds;
|
||||
}
|
||||
|
||||
if (remainingTicks != 0)
|
||||
{
|
||||
segments[^1] = TimeSpan.FromTicks(remainingTicks).TotalSeconds;
|
||||
}
|
||||
|
||||
return segments;
|
||||
throw new ArgumentException("Invalid duration in keyframe data", nameof(keyframeData));
|
||||
}
|
||||
|
||||
// TODO copied from DynamicHlsController
|
||||
private static string GetSegmentFileExtension(string segmentContainer)
|
||||
long lastKeyframe = 0;
|
||||
var result = new List<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++)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(segmentContainer))
|
||||
var keyframe = keyframeData.KeyframeTicks[j];
|
||||
if (keyframe >= desiredCutTime)
|
||||
{
|
||||
return "." + segmentContainer;
|
||||
var currentSegmentLength = keyframe - lastKeyframe;
|
||||
result.Add(TimeSpan.FromTicks(currentSegmentLength).TotalSeconds);
|
||||
lastKeyframe = keyframe;
|
||||
desiredCutTime += desiredSegmentLengthTicks;
|
||||
}
|
||||
|
||||
return DefaultContainerExtension;
|
||||
}
|
||||
|
||||
result.Add(TimeSpan.FromTicks(keyframeData.TotalDuration - lastKeyframe).TotalSeconds);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static double[] ComputeEqualLengthSegments(int desiredSegmentLengthMs, long totalRuntimeTicks)
|
||||
{
|
||||
if (desiredSegmentLengthMs == 0 || totalRuntimeTicks == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Invalid segment length ({desiredSegmentLengthMs}) or runtime ticks ({totalRuntimeTicks})");
|
||||
}
|
||||
|
||||
var desiredSegmentLength = TimeSpan.FromMilliseconds(desiredSegmentLengthMs);
|
||||
|
||||
var segmentLengthTicks = desiredSegmentLength.Ticks;
|
||||
var wholeSegments = totalRuntimeTicks / segmentLengthTicks;
|
||||
var remainingTicks = totalRuntimeTicks % segmentLengthTicks;
|
||||
|
||||
var segmentsLen = wholeSegments + (remainingTicks == 0 ? 0 : 1);
|
||||
var segments = new double[segmentsLen];
|
||||
for (int i = 0; i < wholeSegments; i++)
|
||||
{
|
||||
segments[i] = desiredSegmentLength.TotalSeconds;
|
||||
}
|
||||
|
||||
if (remainingTicks != 0)
|
||||
{
|
||||
segments[^1] = TimeSpan.FromTicks(remainingTicks).TotalSeconds;
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
namespace Jellyfin.MediaEncoding.Hls.Playlist
|
||||
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>
|
||||
/// Generator for dynamic HLS playlists where the segment lengths aren't known in advance.
|
||||
/// Creates the main playlist containing the main video or audio stream.
|
||||
/// </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);
|
||||
}
|
||||
/// <param name="request">An instance of the <see cref="CreateMainPlaylistRequest"/> class.</param>
|
||||
/// <returns>The playlist as a formatted string.</returns>
|
||||
string CreateMainPlaylist(CreateMainPlaylistRequest request);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Data.Enums;
|
||||
using Jellyfin.MediaEncoding.Hls.Extractors;
|
||||
using MediaBrowser.Controller.Dto;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Globalization;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
|
||||
namespace Jellyfin.MediaEncoding.Hls.ScheduledTasks;
|
||||
|
||||
/// <inheritdoc />
|
||||
public class KeyframeExtractionScheduledTask : IScheduledTask
|
||||
{
|
||||
private readonly ILocalizationManager _localizationManager;
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly IKeyframeExtractor[] _keyframeExtractors;
|
||||
private static readonly BaseItemKind[] _itemTypes = { BaseItemKind.Episode, BaseItemKind.Movie };
|
||||
|
||||
/// <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.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";
|
||||
|
||||
/// <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
|
||||
};
|
||||
|
||||
var videos = _libraryManager.GetItemList(query);
|
||||
|
||||
// TODO parallelize with Parallel.ForEach?
|
||||
for (var i = 0; i < videos.Count; i++)
|
||||
{
|
||||
var video = videos[i];
|
||||
// Only local files supported
|
||||
if (!video.IsFileProtocol || !File.Exists(video.Path))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var j = 0; j < _keyframeExtractors.Length; j++)
|
||||
{
|
||||
var extractor = _keyframeExtractors[j];
|
||||
// The cache decorator will make sure to save them in the data dir
|
||||
if (extractor.TryExtractKeyframes(video.Path, out _))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => Enumerable.Empty<TaskTriggerInfo>();
|
||||
}
|
||||
Reference in New Issue
Block a user