aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.MediaEncoding.Keyframes/KeyframeExtractor.cs
blob: 2ee6b43e614d410ab6cb168136e082e54246b340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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></returns>
        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);
        }
    }
}