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

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

有没有方法可以从终端使应用程序静音?

我知道amixer这可以使所有内容静音;但是有没有一个简单的解决方案,可以从终端仅使给定的应用程序静音?

答案1

如果你的 Linux 使用pulseaudio,那么这就是你所需要的:

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

示例(一些不相关的行被替换):

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

这个 Ask Ubuntu 答案使用以下脚本,您可以通过应用程序名称将其静音或取消静音:

#!/bin/bash

main() {
    local action=mute
    while getopts :hu option; do 
        case "$option" in 
            h) usage 0 ;;
            u) action=unmute ;;
            ?) usage 1 "invalid option: -$OPTARG" ;;
        esac
    done
    shift $((OPTIND - 1))

    if [[ "$1" ]]; then
        $action "$1"
    else
        usage 1 "specify an application name" 
    fi
}

usage() {
    [[ "$2" ]] && echo "error: $2"
    echo "usage: $0 [-h] [-u] appname"
    echo "where: -u = ummute application (default action is to mute)"
    exit $1
}

mute()   { adjust_muteness "$1" 1; }
unmute() { adjust_muteness "$1" 0; }

adjust_muteness() { 
    local index=$(get_index "$1")
    [[ "$index" ]] && pacmd set-sink-input-mute "$index" $2 >/dev/null 
}

get_index() {
    local pid=$(pidof "$1")
    if [[ -z "$pid" ]]; then
        echo "error: no running processes for: $1" >&2
    else
        pacmd list-sink-inputs | 
        awk -v pid=$pid '
            $1 == "index:" {idx = $2} 
            $1 == "application.process.id" && $3 == "\"" pid "\"" {print idx; exit}
        '
    fi
}

main "$@"

如果对你有用,请点赞原始答案

答案2

或者如果脚本没有用,就使用 pa_control:

https://github.com/rhaas80/pa_volume

相关内容