executable_mpvctl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env bash
  2. # This script requires:
  3. # - that the directory $HOME/.mpv exist
  4. # - that the program socat be installed
  5. # - that you start mpv with the unix socket feature pointing at that directory
  6. # I recommend an alias in your .bashrc or equivalent file:
  7. # alias mpv="mpv --input-unix-socket=$HOME/.mpv/socket"
  8. socket="$HOME/.mpv/socket"
  9. mpv-ipc()
  10. {
  11. # JSON preamble.
  12. local tosend='{ "command": ['
  13. # adding in the parameters.
  14. for arg in "$@"; do
  15. tosend="$tosend \"$arg\","
  16. done
  17. # closing it up.
  18. tosend=${tosend%?}' ] }'
  19. # send it along and ignore output.
  20. # to print output just remove the redirection to /dev/null
  21. echo "$tosend" | socat - "$socket" &> /dev/null
  22. }
  23. # exit mpv
  24. [ "$1" = "stop" ] && mpv-ipc 'quit'
  25. # toggle play-pause
  26. [ "$1" = "play-pause" ] && mpv-ipc 'cycle' 'pause'
  27. # start playing
  28. [ "$1" = "pause" ] && mpv-ipc 'set' 'pause' 'yes'
  29. # stop playing
  30. [ "$1" = "play" ] && mpv-ipc 'set' 'pause' 'no'
  31. # play next item in playlist
  32. [ "$1" = "next" ] && mpv-ipc 'playlist_next'
  33. # play previous item in playlist
  34. [ "$1" = "previous" ] && mpv-ipc 'playlist_prev'
  35. # add item(s) to playlist
  36. [ "$1" = "add" ] && shift &&
  37. for video in "$@"; do
  38. mpv-ipc 'loadfile' "$video" 'append-play';
  39. done;