mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-05-26 10:36:57 +01:00
Refactor and add scheduled task
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user