Last active
August 22, 2018 14:00
-
-
Save phenyque/0cbc22077590454cd88dbc0ab39a4bfa to your computer and use it in GitHub Desktop.
Vlc remote control script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding:utf8 -*- | |
""" | |
Code snippet for controlling one or multiple instances of vlc player over the | |
network. | |
To make this work, the respective vlc player instances have to be configured | |
to expose their web interfaces as described here: | |
https://wiki.videolan.org/documentation:modules/http_intf | |
""" | |
import requests | |
def send_command(cmd, ip, port, password): | |
""" | |
Send command `cmd` to the target vlc player | |
cmd: command for the vlc player to perform | |
(see: https://wiki.videolan.org/VLC_HTTP_requests/) | |
you only need to pass the part after the 'status.xml?' | |
ip: ip-address of target player as string | |
port: port number of target player as string | |
password: password set for the player's web interface | |
""" | |
url = 'http://{ip}:{port}/requests/status.xml?command={cmd}' | |
url_send = url.format(ip=ip, port=port, cmd=cmd) | |
# autheticate to the player, user is always left blank | |
return requests.get(url_send, auth=('', password)) | |
def multicast_command(cmd, ips, ports, passwords): | |
""" | |
Send command `cmd` to multiple instances of vlc players on different | |
computers on the local network. | |
""" | |
assert(len(ips) == len(ports) == len(passwords)) | |
for ip, port, pw in zip(ips, ports, passwords): | |
send_command(cmd, ip, port, pw) | |
# examples: | |
password = 'unbelievably-secure-passphrase' | |
ip = '127.0.0.1' | |
port = '8080' | |
# add file /home/user/awesome_music.wav to playlist and start playing | |
send_command('in_play&input=awesome_music.wav', ip, port, password) | |
# pause | |
send_command('pl_pause', ip, port, password) | |
# play again | |
send_command('pl_play' ip, port, password) | |
# stop playing | |
send_command('pl_stop', ip, port password) | |
# multiple instances on different machines | |
# (password is the same everywhere) | |
ips = (ip, '192.168. 21.1', '192.168.21.2') | |
ports = (port, port, 9000) | |
pws = (password, password, password) | |
multicast_command('pl_play', ips, ports, pws) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment