Skip to content
Merged
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
7 changes: 4 additions & 3 deletions crates/rustjay-io/src/hap_encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub fn pix_fmt_has_alpha(pix_fmt: &str) -> bool {

/// Convert any video to HAP using ffmpeg for *decode* and hap-qt for *encode*.
///
/// Both `ffmpeg` and `ffprobe` must be on PATH; a missing one is the error.
/// Needs `ffmpeg` and `ffprobe`, beside the executable or on PATH (see
/// [`crate::ffmpeg_tool`]); a missing one is the error.
///
/// ffmpeg decodes to raw RGBA via stdout; we feed that into `HapFrameEncoder`
/// plus `QtHapWriter`. The codec comes from the source's pixel format, via
Expand All @@ -44,7 +45,7 @@ pub fn ffmpeg_to_hap(src: &std::path::Path, dst: &std::path::Path) -> anyhow::Re
// --- probe: width, height, fps, pixel format via ffprobe ---------------
// key=value output, not positional CSV: ffprobe emits fields in its own
// order, not the order they were requested, so positions are not stable.
let probe = Command::new("ffprobe")
let probe = Command::new(crate::ffmpeg_tool("ffprobe"))
.args([
"-v", "error",
"-select_streams", "v:0",
Expand Down Expand Up @@ -81,7 +82,7 @@ pub fn ffmpeg_to_hap(src: &std::path::Path, dst: &std::path::Path) -> anyhow::Re
);

// --- decode: ffmpeg → raw RGBA on stdout ------------------------------
let mut child = Command::new("ffmpeg")
let mut child = Command::new(crate::ffmpeg_tool("ffmpeg"))
.args([
"-y", "-i", src.to_str().unwrap_or_default(),
"-f", "rawvideo", "-pix_fmt", "rgba", "-an", "-",
Expand Down
32 changes: 32 additions & 0 deletions crates/rustjay-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,37 @@ pub use input::{SyphonInputReceiver, SyphonDiscovery};
pub use input::{SpoutDiscovery, SpoutInputReceiver};
pub use output::recorder::{list_audio_devices, Recorder, RecorderCodec};
pub use output::OutputManager;

/// An FFmpeg command-line tool (`"ffmpeg"`, `"ffprobe"`) to run: the copy
/// shipped next to the running executable when there is one, so a packaged app
/// records and converts with nothing installed; otherwise the bare name, found
/// on `PATH`.
pub fn ffmpeg_tool(name: &str) -> std::path::PathBuf {
let exe = std::env::current_exe().ok();
tool_beside(exe.as_deref().and_then(std::path::Path::parent), name)
}

fn tool_beside(dir: Option<&std::path::Path>, name: &str) -> std::path::PathBuf {
let file = format!("{name}{}", std::env::consts::EXE_SUFFIX);
dir.map(|d| d.join(&file))
.filter(|p| p.is_file())
.unwrap_or_else(|| file.into())
}

#[cfg(target_os = "linux")]
pub use v4l2_devices::{V4l2DeviceInfo, list_output_devices};

#[cfg(test)]
mod ffmpeg_tool_tests {
#[test]
fn a_bundled_copy_wins_over_path() {
let dir = std::env::temp_dir().join(format!("rustjay-ffmpeg-tool-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = format!("ffmpeg{}", std::env::consts::EXE_SUFFIX);
// Nothing beside the executable: fall back to the bare name on PATH.
assert_eq!(super::tool_beside(Some(&dir), "ffmpeg"), std::path::PathBuf::from(&file));
std::fs::write(dir.join(&file), b"").unwrap();
assert_eq!(super::tool_beside(Some(&dir), "ffmpeg"), dir.join(&file));
std::fs::remove_dir_all(&dir).ok();
}
}
8 changes: 4 additions & 4 deletions crates/rustjay-io/src/output/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Recorder {
path
};

let mut cmd = Command::new("ffmpeg");
let mut cmd = Command::new(crate::ffmpeg_tool("ffmpeg"));
cmd.arg("-y") // overwrite
.arg("-hide_banner")
.arg("-loglevel")
Expand Down Expand Up @@ -357,7 +357,7 @@ impl Recorder {

/// Length of a media file in seconds, or 0 if ffprobe can't say.
fn duration(path: &Path) -> f64 {
Command::new("ffprobe")
Command::new(crate::ffmpeg_tool("ffprobe"))
.args(["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0"])
.arg(path)
.output()
Expand All @@ -380,7 +380,7 @@ fn mux(video: &Path, audio: &Path, out: &Path) -> anyhow::Result<()> {
// Capture opens before the first frame arrives, so the audio runs long at
// the head — both were stopped together, so the excess is exactly the lead.
let lead = duration(audio) - duration(video);
let mut cmd = Command::new("ffmpeg");
let mut cmd = Command::new(crate::ffmpeg_tool("ffmpeg"));
cmd.args(["-y", "-hide_banner", "-loglevel", "error"]);
if lead > 0.02 {
log::info!("[Recorder] trimming {:.2}s of audio lead-in", lead);
Expand Down Expand Up @@ -465,7 +465,7 @@ impl AudioCapture {
(config.sample_rate(), config.channels(), config.sample_format());

let path = sidecar(near, "audio", "m4a");
let mut child = Command::new("ffmpeg")
let mut child = Command::new(crate::ffmpeg_tool("ffmpeg"))
.args(["-y", "-hide_banner", "-loglevel", "error", "-f", "f32le", "-ar"])
.arg(rate.to_string())
.arg("-ac")
Expand Down
Loading