-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathmain.rs
More file actions
226 lines (194 loc) · 6.55 KB
/
main.rs
File metadata and controls
226 lines (194 loc) · 6.55 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
mod record;
use std::{
io::{Write, stdout},
path::PathBuf,
};
use cap_export::ExporterBase;
use cap_project::XY;
use clap::{Args, Parser, Subcommand};
use record::RecordStart;
use serde_json::json;
use tracing::*;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Export a '.cap' project to an mp4 file
Export(Export),
/// Start a recording or list available capture targets and devices
Record(RecordArgs),
}
#[derive(Args)]
#[command(args_conflicts_with_subcommands = true)]
// #[command(flatten_help = true)]
struct RecordArgs {
#[command(subcommand)]
command: Option<RecordCommands>,
#[command(flatten)]
args: RecordStart,
}
#[derive(Subcommand)]
enum RecordCommands {
/// List screens available for capturing
Screens,
/// List windows available for capturing
Windows,
/// List cameras available for capturing
Cameras,
// Mics,
}
#[tokio::main]
async fn main() -> Result<(), String> {
// let (layer, handle) = tracing_subscriber::reload::Layer::new(None::<DynLoggingLayer>);
let registry = tracing_subscriber::registry().with(tracing_subscriber::filter::filter_fn(
(|v| v.target().starts_with("cap_")) as fn(&tracing::Metadata) -> bool,
));
registry
// .with(layer)
.with(
tracing_subscriber::fmt::layer()
.with_ansi(true)
.with_target(true),
)
.init();
let cli = Cli::parse();
match cli.command {
Commands::Export(e) => {
if let Err(e) = e.run().await {
eprint!("Export failed: {e}")
}
}
Commands::Record(RecordArgs { command, args }) => match command {
Some(RecordCommands::Screens) => {
let screens = cap_recording::screen_capture::list_displays();
for (i, (screen, target)) in screens.iter().enumerate() {
println!(
"
screen {}:
id: {}
name: {}
fps: {}",
i,
screen.id,
screen.name,
target.refresh_rate()
);
}
}
Some(RecordCommands::Windows) => {
let windows = cap_recording::screen_capture::list_windows();
for (i, (window, target)) in windows.iter().enumerate() {
println!(
"
window {}:
id: {}
name: {}
fps: {}",
i,
window.id,
window.name,
target.display().unwrap().refresh_rate()
);
}
}
Some(RecordCommands::Cameras) => {
let cameras = cap_camera::list_cameras().collect::<Vec<_>>();
let mut info = vec![];
for camera_info in cameras {
// let format = RequestedFormat::new::<RgbAFormat>(
// RequestedFormatType::AbsoluteHighestFrameRate,
// );
// let Ok(mut camera) = Camera::new(camera_info.index().clone(), format) else {
// continue;
// };
info.push(json!({
// "model_id": camera_info.model_id().to_string(),
"display_name": camera_info.display_name()
// "index": camera_info.index().to_string(),
// "name": camera_info.human_name(),
// "pixel_format": camera.frame_format(),
// "formats": camera
// .compatible_camera_formats()
// .unwrap()
// .into_iter()
// .map(|f| format!("{}x{}@{}fps", f.resolution().x(), f.resolution().y(), f.frame_rate()))
// .collect::<Vec<_>>()
}));
}
println!("{}", serde_json::to_string_pretty(&info).unwrap());
}
None => {
args.run().await?;
}
},
}
Ok(())
}
#[derive(Args)]
struct Export {
project_path: PathBuf,
output_path: Option<PathBuf>,
}
impl Export {
async fn run(self) -> Result<(), String> {
let exporter_base = ExporterBase::builder(self.project_path)
.build()
.await
.map_err(|v| format!("Exporter build error: {v}"))?;
let mut stdout = stdout();
let exporter_output_path = cap_export::mp4::Mp4ExportSettings {
fps: 60,
resolution_base: XY::new(1920, 1080),
compression: cap_export::mp4::ExportCompression::Maximum,
custom_bpp: None,
force_ffmpeg_decoder: false,
optimize_filesize: false,
}
.export(exporter_base, move |_f| {
// print!("\rrendered frame {f}");
stdout.flush().unwrap();
true
})
.await
.map_err(|v| format!("Exporter error: {v}"))?;
let output_path = if let Some(output_path) = self.output_path {
std::fs::copy(&exporter_output_path, &output_path).unwrap();
output_path
} else {
exporter_output_path
};
info!("Exported video to '{}'", output_path.display());
Ok(())
}
}
// fn ffmpeg_callback_experiment() {
// unsafe {
// unsafe extern "C" fn ffmpeg_log_callback(
// arg1: *mut std::ffi::c_void,
// arg2: std::ffi::c_int,
// arg3: *const std::ffi::c_char,
// arg4: *mut std::ffi::c_char,
// ) {
// // ffmpeg::sys::AVClass;
// if !arg1.is_null() {
// let arg1_ptr = arg1;
// let arg1 = **(arg1 as *mut *mut AVClass);
// dbg!(CStr::from_ptr(arg1.class_name));
// if let Some(item_name_fn) = arg1.item_name {
// dbg!(CStr::from_ptr(item_name_fn(arg1_ptr)));
// }
// }
// // let class_name = if !arg1.is_null() {
// // CStr::from_ptr((*arg1).class_name)
// // } else {
// // "unknown".to_string()
// // };
// // println!("[{class_name}] {arg2} {s:?}",);
// }
// ffmpeg::sys::av_log_set_callback(Some(ffmpeg_log_callback));
// }
// }