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