在 Linux 中从终端静音应用程序

Pra*_*een 5 audio command-line ubuntu

有没有办法从终端静音应用程序?

我知道amixer可以静音一切;但是是否有一个简单的解决方案可以从终端静音给定的应用程序?

Kam*_*ski 3

如果您的 Linux 使用pulseaudio,这就是您所需要的:

\n\n
    \n
  1. 检查输出pacmd list-sink-inputs并找到您想要静音的应用程序;记下它的索引。
  2. \n
  3. 运行pacmd set-sink-input-mute <index> true以使应用程序静音(\xe2\x80\xa6 false取消静音)。
  4. \n
\n\n

示例(一些不相关的行替换为\xe2\x80\xa6):

\n\n
$ pacmd list-sink-inputs\n    index: 0\n\xe2\x80\xa6\n    index: 1\n        driver: <protocol-native.c>\n        flags: START_CORKED FIX_RATE \n        state: RUNNING\n        sink: 0 <alsa_output.pci-0000_00_1b.0.analog-stereo>\n        volume: front-left: 95027 / 145% / 9,68 dB,   front-right: 95027 / 145% / 9,68 dB\n                balance 0,00\n        muted: no\n        current latency: 1952,72 ms\n        requested latency: 40,00 ms\n        sample spec: float32le 2 k 44100 Hz\n        channel map: front-left,front-right\n                     Stereo\n        resample method: copy\n        module: 10\n        client: 8 <VLC media player (LibVLC 2.2.2)>\n        properties:\n                media.role = "video"\n                media.name = "audio stream"\n                application.name = "VLC media player (LibVLC 2.2.2)"\n                native-protocol.peer = "UNIX socket client"\n                native-protocol.version = "30"\n                application.id = "org.VideoLAN.VLC"\n                application.version = "2.2.2"\n                application.icon_name = "vlc"\n                application.language = "pl_PL.UTF-8"\n                application.process.id = "15133"\n\xe2\x80\xa6\n$ pacmd set-sink-input-mute 1 true    # mutes VLC\n$ pacmd set-sink-input-mute 1 false   # unmutes VLC\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

一个 Ask Ubuntu 答案,其中包含以下脚本,允许您通过应用程序名称静音或取消静音:

\n\n
\n
#!/bin/bash\n\nmain() {\n    local action=mute\n    while getopts :hu option; do \n        case "$option" in \n            h) usage 0 ;;\n            u) action=unmute ;;\n            ?) usage 1 "invalid option: -$OPTARG" ;;\n        esac\n    done\n    shift $((OPTIND - 1))\n\n    if [[ "$1" ]]; then\n        $action "$1"\n    else\n        usage 1 "specify an application name" \n    fi\n}\n\nusage() {\n    [[ "$2" ]] && echo "error: $2"\n    echo "usage: $0 [-h] [-u] appname"\n    echo "where: -u = ummute application (default action is to mute)"\n    exit $1\n}\n\nmute()   { adjust_muteness "$1" 1; }\nunmute() { adjust_muteness "$1" 0; }\n\nadjust_muteness() { \n    local index=$(get_index "$1")\n    [[ "$index" ]] && pacmd set-sink-input-mute "$index" $2 >/dev/null \n}\n\nget_index() {\n    local pid=$(pidof "$1")\n    if [[ -z "$pid" ]]; then\n        echo "error: no running processes for: $1" >&2\n    else\n        pacmd list-sink-inputs | \n        awk -v pid=$pid \'\n            $1 == "index:" {idx = $2} \n            $1 == "application.process.id" && $3 == "\\"" pid "\\"" {print idx; exit}\n        \'\n    fi\n}\n\nmain "$@"\n
Run Code Online (Sandbox Code Playgroud)\n
\n\n

如果它对您有用,请为原始答案投票。

\n