Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ notification, and open-editor actions are desktop-only and are skipped on the CL
rules with `cap automations list --json`.

Run `cap --help` or `cap <command> --help` for full flag documentation.

## Window-only recording (Focus)

`cap record start --window <id> --window-only --detach --json` captures the selected
window's content using the existing Focus capture engine on macOS and Windows.
Without `--window-only`, `--window` retains its existing screen-region behavior.
The option requires `--window` and is unavailable on Linux. Studio supports a
separate camera track; Instant with `--window-only` rejects `--camera` because that
combination would fall back to screen-region capture. Stop detached recordings
with `cap record stop --id <recordingId> --json`.

Focus does not guarantee smooth background WebGL rendering or complete input or
privacy isolation. Keyboard/cursor controls, application-specific audio, browser
throttling, and MCP recording tools are separate concerns.
14 changes: 14 additions & 0 deletions apps/cli/skill/cap/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,17 @@ To generate a short, cinematic 3D product-demo video from a URL, use the install
`cap-demo` skill (installed alongside this one). It scouts the page, records it with
virtual input, and treats it with Cap's 3D camera, a brand-matched background, and
music. Currently macOS-only.

## Window-only recording (Focus)

`cap record start --window <id> --window-only --detach --json` captures the selected
window's content using the existing Focus capture engine on macOS and Windows.
Without `--window-only`, `--window` retains its existing screen-region behavior.
The option requires `--window` and is unavailable on Linux. Studio supports a
separate camera track; Instant with `--window-only` rejects `--camera` because that
combination would fall back to screen-region capture. Stop detached recordings
with `cap record stop --id <recordingId> --json`.

Focus does not guarantee smooth background WebGL rendering or complete input or
privacy isolation. Keyboard/cursor controls, application-specific audio, browser
throttling, and MCP recording tools are separate concerns.
2 changes: 1 addition & 1 deletion apps/cli/src/guide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn build() -> Guide {
CommandDoc {
requires_duration: false,
notes: Some(
"The `started` then `stopped` sequence applies to the foreground run. With `--detach` the stream emits only `started` (or `error`) and returns immediately with recordingId+pid; the `stopped` event is delivered by `cap record stop`.",
"--window <id> captures a screen region by default. Add --window-only for Focus (window content only) on macOS/Windows; requires --window, and rejects --mode instant with --camera. Studio can record a separate camera track. Focus does not guarantee background rendering or input/privacy isolation. The `started` then `stopped` sequence applies to the foreground run. With `--detach` the stream emits only `started` (or `error`) and returns immediately with recordingId+pid; the `stopped` event is delivered by `cap record stop`.",
),
..cmd(
"record start",
Expand Down
184 changes: 180 additions & 4 deletions apps/cli/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ use crate::{
pub struct RecordParams {
#[command(flatten)]
target: RecordTargets,
#[arg(
long,
requires = "window",
conflicts_with = "screen",
help = "Capture only the window content (Focus) on macOS/Windows, instead of its screen region; Instant mode cannot combine this with --camera"
)]
window_only: bool,
/// Recording mode to use
#[arg(long, value_enum, default_value_t = RecordMode::Studio)]
mode: RecordMode,
Expand Down Expand Up @@ -66,6 +73,17 @@ pub struct RecordParams {

impl RecordParams {
fn validate(&self) -> Result<(), String> {
if self.window_only {
if self.target.window.is_none() || self.target.screen.is_some() {
return Err("--window-only requires --window and cannot use --screen".to_string());
}
if !cfg!(any(target_os = "macos", windows)) {
return Err("--window-only is supported only on macOS and Windows".to_string());
}
if self.mode == RecordMode::Instant && self.camera.is_some() {
return Err("--window-only cannot combine --mode instant with --camera; use Studio mode or omit --camera".to_string());
}
}
if self.duration.is_some_and(|duration| {
// `> u64::MAX` would panic in `Duration::from_secs_f64` (used by `wait_for_stop`).
!duration.is_finite() || duration <= 0.0 || duration > u64::MAX as f64
Expand All @@ -88,6 +106,9 @@ impl RecordParams {
args.push("--window".to_string());
args.push(id.to_string());
}
if self.window_only {
args.push("--window-only".to_string());
}
args.push("--mode".to_string());
args.push(self.mode.to_string());
if let Some(camera) = &self.camera {
Expand Down Expand Up @@ -422,6 +443,7 @@ impl SessionRunArgs {
}

async fn session_worker(params: RecordParams, recording_id: &str) -> Result<(), String> {
params.validate()?;
let target = resolve_target(&params)?;
let path = params
.path
Expand Down Expand Up @@ -756,9 +778,11 @@ async fn start_recording(
let target_for_shareable_content = target.clone();
let camera_only_target = matches!(target, ScreenCaptureTarget::CameraOnly);
let mut studio_builder = studio_recording::Actor::builder(path.clone(), target.clone())
.with_system_audio(params.system_audio);
.with_system_audio(params.system_audio)
.with_window_only(params.window_only);
let mut instant_builder = instant_recording::Actor::builder(path.clone(), target)
.with_system_audio(params.system_audio);
.with_system_audio(params.system_audio)
.with_window_only(params.window_only);
let mut camera_active = false;
let camera_recorded =
!(cfg!(target_os = "macos") && params.mode == RecordMode::Instant && !camera_only_target);
Expand Down Expand Up @@ -1281,8 +1305,11 @@ struct RecordTargets {
/// ID of the screen to capture
#[arg(long, group = "target")]
screen: Option<DisplayId>,
/// ID of the window to capture
#[arg(long, group = "target")]
#[arg(
long,
group = "target",
help = "ID of the window to capture (screen region by default; use --window-only for Focus)"
)]
window: Option<WindowId>,
}

Expand Down Expand Up @@ -1339,6 +1366,153 @@ fn emit_record_event(format: OutputFormat, event: &RecordEvent<'_>) -> Result<()
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;

fn parse_record(args: &[&str]) -> RecordParams {
let cli = crate::Cli::try_parse_from(args).unwrap();
let Some(crate::Commands::Record(record)) = cli.command else {
panic!("expected record command");
};
match record.command {
Some(crate::RecordCommands::Start(start)) => start.params,
Some(crate::RecordCommands::SessionRun(worker)) => worker.params,
None => record.args.params,
_ => panic!("expected recording parameters"),
}
}

#[test]
fn window_only_requires_a_window_target() {
for args in [
vec!["cap", "record", "start", "--window-only"],
vec!["cap", "record", "start", "--screen", "1", "--window-only"],
vec![
"cap",
"record",
"start",
"--screen",
"1",
"--window",
"247",
"--window-only",
],
] {
assert!(crate::Cli::try_parse_from(&args).is_err(), "{args:?}");
}
}

#[test]
fn window_only_validates_mode_camera_and_platform() {
for mode in ["studio", "instant"] {
for camera in [false, true] {
let mut args = vec![
"cap",
"record",
"start",
"--window",
"247",
"--window-only",
"--mode",
mode,
];
if camera {
args.extend(["--camera", "synthetic-camera"]);
}
let params = parse_record(&args);
let supported = cfg!(any(target_os = "macos", windows));
assert_eq!(
params.validate().is_ok(),
supported && !(mode == "instant" && camera)
);
let mut default_params = params.clone();
default_params.window_only = false;
assert!(default_params.validate().is_ok());
let mut missing_target = params;
missing_target.target.window = None;
assert!(
missing_target
.validate()
.unwrap_err()
.contains("requires --window")
);
}
}
}

#[tokio::test]
async fn window_only_rejects_instant_camera_before_creating_a_project() {
let root = tempfile::tempdir().unwrap();
let path = root.path().join("unused.cap");
let mut params = parse_record(&[
"cap",
"record",
"start",
"--window",
"247",
"--window-only",
"--mode",
"instant",
"--camera",
"synthetic-camera",
]);
params.path = Some(path.clone());
for detach in [false, true] {
let start = RecordStart {
params: params.clone(),
detach,
format: OutputFormat::Json,
};
let error = start.run_inner(OutputFormat::Json).await.unwrap_err();
assert!(error.contains("--window-only"));
assert!(!path.exists());
}
let error = session_worker(params, "synthetic-session")
.await
.unwrap_err();
assert!(error.contains("--window-only"));
assert!(!path.exists());
}

#[test]
fn window_only_survives_detached_worker_round_trip() {
for mode in ["studio", "instant"] {
for enabled in [false, true] {
let mut args = vec![
"cap",
"record",
"start",
"--window",
"247",
"--mode",
mode,
"--detach",
"--duration",
"2",
"--fps",
"30",
];
if enabled {
args.push("--window-only");
}
let params = parse_record(&args);
let serialized = params.to_cli_args();
assert_eq!(serialized.iter().any(|arg| arg == "--window-only"), enabled);
let mut worker_args = vec![
"cap",
"record",
"__session-run",
"--session-id",
"synthetic-session",
];
worker_args.extend(serialized.iter().map(String::as_str));
let worker = parse_record(&worker_args);
assert_eq!(worker.window_only, enabled);
assert_eq!(worker.to_cli_args(), serialized);
assert_eq!(worker.validate(), params.validate());
}
}
assert!(parse_record(&["cap", "record", "--window", "247", "--window-only"]).window_only);
}

#[cfg(unix)]
#[tokio::test]
Expand All @@ -1354,6 +1528,7 @@ mod tests {
screen: None,
window: None,
},
window_only: false,
mode: RecordMode::Studio,
camera: None,
mic: None,
Expand Down Expand Up @@ -1387,6 +1562,7 @@ mod tests {
screen: None,
window: None,
},
window_only: false,
mode,
camera: None,
mic: Some(format!("missing-microphone-{}", Uuid::new_v4())),
Expand Down
11 changes: 10 additions & 1 deletion crates/recording/src/instant_recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,16 @@ mod tests {
let id: scap_targets::WindowId = "247".parse().unwrap();
let window = ScreenCaptureTarget::Window { id: id.clone() };

assert_eq!(instant_capture_window(&window, true, false), Some(id));
let builder = ActorBuilder::new(PathBuf::new(), window.clone());
assert_eq!(
instant_capture_window(&window, builder.window_only, false),
None
);
let builder = builder.with_window_only(true);
assert_eq!(
instant_capture_window(&window, builder.window_only, false),
Some(id)
);
assert_eq!(instant_capture_window(&window, true, true), None);
assert_eq!(instant_capture_window(&window, false, false), None);
assert_eq!(
Expand Down
23 changes: 23 additions & 0 deletions crates/recording/src/studio_recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4311,6 +4311,29 @@ mod tests {
ChannelVideoSourceConfig, Muxer, SetupCtx, TaskPool, VideoFrame, VideoMuxer,
};

#[test]
fn studio_window_only_builder_selects_isolated_capture() {
let id: scap_targets::WindowId = "247".parse().unwrap();
let target = screen_capture::ScreenCaptureTarget::Window { id: id.clone() };
let builder = ActorBuilder::new(PathBuf::new(), target.clone());
assert_eq!(
crate::window_only_target(&target, builder.window_only),
None
);
let builder = builder.with_window_only(true);
assert_eq!(
crate::window_only_target(&target, builder.window_only),
Some(id)
);
assert_eq!(
crate::window_only_target(
&screen_capture::ScreenCaptureTarget::CameraOnly,
builder.window_only
),
None
);
}

/// A start that fails after the metadata was written leaves the recording
/// `Failed` with its error instead of `InProgress` forever (#3948).
#[cfg(not(target_os = "linux"))]
Expand Down
Loading