| | 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 | |
|
| 0 | 46 | | return ParseStream(process.StandardOutput); |
| | 47 | | } |
| 0 | 48 | | catch (Exception) |
| 0 | 49 | | { |
| | 50 | | try |
| 0 | 51 | | { |
| 0 | 52 | | if (!process.HasExited) |
| 0 | 53 | | { |
| 0 | 54 | | process.Kill(); |
| 0 | 55 | | } |
| 0 | 56 | | } |
| 0 | 57 | | catch |
| 0 | 58 | | { |
| | 59 | | // We do not care if this fails |
| 0 | 60 | | } |
| | 61 | |
|
| 0 | 62 | | throw; |
| | 63 | | } |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | internal static KeyframeData ParseStream(StreamReader reader) |
| 2 | 67 | | { |
| 2 | 68 | | var keyframes = new List<long>(); |
| 2 | 69 | | double streamDuration = 0; |
| 2 | 70 | | double formatDuration = 0; |
| | 71 | |
|
| 2 | 72 | | using (reader) |
| 2 | 73 | | { |
| 19337 | 74 | | while (!reader.EndOfStream) |
| 19335 | 75 | | { |
| 19335 | 76 | | var line = reader.ReadLine().AsSpan(); |
| 19335 | 77 | | if (line.IsEmpty) |
| 0 | 78 | | { |
| 0 | 79 | | continue; |
| | 80 | | } |
| | 81 | |
|
| 19335 | 82 | | var firstComma = line.IndexOf(','); |
| 19335 | 83 | | var lineType = line[..firstComma]; |
| 19335 | 84 | | var rest = line[(firstComma + 1)..]; |
| 19335 | 85 | | if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase)) |
| 19331 | 86 | | { |
| | 87 | | // Split time and flags from the packet line. Example line: packet,7169.079000,K_ |
| 19331 | 88 | | var secondComma = rest.IndexOf(','); |
| 19331 | 89 | | var ptsTime = rest[..secondComma]; |
| 19331 | 90 | | var flags = rest[(secondComma + 1)..]; |
| 19331 | 91 | | if (flags.StartsWith("K_")) |
| 222 | 92 | | { |
| 222 | 93 | | if (double.TryParse(ptsTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out v |
| 222 | 94 | | { |
| | 95 | | // Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise do |
| 222 | 96 | | keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond)); |
| 222 | 97 | | } |
| 222 | 98 | | } |
| 19331 | 99 | | } |
| 4 | 100 | | else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase)) |
| 2 | 101 | | { |
| 2 | 102 | | if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var stre |
| 1 | 103 | | { |
| 1 | 104 | | streamDuration = streamDurationResult; |
| 1 | 105 | | } |
| 2 | 106 | | } |
| 2 | 107 | | else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase)) |
| 2 | 108 | | { |
| 2 | 109 | | if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var form |
| 2 | 110 | | { |
| 2 | 111 | | formatDuration = formatDurationResult; |
| 2 | 112 | | } |
| 2 | 113 | | } |
| 19335 | 114 | | } |
| | 115 | |
|
| | 116 | | // Prefer the stream duration as it should be more accurate |
| 2 | 117 | | var duration = streamDuration > 0 ? streamDuration : formatDuration; |
| | 118 | |
|
| 2 | 119 | | return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes); |
| | 120 | | } |
| 2 | 121 | | } |
| | 122 | | } |