using System;
using System.IO;
using Jellyfin.MediaEncoding.Keyframes.FfProbe;
using Jellyfin.MediaEncoding.Keyframes.FfTool;
using Jellyfin.MediaEncoding.Keyframes.Matroska;
using Microsoft.Extensions.Logging;
namespace Jellyfin.MediaEncoding.Keyframes
{
///
/// Manager class for the set of keyframe extractors.
///
public class KeyframeExtractor
{
private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// An instance of the interface.
public KeyframeExtractor(ILogger logger)
{
_logger = logger;
}
///
/// Extracts the keyframe positions from a video file.
///
/// Absolute file path to the media file.
/// Absolute file path to the ffprobe executable.
/// Absolute file path to the fftool executable.
/// An instance of .
public KeyframeData GetKeyframeData(string filePath, string ffProbePath, string ffToolPath)
{
var extension = Path.GetExtension(filePath.AsSpan());
if (extension.Equals(".mkv", StringComparison.OrdinalIgnoreCase))
{
try
{
return MatroskaKeyframeExtractor.GetKeyframeData(filePath);
}
catch (Exception ex)
{
_logger.LogError(ex, "{ExtractorType} failed to extract keyframes", nameof(MatroskaKeyframeExtractor));
}
}
try
{
return FfToolKeyframeExtractor.GetKeyframeData(ffToolPath, filePath);
}
catch (Exception ex)
{
_logger.LogError(ex, "{ExtractorType} failed to extract keyframes", nameof(FfToolKeyframeExtractor));
}
try
{
return FfProbeKeyframeExtractor.GetKeyframeData(ffProbePath, filePath);
}
catch (Exception ex)
{
_logger.LogError(ex, "{ExtractorType} failed to extract keyframes", nameof(FfProbeKeyframeExtractor));
}
return new KeyframeData(0, Array.Empty());
}
}
}