Last active
October 22, 2022 00:30
-
-
Save Mic92/1980578a0b5f72b4bad6 to your computer and use it in GitHub Desktop.
start/stop mpv with python
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 python | |
import socket | |
import subprocess | |
import os, sys | |
MPV_SOCKET = "/tmp/mpvsocket" | |
CIDER = "cider.hq.c3d2.de" | |
IS_PYTHON2 = sys.version_info < (3, 0) | |
if IS_PYTHON2: | |
QUIT_CMD = '{"command": ["quit"]}\n' | |
else: | |
QUIT_CMD = b'{"command": ["quit"]}\n' | |
def stop_playback(): | |
if os.path.exists(MPV_SOCKET): | |
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
try: | |
client.connect(MPV_SOCKET) | |
client.send(QUIT_CMD) | |
client.close() | |
except socket.error as e: | |
return "already stopped or error while sending quit: %s" % e | |
else: | |
return "no mpv running" | |
def playback(uri): | |
stop_playback() | |
subprocess.Popen(["mpv", "--ao", "pulse:"+CIDER, "--no-config", "--input-unix-socket="+MPV_SOCKET, "--", uri]) | |
if __name__ == "__main__": | |
playback("http://soundcloud.com/oliverschories/oliver-koletzki-b2b-oliver-schories-pleinvrees-utrecht-20-02-2015") | |
import time | |
time.sleep(10) | |
stop_playback() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment