Muting an application from terminal in Linux

Solution 1:

If your Linux uses pulseaudio, this is what you need:

  1. Examine the output of pacmd list-sink-inputs and find the application you want to mute; note its index.
  2. Run pacmd set-sink-input-mute <index> true to mute the application (… false to unmute).

Example (some irrelevant lines replaced by ):

$ 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

There is this Ask Ubuntu answer with the following script that allows you to mute or unmute an application by its name:

#!/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 "$@"

If it works for you then please upvote the original answer.