Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Run An External Command In Rust

This is a basic example using ffprobe which gets a string back (after chomping off the newline)

Code

use std::process::Command;

fn main() {
    let args: Vec<&str> = vec![
        "-v",
        "error",
        "-select_streams",
        "v:0",
        "-count_packets",
        "-show_entries",
        "stream=nb_read_packets",
        "-of",
        "csv=p=0",
        "some/path.mp4",
    ];
    let cmd_output = Command::new("ffprobe").args(args).output().unwrap();
    let frame_count_string = String::from_utf8(cmd_output.stdout).unwrap();
    let frame_count_str = frame_count_string.as_str().trim();
    dbg!(frame_count_str);
}

Results

[src/main.rs:24] frame_count_str = "33366"