|
|
@ -68,51 +68,54 @@ public static class FfProbeKeyframeExtractor
|
|
|
|
double streamDuration = 0;
|
|
|
|
double streamDuration = 0;
|
|
|
|
double formatDuration = 0;
|
|
|
|
double formatDuration = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
using (reader)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var line = reader.ReadLine().AsSpan();
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
if (line.IsEmpty)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
var line = reader.ReadLine().AsSpan();
|
|
|
|
}
|
|
|
|
if (line.IsEmpty)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var firstComma = line.IndexOf(',');
|
|
|
|
var firstComma = line.IndexOf(',');
|
|
|
|
var lineType = line[..firstComma];
|
|
|
|
var lineType = line[..firstComma];
|
|
|
|
var rest = line[(firstComma + 1)..];
|
|
|
|
var rest = line[(firstComma + 1)..];
|
|
|
|
if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase))
|
|
|
|
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))
|
|
|
|
// 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_"))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise down to 1 ms when converting double.
|
|
|
|
if (double.TryParse(ptsTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var keyframe))
|
|
|
|
keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond));
|
|
|
|
{
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
streamDuration = streamDurationResult;
|
|
|
|
if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
streamDuration = streamDurationResult;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase))
|
|
|
|
else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
formatDuration = formatDurationResult;
|
|
|
|
if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
formatDuration = formatDurationResult;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Prefer the stream duration as it should be more accurate
|
|
|
|
// Prefer the stream duration as it should be more accurate
|
|
|
|
var duration = streamDuration > 0 ? streamDuration : formatDuration;
|
|
|
|
var duration = streamDuration > 0 ? streamDuration : formatDuration;
|
|
|
|
|
|
|
|
|
|
|
|
return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes);
|
|
|
|
return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|