1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use super::core::Ffmpeg;
use crate::cmd_ffprobe;
impl Ffmpeg {
/// Get keyframe timestamps from a video file
///
/// Extracts the timestamps of all keyframes (I-frames) from the video
/// using ffprobe. Falls back to packet-based analysis if frame-based
/// analysis fails.
///
/// # Arguments
/// * `file_path` - Path to the video file
///
/// # Returns
/// * `Ok(Vec<f64>)` - List of keyframe timestamps in seconds
/// * `Err(String)` - Error message if extraction fails
pub fn get_keyframes(&self, file_path: &str) -> Result<Vec<f64>, String> {
if !std::path::Path::new(file_path).exists() {
return Err(format!("File not found: {}", file_path));
}
let output = cmd_ffprobe()
.args([
"-v",
"error",
"-select_streams",
"v:0",
"-show_entries",
"frame=key_frame,pts_time",
"-of",
"csv=p=0",
file_path,
])
.output()
.map_err(|e| format!("ffprobe failed: {}", e))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("ffprobe error: {}", stderr));
}
let output_str = String::from_utf8_lossy(&output.stdout);
let mut timestamps = Vec::new();
for line in output_str.lines() {
let parts: Vec<&str> = line.split(',').collect();
if parts.len() >= 2 {
let key_frame = parts[0].trim();
let pts_time = parts[1].trim();
if key_frame == "1" {
if let Ok(time) = pts_time.parse::<f64>() {
timestamps.push(time);
}
}
}
}
if timestamps.is_empty() {
return self.get_keyframe_timestamps_from_packets(file_path);
}
if timestamps.len() > 0 {}
Ok(timestamps)
}
/// Get keyframe timestamps from packet analysis
///
/// Alternative method to extract keyframe timestamps by analyzing
/// packet flags instead of frame data. Used as fallback when
/// the primary frame-based method fails.
///
/// # Arguments
/// * `file_path` - Path to the video file
///
/// # Returns
/// * `Ok(Vec<f64>)` - List of keyframe timestamps in seconds
/// * `Err(String)` - Error message if extraction fails
fn get_keyframe_timestamps_from_packets(&self, file_path: &str) -> Result<Vec<f64>, String> {
let output = cmd_ffprobe()
.args([
"-v",
"error",
"-select_streams",
"v:0",
"-show_entries",
"packet=flags,pts_time",
"-of",
"csv=p=0",
file_path,
])
.output()
.map_err(|e| format!("ffprobe failed: {}", e))?;
let output_str = String::from_utf8_lossy(&output.stdout);
let mut timestamps = Vec::new();
for line in output_str.lines() {
let parts: Vec<&str> = line.split(',').collect();
if parts.len() >= 2 {
let flags = parts[0].trim();
let pts_time = parts[1].trim();
if flags.contains('K') {
if let Ok(time) = pts_time.parse::<f64>() {
timestamps.push(time);
}
}
}
}
if timestamps.len() > 0 {}
Ok(timestamps)
}
}