Advertisement
alfonz19

formatting "pacmd list-sink-inputs" using jq

Jan 26th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. cat exampleOf2Inputs
  2.  
  3.  
  4. 2 sink input(s) available.
  5. index: 144
  6. driver: <protocol-native.c>
  7. flags:
  8. state: RUNNING
  9. sink: 4 <alsa_output.pci-0000_05_00.0.analog-stereo>
  10. volume: front-left: 15728 / 24% / -37.19 dB, front-right: 15728 / 24% / -37.19 dB
  11. balance 0.00
  12. muted: no
  13. current latency: 70.48 ms
  14. requested latency: 210.00 ms
  15. sample spec: float32le 2ch 44100Hz
  16. channel map: front-left,front-right
  17. Stereo
  18. resample method: copy
  19. module: 13
  20. client: 245 <MPlayer>
  21. properties:
  22. media.name = "UNREAL! Tetris Theme on Violin and Guitar-TnDIRr9C83w.webm"
  23. application.name = "MPlayer"
  24. native-protocol.peer = "UNIX socket client"
  25. native-protocol.version = "32"
  26. application.process.id = "1543"
  27. application.process.user = "mmucha"
  28. application.process.host = "vbDesktop"
  29. application.process.binary = "mplayer"
  30. application.language = "C"
  31. window.x11.display = ":0"
  32. application.process.machine_id = "720184179caa46f0a3ce25156642f7a0"
  33. application.process.session_id = "2"
  34. module-stream-restore.id = "sink-input-by-application-name:MPlayer"
  35. index: 145
  36. driver: <protocol-native.c>
  37. flags:
  38. state: RUNNING
  39. sink: 4 <alsa_output.pci-0000_05_00.0.analog-stereo>
  40. volume: front-left: 24903 / 38% / -25.21 dB, front-right: 24903 / 38% / -25.21 dB
  41. balance 0.00
  42. muted: no
  43. current latency: 70.50 ms
  44. requested latency: 210.00 ms
  45. sample spec: float32le 2ch 48000Hz
  46. channel map: front-left,front-right
  47. Stereo
  48. resample method: speex-float-1
  49. module: 13
  50. client: 251 <MPlayer>
  51. properties:
  52. media.name = "Trombone Shorty At Age 13 - 2nd Line-k9YUi3UhEPQ.webm"
  53. application.name = "MPlayer"
  54. native-protocol.peer = "UNIX socket client"
  55. native-protocol.version = "32"
  56. application.process.id = "2831"
  57. application.process.user = "mmucha"
  58. application.process.host = "vbDesktop"
  59. application.process.binary = "mplayer"
  60. application.language = "C"
  61. window.x11.display = ":0"
  62. application.process.machine_id = "720184179caa46f0a3ce25156642f7a0"
  63. application.process.session_id = "2"
  64. module-stream-restore.id = "sink-input-by-application-name:MPlayer"
  65.  
  66.  
  67.  
  68. cat parse.jq
  69.  
  70.  
  71.  
  72. def parse:
  73. def interpret:
  74. if . == null then .
  75. elif startswith("\"") and endswith("\"")
  76. then .[1:-1]
  77. else tonumber? // .
  78. end;
  79. (capture( "(?<key>[^:= ]*)(: | = )(?<value>.*)" ) // null)
  80. | if . then .value = (.value | interpret) else . end
  81. ;
  82.  
  83. # Construct one object for each "segment"
  84. def construct(s):
  85. [ foreach (s, 0) as $kv (null;
  86. if $kv == 0 or $kv.index
  87. then .complete = .accumulator | .accumulator = $kv
  88. else .complete = null | .accumulator += $kv
  89. end;
  90. .complete // empty ) ]
  91. ;
  92.  
  93.  
  94. construct(inputs | parse | select(.) | {(.key):.value})
  95. | map( {pid: .["application.process.id"],
  96. index,
  97. appname: .["application.name"],
  98. medianame: .["media.name"]} )
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. jq -nR -f parse.jq exampleOf2Inputs
  106. [
  107. {
  108. "pid": null,
  109. "index": 144,
  110. "appname": null,
  111. "medianame": null
  112. },
  113. {
  114. "pid": null,
  115. "index": 145,
  116. "appname": null,
  117. "medianame": null
  118. }
  119. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement