cdosea-play/cdoseaplay/utils.py
2023-10-12 11:38:34 +01:00

99 lines
2.6 KiB
Python

import os
import sys
from glob import glob
import datetime
import time
import string
import random
import subprocess
import ox
import mpv
import logging
logger = logging.getLogger('cdosea')
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static')
def mpv_log(loglevel, component, message):
logger.info('[{}] {}: {}'.format(loglevel, component, message))
def get_player(fullscreen=True, font='Menlo', font_size=28):
# 42 max
player = mpv.MPV(
log_handler=mpv_log, input_default_bindings=True,
input_vo_keyboard=True, sub_text_font_size=font_size, sub_text_font=font,
)
#player.observe_property('time-pos', lambda pos: print('Now playing at {:.2f}s'.format(pos)))
player.fullscreen = fullscreen
player.loop = 'inf'
player.loop_file = 'no'
return player
def update_playlist(playlist, prefix, position=None, shift=True):
files = []
videos = {}
if not prefix.endswith('/'):
prefix += '/'
if not shift:
files.append(os.path.join(STATIC_ROOT, 'title.png'))
for letter in string.ascii_uppercase:
videos[letter] = glob('%s%s*.mp4' % (prefix, letter.lower()))
random.shuffle(videos[letter])
for i in range(10):
for letter in string.ascii_uppercase:
if len(videos[letter]) > i:
files.append(videos[letter][i])
else:
print("warning some video files are missing %s%s%02d.1080p.mp4" % (prefix, letter.lower(), i))
if not shift:
black = os.path.normpath(os.path.join(prefix, '..', 'black.mp4'))
if os.path.exists(black):
files.append(black)
if shift:
if position is None:
today = datetime.date.today()
seconds_since_midnight = time.time() - time.mktime(today.timetuple())
offset = 0
position = 0
while offset < seconds_since_midnight:
f = files.pop(0)
try:
offset += ox.avinfo(f)['duration']
files.append(f)
except:
logger.debug('WTF', exc_info=True)
pass
position += 1
else:
pos = position
while pos:
f = files.pop(0)
files.append(f)
pos -= 1
with open(playlist, 'w') as f:
f.write('\n'.join(files))
f.write('\n')
return position, len(files)
def trigger_lights(path):
cmd = ['python3', '-m', 'cdoseaplay.lights', path]
subprocess.Popen(cmd)
def get_path(path):
if isinstance(path, bytes):
path = path.decode()
return path