| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | |
| | | 7 | | namespace Jellyfin.MediaEncoding.Keyframes.FfProbe; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// FfProbe based keyframe extractor. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class FfProbeKeyframeExtractor |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Extracts the keyframes using the ffprobe executable at the specified path. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="ffProbePath">The path to the ffprobe executable.</param> |
| | | 18 | | /// <param name="filePath">The file path.</param> |
| | | 19 | | /// <returns>An instance of <see cref="KeyframeData"/>.</returns> |
| | | 20 | | public static KeyframeData GetKeyframeData(string ffProbePath, string filePath) |
| | 0 | 21 | | { |
| | 0 | 22 | | using var process = new Process |
| | 0 | 23 | | { |
| | 0 | 24 | | StartInfo = new ProcessStartInfo |
| | 0 | 25 | | { |
| | 0 | 26 | | FileName = ffProbePath, |
| | 0 | 27 | | Arguments = string.Format( |
| | 0 | 28 | | CultureInfo.InvariantCulture, |
| | 0 | 29 | | "-fflags +genpts -v error -skip_frame nokey -show_entries format=duration -show_entries stream=durat |
| | 0 | 30 | | filePath), |
| | 0 | 31 | | |
| | 0 | 32 | | CreateNoWindow = true, |
| | 0 | 33 | | UseShellExecute = false, |
| | 0 | 34 | | RedirectStandardOutput = true, |
| | 0 | 35 | | |
| | 0 | 36 | | WindowStyle = ProcessWindowStyle.Hidden, |
| | 0 | 37 | | ErrorDialog = false, |
| | 0 | 38 | | }, |
| | 0 | 39 | | EnableRaisingEvents = true |
| | 0 | 40 | | }; |
| | | 41 | | |
| | | 42 | | try |
| | 0 | 43 | | { |
| | 0 | 44 | | process.Start(); |
| | | 45 | | try |
| | 0 | 46 | | { |
| | 0 | 47 | | process.PriorityClass = ProcessPriorityClass.BelowNormal; |
| | 0 | 48 | | } |
| | 0 | 49 | | catch |
| | 0 | 50 | | { |
| | | 51 | | // We do not care if process priority setting fails |
| | | 52 | | // Ideally log a warning but this does not have a logger available |
| | 0 | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | return ParseStream(process.StandardOutput); |
| | | 56 | | } |
| | 0 | 57 | | catch (Exception) |
| | 0 | 58 | | { |
| | | 59 | | try |
| | 0 | 60 | | { |
| | 0 | 61 | | if (!process.HasExited) |
| | 0 | 62 | | { |
| | 0 | 63 | | process.Kill(); |
| | 0 | 64 | | } |
| | 0 | 65 | | } |
| | 0 | 66 | | catch |
| | 0 | 67 | | { |
| | | 68 | | // We do not care if this fails |
| | 0 | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | throw; |
| | | 72 | | } |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | internal static KeyframeData ParseStream(StreamReader reader) |
| | 2 | 76 | | { |
| | 2 | 77 | | var keyframes = new List<long>(); |
| | 2 | 78 | | double streamDuration = 0; |
| | 2 | 79 | | double formatDuration = 0; |
| | | 80 | | |
| | 2 | 81 | | using (reader) |
| | 2 | 82 | | { |
| | 19337 | 83 | | while (!reader.EndOfStream) |
| | 19335 | 84 | | { |
| | 19335 | 85 | | var line = reader.ReadLine().AsSpan(); |
| | 19335 | 86 | | if (line.IsEmpty) |
| | 0 | 87 | | { |
| | 0 | 88 | | continue; |
| | | 89 | | } |
| | | 90 | | |
| | 19335 | 91 | | var firstComma = line.IndexOf(','); |
| | 19335 | 92 | | var lineType = line[..firstComma]; |
| | 19335 | 93 | | var rest = line[(firstComma + 1)..]; |
| | 19335 | 94 | | if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase)) |
| | 19331 | 95 | | { |
| | | 96 | | // Split time and flags from the packet line. Example line: packet,7169.079000,K_ |
| | 19331 | 97 | | var secondComma = rest.IndexOf(','); |
| | 19331 | 98 | | var ptsTime = rest[..secondComma]; |
| | 19331 | 99 | | var flags = rest[(secondComma + 1)..]; |
| | 19331 | 100 | | if (flags.StartsWith("K_")) |
| | 222 | 101 | | { |
| | 222 | 102 | | if (double.TryParse(ptsTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out v |
| | 222 | 103 | | { |
| | | 104 | | // Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise do |
| | 222 | 105 | | keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond)); |
| | 222 | 106 | | } |
| | 222 | 107 | | } |
| | 19331 | 108 | | } |
| | 4 | 109 | | else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase)) |
| | 2 | 110 | | { |
| | 2 | 111 | | if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var stre |
| | 1 | 112 | | { |
| | 1 | 113 | | streamDuration = streamDurationResult; |
| | 1 | 114 | | } |
| | 2 | 115 | | } |
| | 2 | 116 | | else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase)) |
| | 2 | 117 | | { |
| | 2 | 118 | | if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var form |
| | 2 | 119 | | { |
| | 2 | 120 | | formatDuration = formatDurationResult; |
| | 2 | 121 | | } |
| | 2 | 122 | | } |
| | 19335 | 123 | | } |
| | | 124 | | |
| | | 125 | | // Prefer the stream duration as it should be more accurate |
| | 2 | 126 | | var duration = streamDuration > 0 ? streamDuration : formatDuration; |
| | | 127 | | |
| | 2 | 128 | | return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes); |
| | | 129 | | } |
| | 2 | 130 | | } |
| | | 131 | | } |