Change application volume from terminal

Is there a way to change individual application volumes from the terminal? I found a way to change the master volume which is amixer -D pulse sset Master 50% but I would like to be able to change volumes for individual applications like is possible in pavucontrol. My usage would be for scripting.

Asked By: AkBKukU

||

I found the solution hidden in a comment on unix.stackexchange. Use pactl list sink-inputs to find your application’s sink input number. Then use pactl set-sink-input-volume [sink number] [volume percent] to set the volume of your application.

Answered By: AkBKukU

This function easily sets the volume of any app.

function pacmd-set-app-volume() {
  local player="$1"
  local volume="$2"
  firstPlayerSinkIndex="$(pacmd list-sink-inputs | awk '/index:|application.name |application.process.binary / {print $0};' | grep -iB 1 "$player" | awk '/index:/ {print $2; exit}')"  # get specific app sink
  [[ $firstPlayerSinkIndex ]] && pacmd set-sink-input-volume "$firstPlayerSinkIndex" "$((volume*65536/100))" # 100% → 65536
}

Could be added to .bashrc/.zshrc.

Usage:

pacmd-set-app-volume <loosly_app_name> <volume_percentage>

For e.g.:

pacmd-set-app-volume "MPV Media Player" 55  # Or "mpv" "75"
Answered By: Pablo Bianchi
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.