how can I find out if a video is playing in kodi or not?
#1
I am running kodi on a linux computer and I am looking for a way to close the kodi application as soon as someone clicks on the stop button in kodi.

I though that a good way would be to run a bash script in loop, which will check every second if a video is playing in kodi, and if a video is not playing it will run sudo systemctl stop kodi.service

how can I find out if a video is playing in kodi or not?

thanks
Reply
#2
In case you want to use a bash script, I would execute something via JSON and "jq". Maybe you have to install jq first according to the distro you are using. 

By using: curl -s -X POST http://127.0.0.1:8080/jsonrpc -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":0}'

you will get a result like: {"id":0,"jsonrpc":"2.0","result":[{"playerid":1,"playertype":"internal","type":"video"}]}

That's a bit hard to parse and probably a bit meh if you want to store something in a variable to check against. That's where "jq" comes in place. By executing the same command and pipe it through "jq" like:
curl -s -X POST http://127.0.0.1:8080/jsonrpc -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":0}' | jq .

you will receive something like: 

Code:
{
  "id": 0,
  "jsonrpc": "2.0",
  "result": [
    {
      "playerid": 1,
      "playertype": "internal",
      "type": "video"
    }
  ]
}

And the benefit is, that you are able to access each section of that like for example: "type": "video" via:

curl -s -X POST http://127.0.0.1:8080/jsonrpc -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":0}' | jq .result[].type

response: "video"

To remove the quotes, simply use the "-r" option for jq:
curl -s -X POST http://127.0.0.1:8080/jsonrpc -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":0}' | jq -r .result[].type

response: video


So, what you get is an active player with the type video. I guess that's what you need Wink

a simple one liner and the result stored in a variable should be enough: 

Code:
player=$(curl -s -X POST http://127.0.0.1:8080/jsonrpc -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":0}' | jq -r .result[].type)
if [[ $player = "" ]]; then echo "nothing is playing"; elif [[ $player == "video" ]]; then echo "a video is playing"; elif [[ $player = "audio" ]]; then echo "a song is playing"; fi
Reply
#3
thank you for the information, it was very helpful!
Reply

Logout Mark Read Team Forum Stats Members Help
how can I find out if a video is playing in kodi or not?0