aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs
blob: 479e6ffdc851be9f5bd81621823a86aa5748e645 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;

namespace Jellyfin.MediaEncoding.Keyframes.FfProbe;

/// <summary>
/// FfProbe based keyframe extractor.
/// </summary>
public static class FfProbeKeyframeExtractor
{
    private const string DefaultArguments = "-fflags +genpts -v error -skip_frame nokey -show_entries format=duration -show_entries stream=duration -show_entries packet=pts_time,flags -select_streams v -of csv \"{0}\"";

    /// <summary>
    /// Extracts the keyframes using the ffprobe executable at the specified path.
    /// </summary>
    /// <param name="ffProbePath">The path to the ffprobe executable.</param>
    /// <param name="filePath">The file path.</param>
    /// <returns>An instance of <see cref="KeyframeData"/>.</returns>
    public static KeyframeData GetKeyframeData(string ffProbePath, string filePath)
    {
        using var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = ffProbePath,
                Arguments = string.Format(CultureInfo.InvariantCulture, DefaultArguments, filePath),

                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true,

                WindowStyle = ProcessWindowStyle.Hidden,
                ErrorDialog = false,
            },
            EnableRaisingEvents = true
        };

        try
        {
            process.Start();

            return ParseStream(process.StandardOutput);
        }
        catch (Exception)
        {
            try
            {
                if (!process.HasExited)
                {
                    process.Kill();
                }
            }
            catch
            {
                // We do not care if this fails
            }

            throw;
        }
    }

    internal static KeyframeData ParseStream(StreamReader reader)
    {
        var keyframes = new List<long>();
        double streamDuration = 0;
        double formatDuration = 0;

        using (reader)
        {
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine().AsSpan();
                if (line.IsEmpty)
                {
                    continue;
                }

                var firstComma = line.IndexOf(',');
                var lineType = line[..firstComma];
                var rest = line[(firstComma + 1)..];
                if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase))
                {
                    // Split time and flags from the packet line. Example line: packet,7169.079000,K_
                    var secondComma = rest.IndexOf(',');
                    var ptsTime = rest[..secondComma];
                    var flags = rest[(secondComma + 1)..];
                    if (flags.StartsWith("K_"))
                    {
                        if (double.TryParse(ptsTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var keyframe))
                        {
                            // Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise down to 1 ms when converting double.
                            keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond));
                        }
                    }
                }
                else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase))
                {
                    if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult))
                    {
                        streamDuration = streamDurationResult;
                    }
                }
                else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase))
                {
                    if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult))
                    {
                        formatDuration = formatDurationResult;
                    }
                }
            }

            // Prefer the stream duration as it should be more accurate
            var duration = streamDuration > 0 ? streamDuration : formatDuration;

            return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes);
        }
    }
}