Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Look up pulseaudio sink-input index by property

Tags:

pulseaudio

The output of both pactl list sink-inputs and pacmd list-sink-inputs contains a Properties section:

Properties:
    media.name = "ALSA Playback"
    application.name = "ALSA plug-in [snapclient]"
    native-protocol.peer = "UNIX socket client"
    native-protocol.version = "29"
    application.process.id = "6393"
    application.process.user = "root"
    application.process.host = "xxxxxx"
    application.process.binary = "snapclient"
    application.language = "C"
    application.process.machine_id = "8dadf95c2f504864bc0f8b3ab149cbe0"
    application.process.session_id = "c4"
    module-stream-restore.id = "sink-input-by-application-name:ALSA plug-in [snapclient]"

I am wondering if there is a way to directly look up the index of a sink-input by either the application.process.id or application.process.binary, without resorting to parsing the many lines of output of the aforementioned commands or writing a separate C program.

like image 485
Jakob Hansen Avatar asked Sep 28 '16 01:09

Jakob Hansen


2 Answers

Some commands also accept the unique name instead of the id, but the ones you are trying to use seem to not be able to do so.. probably because the name is not unique and there could be mulitple matches. You need to parse it yourself. This is what I came up with:

pacmd list-sink-inputs |
tr '\n' '\r' |
perl -pe 's/.*? *index: ([0-9]+).+?application\.name = "([^\r]+)"\r.+?(?=index:|$)/\2:\1\r/g' |
tr '\r' '\n'

perl -pe is like sed, just better. This basically matches [anything] [index]: [id] [anything] [application.name] = [name] [anything] and formats the output to something like

"speech-dispatcher":166
"SoX":407
"Clementine":413

Which you then can grep or sed.

Maybe you want to adjust application\.name to something more suitable to you.

like image 160
phil294 Avatar answered Jan 02 '23 12:01

phil294


Although it does parse the output and doesn't yet do any matching for the id you may be looking for, this method will provide a way, with some modifications, to get that information by sink id:

pactl list sink-inputs | while read -r line ; do

  #echo "Processing ${line}"
  echo $line | grep -oP 'Sink Input #\K[^$]'
  echo $line | grep -oP 'application.process.id = "\K[^"]+'
  echo $line | grep -oP 'application.process.binary = "\K[^"]+'

done
like image 28
Greywood Avatar answered Jan 02 '23 13:01

Greywood