11#! /bin/bash
2- # # Author: Nawar
2+ # # Author: Nawar
33# # License: GPL (c) 2014
44# #
55# # Note:
1111# # Volume control added by elpraga
1212# # using the command found at http://forum.kodi.tv/showthread.php?tid=176795
1313
14-
14+
1515# # Configure your KODI RPC details here
1616# set -x
17-
18- KODI_HOST=127.0.0.1
19- KODI_PORT=8080
17+
18+ KODI_HOST=
19+ KODI_PORT=
2020KODI_USER=
2121KODI_PASS=
2222LOCK=false
2323
2424function xbmc_req {
25- output=$( curl -s -i -X POST --header " Content-Type: application/json" -d " $1 " http://$KODI_USER :$KODI_PASS @$KODI_HOST :$KODI_PORT /jsonrpc)
25+ output=$( curl -s -i -X POST --header " Content-Type: application/json" -d " $1 " http://$KODI_USER :$KODI_PASS @$KODI_HOST :$KODI_PORT /jsonrpc)
2626
2727 if [[ $2 = true ]];
2828 then
2929 echo $output
30- fi
30+ fi
3131}
3232
3333function parse_json {
3434 key=$1
35- awk -F" [,:}]" ' {for(i=1;i<=NF;i++){if($i~/' $key ' \042/){print $(i+1)}}}' | tr -d ' "'
35+ awk -F" [,:}]" ' {for(i=1;i<=NF;i++){if($i~/' $key ' \042/){print $(i+1)}}}' | tr -d ' "'
3636}
37-
37+
3838function play_youtube {
3939
4040 REGEX=" ^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*"
41-
41+
4242 ID=$1
43-
43+
4444 if [ " $ID " == " " ];
4545 then
4646 echo " Syntax $0 :$1 <id|url>"
4747 exit
4848 fi
49-
49+
5050 if [[ $ID =~ $REGEX ]]; then
5151 ID=${BASH_REMATCH[7]}
5252 fi
53-
53+
5454 echo -n " Opening video id $ID on $KODI_HOST ..."
5555
5656 # clear the list
@@ -68,22 +68,22 @@ function play_youtube {
6868function queue_yt_videos {
6969
7070 REGEX=" ^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*"
71-
71+
7272 ID=$1
73-
73+
7474 if [ " $ID " == " " ];
7575 then
7676 echo " Syntax $0 :$1 <id|url>"
7777 exit
7878 fi
79-
79+
8080 if [[ $ID =~ $REGEX ]]; then
8181 ID=${BASH_REMATCH[7]}
8282 fi
8383
8484 output=` xbmc_req ' {"jsonrpc": "2.0", "method": "Playlist.GetItems", "params":{"playlistid":1}, "id": 99}' true`
8585 numberitems=` echo $output | parse_json " total" `
86-
86+
8787 echo -n " Video added to the current playlist $ID on $KODI_HOST which has ($numberitems ) items..."
8888 # add the video to the list
8989 xbmc_req ' {"jsonrpc": "2.0", "method": "Playlist.Add", "params":{"playlistid":1, "item" :{ "file" : "plugin://plugin.video.youtube/?action=play_video&videoid=' $ID ' "}}, "id" : 1}' ;
@@ -94,15 +94,15 @@ function queue_yt_videos {
9494function play_pause {
9595 # Get Active players first
9696 output=` xbmc_req ' {"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 99}' true`
97- player_id=` echo $output | parse_json " playerid" `
97+ player_id=` echo $output | parse_json " playerid" `
9898 echo " Pausing/Playing the player with ID => $player_id "
99- xbmc_req ' {"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": ' $player_id ' }, "id": 1}'
99+ xbmc_req ' {"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": ' $player_id ' }, "id": 1}'
100100}
101101
102102function stop {
103103 # Get Active players first
104104 output=` xbmc_req ' {"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 99}' true`
105- player_id=` echo $output | parse_json " playerid" `
105+ player_id=` echo $output | parse_json " playerid" `
106106 echo " Stopping the player with ID => $player_id "
107107 xbmc_req ' {"jsonrpc": "2.0", "method": "Player.Stop", "params": { "playerid": ' $player_id ' }, "id": 1}'
108108}
@@ -118,7 +118,7 @@ function press_key {
118118 CLR=" \e[K"
119119
120120 case " $1 " in
121- A) ACTION=' Up'
121+ A) ACTION=' Up'
122122 echo -ne " \rUp$CLR " ;
123123 ;;
124124 B) ACTION=' Down'
163163 break
164164 elif [[ $key != ' ' ]]
165165 then
166- press_key " $key "
166+ press_key " $key "
167167 fi
168168done
169169}
170170
171171function volume_up {
172172 echo " Incrementing volume"
173- xbmc_req ' { "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "increment" }, "id": 1 }'
173+ xbmc_req ' { "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "increment" }, "id": 1 }'
174174}
175175
176176function volume_down {
177177 echo " Decrementing volume on"
178- xbmc_req ' { "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "decrement" }, "id": 1 }'
178+ xbmc_req ' { "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "decrement" }, "id": 1 }'
179+ }
180+
181+ function handle_volume {
182+ echo " Press up/down for volume adjustment (q to quit): " ;
183+ while :
184+ do
185+ read -s -n1 key
186+ if [[ $key = q ]]; then
187+ printf " \n"
188+ break
189+ elif [[ $key == ' A' ]]; then
190+ printf " \r+ Volume increasing."
191+ xbmc_req ' { "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "increment" }, "id": 1 }'
192+ elif [[ $key == ' B' ]]; then
193+ printf " \r- Volume decreasing."
194+ xbmc_req ' { "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "decrement" }, "id": 1 }'
195+ fi
196+ done
179197}
180198
181199function fullscreen_toggle {
@@ -194,6 +212,7 @@ echo -e "\n kodi-cli -[p|i|h|s|y youtbe URL/ID|t 'text to send']\n\n" \
194212 " -y play youtube video. Use either URL/ID (of video)\n" \
195213 " -q queue youtube video to the current list. Use either URL/ID (of video). Use instead of -y.\n" \
196214 " -o play youtube video directly on Kodi. Use the name of video.\n" \
215+ " -v interactive volume control\n" \
197216 " -i interactive navigation mode. Accept keyboard keys of Up, Down, Left, Right, Back,\n" \
198217 " Context menu and information\n" \
199218 " -t 'text to send'\n" \
@@ -205,44 +224,52 @@ echo -e "\n kodi-cli -[p|i|h|s|y youtbe URL/ID|t 'text to send']\n\n" \
205224}
206225
207226# # Process command line arguments
208- while getopts " yqopstiudfh " opt; do
227+ while getopts " yqopstiudfhv " opt; do
209228 case $opt in
210229 y)
211230 # play youtube video
212- play_youtube $2
231+ play_youtube $2
213232 ;;
214233 q)
215234 # queue youtube video
216- queue_yt_videos $2
235+ queue_yt_videos $2
217236 ;;
218237 o)
219238 # play youtube video directly
220239 # this depends on using mps-youtube
221240 play_youtube ` mpsyt /$2 , i 1, q | grep -i link | awk -F ' v=' ' { print $2 }' `
222241 ;;
223242 p)
224- play_pause
243+ play_pause
225244 ;;
226- s)
245+ s)
227246 stop
228247 ;;
229248 t) send_text $2
230249 handle_keys
231250 ;;
232251 i)
233- handle_keys
252+ handle_keys
234253 ;;
235254 u)
236255 volume_up
237256 ;;
238257 d)
239- volume_down
258+ volume_down
240259 ;;
241260 f)
242- fullscreen_toggle
243- ;;
261+ fullscreen_toggle
262+ ;;
244263 h)
245- show_help
264+ show_help
246265 ;;
266+ v)
267+ handle_volume
268+ ;;
247269 esac
248270done
271+
272+
273+ if [ $# -eq 0 ]; then
274+ show_help
275+ fi
0 commit comments