You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rust调用FFmpeg录制音视频时音频设备找不到的问题

问题排查与解决

核心原因

你在Rust中给FFmpeg传递的音频设备参数多了一层转义引号,导致FFmpeg实际收到的设备名是包含外层双引号的字符串(比如"Microphone (Arctis Nova Pro Wireless)"),但真实的设备名是不带这层引号的,所以匹配失败。

CMD中需要引号是因为Shell会把空格当作参数分隔符,必须用引号把带空格的参数包裹起来;但Rust的Command是直接将每个arg作为独立参数传递给程序,不需要额外添加引号,添加后反而会把引号作为参数内容的一部分。

修复后的代码

let mut ffmpegcommand = Command::new("ffmpeg");
ffmpegcommand.arg("-hide_banner")
    .arg("-y")
    .arg("-f")
    .arg("dshow")
    .arg("-i")
    .arg("audio=Microphone (Arctis Nova Pro Wireless)")  // 去掉转义引号
    .arg("-f")
    .arg("dshow")
    .arg("-i")
    .arg("audio=What U Hear (Sound Blaster Audigy 5/Rx)")  // 同样去掉转义引号
    .arg("-filter_complex")
    .arg("amix=inputs=2")
    .arg("-f")
    .arg("gdigrab")
    .arg("-framerate")
    .arg("60")
    .arg("-show_region")
    .arg("1")
    .arg("-video_size")
    .arg("600x600")
    .arg("-offset_x")
    .arg("100")
    .arg("-offset_y")
    .arg("100")
    .arg("-i")
    .arg("desktop")
    .arg("-strftime")
    .arg("1")
    .arg(r"G:\School\Capstone\screenshotFFMPEG\%Y-%m-%d_%H-%M-%S.mp4");  // 用原始字符串简化路径写法
ffmpegcommand.status().expect("Failed to execute FFmpeg command");

额外验证建议

可以在Rust代码中添加打印命令参数的逻辑,确认传递的参数是否正确:

println!("Command args: {:?}", ffmpegcommand.get_args().collect::<Vec<_>>());

执行后会输出所有参数,你会看到修复后的音频设备参数是"audio=Microphone (Arctis Nova Pro Wireless)"(没有额外的内层引号),和CMD中实际传递的参数一致。

内容的提问来源于stack exchange,提问作者Mypantsrtooobig

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 03:13:21