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

在macOS Monterey上实现后台录制系统音频并自动复制文件到剪贴板的方案求助

在macOS Monterey上实现后台录制系统音频并自动复制文件到剪贴板的方案求助

我想在macOS Monterey上实现类似Windows上ShareX的功能:用全局快捷键录制系统音频,录制完成后自动把音频文件复制到剪贴板,全程后台运行。如果可以的话,我尽量不想使用闭源软件来实现。

已解决的部分:录制系统音频

第一个难题是在macOS上录制系统音频,我用BackgroundMusic轻松解决了这个问题。一开始我尝试用BlackHole,但始终没搞明白怎么把系统音频作为输入源的同时还能听到播放的声音,而BackgroundMusic完全没有这个问题。

尝试过的两种方案

方案A:AppleScript + 语音备忘录?

我最初尝试用AppleScript配合语音备忘录来录制音频,参考过一篇教程。但不知道从什么时候起,语音备忘录没法在后台运行了,原来脚本里的set visible of process appName to false和perform action "AXRaise" of window 1 of process appName这两句失效了。我联系过教程作者,他推测可能是系统更新破坏了这个工作流程。如果有人知道怎么在Monterey上修复这个问题,我会非常感激——我完全不知道该怎么调试这个脚本。

附上我用的AppleScript代码:

set appName to "VoiceMemos"

if application appName is running then
    tell application id (id of application appName)
        quit
    end tell
    copy {do shell script "echo $HOME"} to {homeFolder}
    set recordingsFolder to homeFolder & "/Library/Application Support/com.apple.voicememos/Recordings"
    set audioFileName to (do shell script "cd " & quoted form of recordingsFolder & " && ls -ltr -A1 | grep m4a | tail -1")
    set the clipboard to POSIX file (recordingsFolder & "/" & audioFileName)
else
    tell application id (id of application appName) to activate
    tell application "System Events"
        set limit to 10
        set counter to 0
        repeat
            if exists window 1 of process appName then exit repeat
            if (counter > limit) then exit repeat
            set counter to counter + 1
            delay 1
        end repeat
        perform action "AXRaise" of window 1 of process appName
        keystroke "n" using command down
    end tell
end if

方案B:Shell脚本 + FFmpeg?

我还试了另一种方法:用Shell脚本配合FFmpeg(加Automator)。目前还没解决录制完成后把输出文件复制到剪贴板的功能,但看起来是可行的,不过先不说这个——现在的问题是音频质量。

我写的脚本如下,它能在后台启停录制,但测试时大概一半的文件会出现爆音、失真、卡顿或者音频加速的问题:

#!/bin/bash

if ! pgrep -x "ffmpeg" | grep -q "avfoundation"; then
    pkill ffmpeg
fi

output_file="/Users/user/Desktop/a_$(date +'%y%m%d-%H%M%S').wav"

/usr/local/bin/ffmpeg -f avfoundation -i ":0" -t 600 "$output_file"

我尝试调整过以下参数,但都没解决问题:

  • 设置采样率:-ar 44100
  • 设置比特率:-b:a 128k
  • 设置缓冲区大小:-max_delay 1000000
  • 设置采样格式为32位浮点:-sample_fmt fltp(我的Mac本身就是32位浮点格式)

我对FFmpeg参数其实不太懂,所以完全摸不着头绪。有没有明显的解决方案?我觉得问题出在FFmpeg的设置上,因为用其他方法录制时没有质量问题。另外,我列出设备时会出现下面的错误,不知道有没有关联:

user@mac ~ % ffmpeg -hide_banner -f avfoundation -list_devices true -i ""
[AVFoundation indev @ 0x7ff144005cc0] AVFoundation video devices:
[AVFoundation indev @ 0x7ff144005cc0] [0] FaceTime HD Camera
[AVFoundation indev @ 0x7ff144005cc0] [1] Capture screen 0
[AVFoundation indev @ 0x7ff144005cc0] AVFoundation audio devices:
[AVFoundation indev @ 0x7ff144005cc0] [0] Background Music
[AVFoundation indev @ 0x7ff144005cc0] [1] Bose QC35 II
[AVFoundation indev @ 0x7ff144005cc0] [2] Built-in Microphone
[AVFoundation indev @ 0x7ff144005cc0] [3] Background Music (UI Sounds)
[in#0 @ 0x7ff1440057c0] Error opening input: Input/output error
Error opening input file .
Error opening input files: Input/output error

求助

有没有人知道我该怎么实现这个功能?是继续折腾FFmpeg参数,还是有更简单的替代方案?另外,语音备忘录的那个AppleScript还能修复吗?

备注:内容来源于stack exchange,提问作者user0145

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 07:23:06