-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathproject_recordings.rs
181 lines (158 loc) · 5.89 KB
/
project_recordings.rs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::path::{Path, PathBuf};
use crate::RecordingMeta;
use cap_project::StudioRecordingMeta;
use serde::Serialize;
use specta::Type;
#[derive(Debug, Clone, Copy, Serialize, Type)]
pub struct Video {
pub duration: f64,
pub width: u32,
pub height: u32,
pub fps: u32,
}
impl Video {
pub fn new(path: impl AsRef<Path>) -> Result<Self, String> {
fn inner(path: &Path) -> Result<Video, String> {
let input =
ffmpeg::format::input(path).map_err(|e| format!("Failed to open video: {}", e))?;
let stream = input
.streams()
.best(ffmpeg::media::Type::Video)
.ok_or_else(|| "No video stream found".to_string())?;
let video_decoder = ffmpeg::codec::Context::from_parameters(stream.parameters())
.map_err(|e| format!("Failed to create decoder: {}", e))?
.decoder()
.video()
.map_err(|e| format!("Failed to get video decoder: {}", e))?;
let rate = stream.avg_frame_rate();
let fps = rate.numerator() as f64 / rate.denominator() as f64;
Ok(Video {
width: video_decoder.width(),
height: video_decoder.height(),
duration: input.duration() as f64 / 1_000_000.0,
fps: fps.round() as u32,
})
}
inner(path.as_ref())
}
pub fn fps(&self) -> u32 {
self.fps
}
}
#[derive(Debug, Clone, Copy, Serialize, Type)]
pub struct Audio {
pub duration: f64,
pub sample_rate: u32,
pub channels: u16,
}
impl Audio {
pub fn new(path: impl AsRef<Path>) -> Result<Self, String> {
fn inner(path: &Path) -> Result<Audio, String> {
let input =
ffmpeg::format::input(path).map_err(|e| format!("Failed to open audio: {}", e))?;
let stream = input
.streams()
.best(ffmpeg::media::Type::Audio)
.ok_or_else(|| "No audio stream found".to_string())?;
let audio_decoder = ffmpeg::codec::Context::from_parameters(stream.parameters())
.map_err(|e| format!("Failed to create decoder: {}", e))?
.decoder()
.audio()
.map_err(|e| format!("Failed to get audio decoder: {}", e))?;
Ok(Audio {
duration: input.duration() as f64 / 1_000_000.0,
sample_rate: audio_decoder.rate(),
channels: audio_decoder.channels(),
})
}
inner(path.as_ref())
}
}
#[derive(Debug, Clone, Serialize, Type)]
pub struct ProjectRecordings {
pub segments: Vec<SegmentRecordings>,
}
impl ProjectRecordings {
pub fn new(recording_path: &PathBuf, meta: &StudioRecordingMeta) -> Self {
let segments = match &meta {
StudioRecordingMeta::SingleSegment { segment } => {
let display = Video::new(segment.display.path.to_path(recording_path))
.expect("Failed to read display video");
let camera = segment.camera.as_ref().map(|camera| {
Video::new(&camera.path.to_path(recording_path))
.expect("Failed to read camera video")
});
let audio = segment
.audio
.as_ref()
.map(|audio| Audio::new(audio.path.to_path(recording_path)))
.transpose()
.expect("Failed to read audio");
vec![SegmentRecordings {
display,
camera,
audio,
system_audio: None,
}]
}
StudioRecordingMeta::MultipleSegments { inner } => inner
.segments
.iter()
.map(|s| {
let display = Video::new(s.display.path.to_path(recording_path))
.expect("Failed to read display video");
let camera = s.camera.as_ref().map(|camera| {
Video::new(&camera.path.to_path(recording_path))
.expect("Failed to read camera video")
});
let audio = s
.audio
.as_ref()
.map(|audio| Audio::new(audio.path.to_path(recording_path)))
.transpose()
.expect("Failed to read audio");
let system_audio = s
.system_audio
.as_ref()
.map(|audio| Audio::new(audio.path.to_path(recording_path)))
.transpose()
.expect("Failed to read audio");
SegmentRecordings {
display,
camera,
audio,
system_audio,
}
})
.collect(),
};
Self { segments }
}
pub fn duration(&self) -> f64 {
self.segments.iter().map(|s| s.duration()).sum()
}
pub fn get_source_duration(&self, path: &PathBuf) -> Result<f64, String> {
Video::new(path).map(|v| v.duration)
}
}
#[derive(Debug, Clone, Serialize, Type)]
pub struct SegmentRecordings {
pub display: Video,
pub camera: Option<Video>,
pub audio: Option<Audio>,
pub system_audio: Option<Audio>,
}
impl SegmentRecordings {
pub fn duration(&self) -> f64 {
let mut duration_ns = [
Some(self.display.duration),
self.camera.as_ref().map(|s| s.duration),
self.audio.as_ref().map(|s| s.duration),
]
.into_iter()
.flatten()
.collect::<Vec<_>>();
duration_ns.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
duration_ns[0]
}
}