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.
///
public KeyframeData GetKeyframeData(string filePath, string ffProbePath, string ffToolPath)
{
var extension = Path.GetExtension(filePath);
if (string.Equals(extension, ".mkv", StringComparison.OrdinalIgnoreCase))
{
try
{
return MatroskaKeyframeExtractor.GetKeyframeData(filePath);
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, "{MatroskaKeyframeExtractor} failed to extract keyframes", nameof(MatroskaKeyframeExtractor));
}
}
if (!string.IsNullOrEmpty(ffToolPath))
{
return FfToolKeyframeExtractor.GetKeyframeData(ffToolPath, filePath);
}
return FfProbeKeyframeExtractor.GetKeyframeData(ffProbePath, filePath);
}
}
}