From 8532d88a7153f148888aa2e68542f0d6f3935aa3 Mon Sep 17 00:00:00 2001 From: Andreas Egli Date: Sun, 28 Aug 2022 11:40:51 +0200 Subject: [PATCH 1/4] add TryParse to FFProbe Keyframe extraction --- CONTRIBUTORS.md | 1 + .../FfProbe/FfProbeKeyframeExtractor.cs | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index fb582238df..21c884d24a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -36,6 +36,7 @@ - [dmitrylyzo](https://github.com/dmitrylyzo) - [DMouse10462](https://github.com/DMouse10462) - [DrPandemic](https://github.com/DrPandemic) + - [eglia](https://github.com/eglia) - [EraYaN](https://github.com/EraYaN) - [escabe](https://github.com/escabe) - [excelite](https://github.com/excelite) diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs index 320604e101..60433048be 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs @@ -65,9 +65,11 @@ public static class FfProbeKeyframeExtractor if (rest.EndsWith(",K_")) { // 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)); + if (double.TryParse(rest[..^3], 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)) From 14008fd7d01bfeb90055bad967c4759af38a1594 Mon Sep 17 00:00:00 2001 From: Andreas Egli Date: Sun, 28 Aug 2022 15:53:28 +0200 Subject: [PATCH 2/4] add gentps flag to ffprobe for keyframe extraction --- .../FfProbe/FfProbeKeyframeExtractor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs index 60433048be..d26048874f 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs @@ -11,7 +11,7 @@ namespace Jellyfin.MediaEncoding.Keyframes.FfProbe; /// public static class FfProbeKeyframeExtractor { - 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}\""; + 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}\""; /// /// Extracts the keyframes using the ffprobe executable at the specified path. From 9db0b275ff47a8e593fa48d89ecee7504cda21b0 Mon Sep 17 00:00:00 2001 From: Andreas Egli Date: Sun, 28 Aug 2022 16:14:53 +0200 Subject: [PATCH 3/4] allow additional flags after K_ for ffprobe keyframe extraction --- .../FfProbe/FfProbeKeyframeExtractor.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs index d26048874f..cda3b4e8ed 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs @@ -62,10 +62,13 @@ public static class FfProbeKeyframeExtractor var rest = line[(firstComma + 1)..]; if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase)) { - if (rest.EndsWith(",K_")) + // Split time and flags from the packet line. Example line: packet,7169.079000,K_ + var secondComma = rest.IndexOf(','); + var pts_time = rest[..secondComma]; + var flags = rest[(secondComma + 1)..]; + if (flags.StartsWith("K_")) { - // Trim the flags from the packet line. Example line: packet,7169.079000,K_ - if (double.TryParse(rest[..^3], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var keyframe)) + if (double.TryParse(pts_time, 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)); From 866b4460b1337bf2d8d1b57c230d0709c5e903db Mon Sep 17 00:00:00 2001 From: Andreas Egli Date: Tue, 6 Sep 2022 07:41:27 +0200 Subject: [PATCH 4/4] change variable to camelCase --- .../FfProbe/FfProbeKeyframeExtractor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs index cda3b4e8ed..79aa8a3549 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs @@ -64,11 +64,11 @@ public static class FfProbeKeyframeExtractor { // Split time and flags from the packet line. Example line: packet,7169.079000,K_ var secondComma = rest.IndexOf(','); - var pts_time = rest[..secondComma]; + var ptsTime = rest[..secondComma]; var flags = rest[(secondComma + 1)..]; if (flags.StartsWith("K_")) { - if (double.TryParse(pts_time, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var keyframe)) + 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));