Files
jellyfin/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs

95 lines
3.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
2022-01-11 23:30:30 +01:00
namespace Jellyfin.MediaEncoding.Keyframes.FfProbe;
/// <summary>
/// FfProbe based keyframe extractor.
/// </summary>
public static class FfProbeKeyframeExtractor
{
2022-01-11 23:30:30 +01:00
private const string DefaultArguments = "-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}\"";
2021-09-23 17:00:39 +02:00
/// <summary>
2022-01-11 23:30:30 +01:00
/// Extracts the keyframes using the ffprobe executable at the specified path.
2021-09-23 17:00:39 +02:00
/// </summary>
2022-01-11 23:30:30 +01:00
/// <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)
{
2022-01-11 23:30:30 +01:00
using var process = new Process
{
2022-01-11 23:30:30 +01:00
StartInfo = new ProcessStartInfo
{
2022-01-11 23:30:30 +01:00
FileName = ffProbePath,
Arguments = string.Format(CultureInfo.InvariantCulture, DefaultArguments, filePath),
2022-01-11 23:30:30 +01:00
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
2022-01-11 23:30:30 +01:00
WindowStyle = ProcessWindowStyle.Hidden,
ErrorDialog = false,
},
EnableRaisingEvents = true
};
2022-01-11 23:30:30 +01:00
process.Start();
2022-01-11 23:30:30 +01:00
return ParseStream(process.StandardOutput);
}
2022-01-11 23:30:30 +01:00
internal static KeyframeData ParseStream(StreamReader reader)
{
var keyframes = new List<long>();
double streamDuration = 0;
double formatDuration = 0;
2022-01-11 23:30:30 +01:00
while (!reader.EndOfStream)
{
var line = reader.ReadLine().AsSpan();
if (line.IsEmpty)
{
2022-01-11 23:30:30 +01:00
continue;
}
2022-01-11 23:30:30 +01:00
var firstComma = line.IndexOf(',');
var lineType = line[..firstComma];
var rest = line[(firstComma + 1)..];
if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase))
{
if (rest.EndsWith(",K_"))
{
2022-01-11 23:30:30 +01:00
// Trim the flags from the packet line. Example line: packet,7169.079000,K_
var keyframe = double.Parse(rest[..^3], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture);
// 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));
}
2022-01-11 23:30:30 +01:00
}
else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase))
{
if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult))
{
2022-01-11 23:30:30 +01:00
streamDuration = streamDurationResult;
}
2022-01-11 23:30:30 +01:00
}
else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase))
{
if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult))
{
2022-01-11 23:30:30 +01:00
formatDuration = formatDurationResult;
}
}
2022-01-11 23:30:30 +01:00
}
2022-01-11 23:30:30 +01:00
// Prefer the stream duration as it should be more accurate
var duration = streamDuration > 0 ? streamDuration : formatDuration;
2022-01-11 23:30:30 +01:00
return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes);
}
}