mpv_commander/mpv_commander.py
2025-04-24 15:28:53 -04:00

62 lines
1.5 KiB
Python

import os
import signal
import subprocess
import sqlite3
import time
import json
get_active_video = r'''
SELECT video_url FROM buttons
WHERE is_active = 1
LIMIT 1
'''
def run_mpv(previous_process, mpv_arguments, video_url):
if not previous_process is None:
os.killpg(os.getpgid(previous_process.pid), signal.SIGTERM)
return subprocess.Popen(
"/usr/bin/mpv {arguments} {video_url}".format(
arguments = " ".join(mpv_arguments),
video_url = video_url
),
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell = True,
preexec_fn = os.setsid
)
def get_active_video_url(db):
cursor = db.execute(get_active_video)
return cursor.fetchall()[0][0]
with open("config.json", "r") as config_file:
config_json = json.loads(config_file.read())
current_process = None
current_video_url = None
db = sqlite3.connect("mpvcommander.db")
while True:
if current_process is None:
current_video_url = get_active_video_url(db)
current_process = run_mpv(
current_process,
config_json["mpv_arguments"],
current_video_url
)
active_video_url = get_active_video_url(db)
if current_video_url != active_video_url:
current_video_url = active_video_url
current_process = run_mpv(
current_process,
config_json["mpv_arguments"],
current_video_url
)
time.sleep(1)