Files
jellyfin-ha-src/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs
T
2021-09-25 10:41:36 +02:00

70 lines
2.5 KiB
C#

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
{
/// <summary>
/// Manager class for the set of keyframe extractors.
/// </summary>
public class KeyframeExtractor
{
private readonly ILogger<KeyframeExtractor> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="KeyframeExtractor"/> class.
/// </summary>
/// <param name="logger">An instance of the <see cref="ILogger{KeyframeExtractor}"/> interface.</param>
public KeyframeExtractor(ILogger<KeyframeExtractor> logger)
{
_logger = logger;
}
/// <summary>
/// Extracts the keyframe positions from a video file.
/// </summary>
/// <param name="filePath">Absolute file path to the media file.</param>
/// <param name="ffProbePath">Absolute file path to the ffprobe executable.</param>
/// <param name="ffToolPath">Absolute file path to the fftool executable.</param>
/// <returns>An instance of <see cref="KeyframeData"/>.</returns>
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<long>());
}
}
}