Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(cli): export several recordings in one run

sloemodznaea63ca0d333
diff --git a/apps/cli/src/export.rs b/apps/cli/src/export.rs
index fcf9c40e8..648f08ff9 100644
--- a/apps/cli/src/export.rs
+++ b/apps/cli/src/export.rs
@@ -40,19 +40,24 @@ impl From<QualityArg> for cap_export::mp4::ExportCompression {
}
#[derive(Args)]
-#[command(long_about = "Render a '.cap' project to a video file.
+#[command(long_about = "Render one or more '.cap' projects to video files.
+
+Pass one project for a single export, or several to export them all in one run. With more than
+one project the output must be a directory (--output, or a trailing directory argument), and
+each project is written as '<project name>.<ext>'. Every positional is read as a project; a
+trailing path that cannot be read as one is taken as the output location, as in
+`cap export project.cap out.mp4`.
NOTE: here --format selects the CONTAINER (mp4/gif/mov), NOT the output mode. For machine-readable
output pass --json (the global flag), which streams NDJSON progress + completion events to stdout.
The NDJSON uses PascalCase type tags and snake_case fields ({\"type\":\"Progress\",\"rendered_count\":N,
\"total_frames\":N} then {\"type\":\"Completed\",\"path\":\"...\"}); on failure a final
-{\"type\":\"Error\",\"error\":\"...\"} is emitted.")]
+{\"type\":\"Error\",\"error\":\"...\"} is emitted. Batch runs also emit {\"type\":\"FileStarted\",...} and {\"type\":\"FileFailed\",...} per project plus a closing {\"type\":\"BatchCompleted\",...}.")]
pub struct Export {
- /// Path to a '.cap' project directory (as produced by `cap record`)
- project_path: PathBuf,
- /// Output file (positional alternative to --output)
- output_path: Option<PathBuf>,
- /// Output file to write the export to
+ /// One or more '.cap' project directories (as produced by `cap record`)
+ #[arg(required = true, num_args = 1..)]
+ project_paths: Vec<PathBuf>,
+ /// Output file for a single project, or directory for several
#[arg(long, short = 'o')]
output: Option<PathBuf>,
/// Container to export: mp4 (default), gif, or mov. NOT the output mode — use --json for JSON
@@ -104,7 +109,7 @@ impl ExportFlags {
}
}
-#[derive(Deserialize)]
+#[derive(Clone, Copy, Deserialize)]
#[serde(tag = "format")]
pub enum CliExportSettings {
#[serde(alias = "mp4")]
@@ -137,6 +142,14 @@ impl CliExportSettings {
Self::Mp4(_) | Self::Gif(_) => false,
}
}
+
+ fn extension(&self) -> &'static str {
+ match self {
+ Self::Mp4(_) => "mp4",
+ Self::Gif(_) => "gif",
+ Self::Mov(_) => "mov",
+ }
+ }
}
fn default_fps(format: ExportFormat) -> u32 {
@@ -229,6 +242,19 @@ enum ExportProgressMessage<'a> {
Completed {
path: &'a std::path::Path,
},
+ FileStarted {
+ path: &'a std::path::Path,
+ index: u32,
+ total: u32,
+ },
+ FileFailed {
+ path: &'a std::path::Path,
+ error: &'a str,
+ },
+ BatchCompleted {
+ succeeded: u32,
+ failed: u32,
+ },
// Field is named `error` (not `message`) so a single `"error" in obj` predicate detects failure
// across every JSON-emitting command. The desktop sidecar only parses Progress/Completed, so the
// PascalCase `type` tag stays for back-compat while this terminal field is free to standardize.
@@ -237,18 +263,75 @@ enum ExportProgressMessage<'a> {
},
}
-impl Export {
- fn resolve_output(&self) -> Result<Option<PathBuf>, String> {
- match (&self.output, &self.output_path) {
- (Some(_), Some(_)) => Err(
+/// True when `path` reads as a '.cap' project: it ends in `.cap`, or it is a directory holding a
+/// `recording-meta.json`. Tells a trailing output argument apart from a project argument.
+fn looks_like_project(path: &Path) -> bool {
+ if path
+ .extension()
+ .is_some_and(|extension| extension.eq_ignore_ascii_case("cap"))
+ {
+ return true;
+ }
+ path.join("recording-meta.json").is_file()
+}
+
+/// Split positional arguments into projects plus an optional output. A trailing path that cannot
+/// be read as a project is the output location, but only when --output was not given; passing both
+/// stays an error. With --output present, every positional is a project.
+fn split_projects_and_output(
+ mut paths: Vec<PathBuf>,
+ flag_output: Option<PathBuf>,
+) -> Result<(Vec<PathBuf>, Option<PathBuf>), String> {
+ let trailing_output =
+ paths.len() >= 2 && paths.last().is_some_and(|path| !looks_like_project(path));
+
+ if let Some(output) = flag_output {
+ if trailing_output {
+ return Err(
"Specify the output path either positionally or with --output, not both"
.to_string(),
- ),
- (Some(path), None) | (None, Some(path)) => Ok(Some(path.clone())),
- (None, None) => Ok(None),
+ );
+ }
+ return Ok((paths, Some(output)));
+ }
+
+ let output = if trailing_output { paths.pop() } else { None };
+ Ok((paths, output))
+}
+
+/// `<project name>.<ext>` inside `dir` for one batch entry.
+fn batch_output_name(project: &Path, extension: &str) -> String {
+ let stem = project
+ .file_stem()
+ .and_then(|stem| stem.to_str())
+ .filter(|stem| !stem.is_empty())
+ .unwrap_or("export");
+ format!("{stem}.{extension}")
+}
+
+/// Pair every project with its output inside `dir`, rejecting runs that would write one name twice.
+fn plan_batch_outputs(
+ projects: Vec<PathBuf>,
+ dir: &Path,
+ extension: &str,
+) -> Result<Vec<(PathBuf, PathBuf)>, String> {
+ let mut planned = Vec::with_capacity(projects.len());
+ let mut claimed = std::collections::HashMap::new();
+ for project in projects {
+ let name = batch_output_name(&project, extension);
+ if let Some(previous) = claimed.insert(name.clone(), project.clone()) {
+ return Err(format!(
+ "Two projects would both export to '{name}' ({} and {}); rename one of them or export it separately",
+ previous.display(),
+ project.display()
+ ));
}
+ planned.push((project, dir.join(name)));
}
+ Ok(planned)
+}
+impl Export {
fn resolve_settings(&self) -> Result<CliExportSettings, String> {
let flags = ExportFlags {
format: self.format,
@@ -306,16 +389,129 @@ impl Export {
completion_json: bool,
stdout: &Arc<Mutex<std::io::Stdout>>,
) -> Result<(), String> {
- let output = self.resolve_output()?;
let settings = self.resolve_settings()?;
+ let (projects, output) = split_projects_and_output(self.project_paths, self.output)?;
+
+ if projects.len() == 1 {
+ return Self::export_project(
+ &projects[0],
+ output,
+ settings,
+ self.force_ffmpeg_decoder,
+ progress_json,
+ completion_json,
+ stdout,
+ )
+ .await;
+ }
+
+ let dir = output.ok_or_else(|| {
+ format!(
+ "Exporting {} projects needs an output directory: pass --output <DIR>, or a trailing directory",
+ projects.len()
+ )
+ })?;
+ if dir.exists() && !dir.is_dir() {
+ return Err(format!(
+ "--output {} is a file; exporting several projects needs a directory",
+ dir.display()
+ ));
+ }
+ std::fs::create_dir_all(&dir).map_err(|error| {
+ format!(
+ "Failed to create output directory {}: {error}",
+ dir.display()
+ )
+ })?;
+
+ let planned = plan_batch_outputs(projects, &dir, settings.extension())?;
+ let total = planned.len();
+ let mut succeeded = 0u32;
+ let mut failed = 0u32;
+ let mut first_error: Option<String> = None;
+
+ for (index, (project, destination)) in planned.iter().enumerate() {
+ if progress_json || completion_json {
+ emit_export_message(
+ stdout,
+ &ExportProgressMessage::FileStarted {
+ path: project,
+ index: index as u32 + 1,
+ total: total as u32,
+ },
+ )?;
+ } else {
+ println!("Exporting {}/{}: {}", index + 1, total, project.display());
+ }
+
+ match Self::export_project(
+ project,
+ Some(destination.clone()),
+ settings,
+ self.force_ffmpeg_decoder,
+ progress_json,
+ completion_json,
+ stdout,
+ )
+ .await
+ {
+ Ok(()) => succeeded += 1,
+ Err(error) => {
+ failed += 1;
+ if progress_json || completion_json {
+ emit_export_message(
+ stdout,
+ &ExportProgressMessage::FileFailed {
+ path: project,
+ error: &error,
+ },
+ )?;
+ } else {
+ eprintln!("Failed to export {}: {error}", project.display());
+ }
+ first_error.get_or_insert(error);
+ }
+ }
+ }
- ensure_remuxed(self.project_path.clone()).await?;
- let meta = RecordingMeta::load_for_project(&self.project_path)
+ if progress_json || completion_json {
+ emit_export_message(
+ stdout,
+ &ExportProgressMessage::BatchCompleted { succeeded, failed },
+ )?;
+ } else if failed > 0 {
+ println!("Exported {succeeded} of {total} projects ({failed} failed).");
+ } else {
+ println!("Exported {succeeded} of {total} projects.");
+ }
+
+ if failed > 0 {
+ return Err(match first_error {
+ Some(first) => format!("{failed} of {total} exports failed; first error: {first}"),
+ None => format!("{failed} of {total} exports failed"),
+ });
+ }
+
+ Ok(())
+ }
+
+ /// Export one project. Batch runs pass the planned destination file explicitly.
+ async fn export_project(
+ project_path: &Path,
+ output: Option<PathBuf>,
+ settings: CliExportSettings,
+ force_ffmpeg_decoder: bool,
+ progress_json: bool,
+ completion_json: bool,
+ stdout: &Arc<Mutex<std::io::Stdout>>,
+ ) -> Result<(), String> {
+ ensure_remuxed(project_path.to_path_buf()).await?;
+ let meta = RecordingMeta::load_for_project(project_path)
.map_err(|e| format!("Failed to load recording meta: {e}"))?;
if matches!(&meta.inner, RecordingMetaInner::Instant(_)) {
return export_instant_project(
- self.project_path,
+ project_path.to_path_buf(),
output,
&settings,
progress_json,
@@ -325,8 +521,8 @@ impl Export {
.await;
}
- let force_ffmpeg_decoder = self.force_ffmpeg_decoder || settings.force_ffmpeg_decoder();
- let mut builder = ExporterBase::builder(self.project_path.clone())
+ let force_ffmpeg_decoder = force_ffmpeg_decoder || settings.force_ffmpeg_decoder();
+ let mut builder = ExporterBase::builder(project_path.to_path_buf())
.with_force_ffmpeg_decoder(force_ffmpeg_decoder);
if let Some(output_path) = output {
@@ -1032,4 +1228,80 @@ mod tests {
.is_err()
);
}
+
+ #[test]
+ fn single_project_keeps_output_and_default_forms() {
+ let (projects, output) =
+ split_projects_and_output(vec![PathBuf::from("/tmp/a.cap")], None).unwrap();
+ assert_eq!(projects, vec![PathBuf::from("/tmp/a.cap")]);
+ assert_eq!(output, None);
+
+ let (projects, output) = split_projects_and_output(
+ vec![PathBuf::from("/tmp/a.cap"), PathBuf::from("/tmp/out.mp4")],
+ None,
+ )
+ .unwrap();
+ assert_eq!(projects, vec![PathBuf::from("/tmp/a.cap")]);
+ assert_eq!(output, Some(PathBuf::from("/tmp/out.mp4")));
+ }
+
+ #[test]
+ fn several_projects_are_all_inputs_with_optional_trailing_output() {
+ let (projects, output) = split_projects_and_output(
+ vec![
+ PathBuf::from("/tmp/a.cap"),
+ PathBuf::from("/tmp/b.cap"),
+ PathBuf::from("/tmp/out"),
+ ],
+ None,
+ )
+ .unwrap();
+ assert_eq!(projects.len(), 2);
+ assert_eq!(output, Some(PathBuf::from("/tmp/out")));
+
+ let (projects, output) = split_projects_and_output(
+ vec![PathBuf::from("/tmp/a.cap"), PathBuf::from("/tmp/b.cap")],
+ None,
+ )
+ .unwrap();
+ assert_eq!(projects.len(), 2);
+ assert_eq!(output, None);
+ }
+
+ #[test]
+ fn output_flag_plus_trailing_output_is_rejected() {
+ let result = split_projects_and_output(
+ vec![PathBuf::from("/tmp/a.cap"), PathBuf::from("/tmp/out.mp4")],
+ Some(PathBuf::from("/tmp/out2.mp4")),
+ );
+ assert!(result.is_err());
+ }
+
+ #[test]
+ fn batch_plans_one_output_per_project() {
+ let planned = plan_batch_outputs(
+ vec![
+ PathBuf::from("/tmp/one/Talk.cap"),
+ PathBuf::from("/tmp/two/Demo.cap"),
+ ],
+ Path::new("/tmp/exports"),
+ "mp4",
+ )
+ .unwrap();
+ assert_eq!(planned[0].1, PathBuf::from("/tmp/exports/Talk.mp4"));
+ assert_eq!(planned[1].1, PathBuf::from("/tmp/exports/Demo.mp4"));
+ }
+
+ #[test]
+ fn batch_rejects_duplicate_output_names() {
+ let result = plan_batch_outputs(
+ vec![
+ PathBuf::from("/tmp/one/Talk.cap"),
+ PathBuf::from("/tmp/two/Talk.cap"),
+ ],
+ Path::new("/tmp/exports"),
+ "mp4",
+ );
+ assert!(result.is_err());
+ }
}
diff --git a/apps/cli/src/guide.rs b/apps/cli/src/guide.rs
index 5a99d4622..e20a349fd 100644
--- a/apps/cli/src/guide.rs
+++ b/apps/cli/src/guide.rs
@@ -194,13 +194,20 @@ fn build() -> Guide {
notes: Some(
"EXCEPTION: export NDJSON uses PascalCase `type` tags and snake_case fields \
(rendered_count, total_frames) for desktop compatibility. --format selects the \
- CONTAINER (mp4/gif/mov), NOT output mode; use --json for machine-readable output.",
+ CONTAINER (mp4/gif/mov), NOT output mode; use --json for machine-readable output. Pass several projects to export them in one run; each is written to '<project name>.<ext>' inside the output directory.",
),
..cmd(
"export",
- "Render a .cap project to a video file.",
+ "Render one or more .cap projects to video files.",
OutputMode::Ndjson,
- &["Progress", "Completed", "Error"],
+ &[
+ "Progress",
+ "Completed",
+ "Error",
+ "FileStarted",
+ "FileFailed",
+ "BatchCompleted",
+ ],
)
},
cmd(
diff --git a/apps/cli/src/main.rs b/apps/cli/src/main.rs
index 00f1635d1..ccb682afe 100644
--- a/apps/cli/src/main.rs
+++ b/apps/cli/src/main.rs
@@ -189,7 +189,7 @@ impl CliLogLevel {
#[derive(Subcommand)]
enum Commands {
- /// Export a '.cap' project to a video file
+ /// Export one or more '.cap' projects to video files
Export(Export),
/// Render an export preview frame
ExportPreview(ExportPreview),
diff --git a/apps/cli/tests/cli.rs b/apps/cli/tests/cli.rs
index 14d3b52fb..e6037a168 100644
--- a/apps/cli/tests/cli.rs
+++ b/apps/cli/tests/cli.rs
@@ -436,6 +436,56 @@ fn export_rejects_duplicate_output() {
assert!(!output.status.success());
}
+#[test]
+fn export_several_projects_requires_output_directory() {
+ let output = run(&["export", "/tmp/whatever.cap", "/tmp/also.cap"]);
+ assert!(!output.status.success());
+ assert!(
+ stderr(&output).contains("output directory"),
+ "stderr: {}",
+ stderr(&output)
+ );
+}
+
+#[test]
+fn export_batch_reports_every_failed_project() {
+ let dir = tempfile::tempdir().unwrap();
+ let output = run(&[
+ "export",
+ "/tmp/whatever.cap",
+ "/tmp/also.cap",
+ "-o",
+ dir.path().to_str().unwrap(),
+ ]);
+ assert!(!output.status.success());
+ let text = stderr(&output);
+ assert_eq!(
+ text.matches("Failed to export ").count(),
+ 2,
+ "stderr: {text}"
+ );
+}
+
+#[test]
+fn export_batch_json_streams_file_events_and_summary() {
+ let dir = tempfile::tempdir().unwrap();
+ let output = run(&[
+ "export",
+ "/tmp/whatever.cap",
+ "/tmp/also.cap",
+ "-o",
+ dir.path().to_str().unwrap(),
+ "--json",
+ ]);
+ assert!(!output.status.success());
+ let text = stdout(&output);
+ for event in ["\"FileStarted\"", "\"FileFailed\"", "\"BatchCompleted\""] {
+ assert!(text.contains(event), "stdout missing {event}: {text}");
+ }
+ let last = text.lines().last().unwrap_or_default();
+ assert!(last.contains("\"Error\""), "last line: {last}");
+}
+
#[test]
fn targets_mics_json_is_parseable() {
let output = run(&["targets", "mics", "--format", "json"]);