cdosea-play/cdoseaplay/utils.py

95 lines
2.5 KiB
Python
Raw Normal View History

2017-08-30 14:07:50 +00:00
import os
import sys
from glob import glob
import datetime
import time
import string
import random
2017-10-16 18:31:44 +00:00
import subprocess
2017-08-30 14:07:50 +00:00
import ox
import mpv
2017-08-30 14:07:50 +00:00
import logging
2017-08-30 14:07:50 +00:00
logger = logging.getLogger('cdosea')
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static')
2017-08-30 14:07:50 +00:00
def mpv_log(loglevel, component, message):
logger.info('[{}] {}: {}'.format(loglevel, component, message))
2018-07-31 07:37:42 +00:00
def get_player(fullscreen=True, font='Menlo', font_size=28):
2017-08-30 14:07:50 +00:00
# 42 max
player = mpv.MPV(
log_handler=mpv_log, input_default_bindings=True,
2018-07-31 07:37:42 +00:00
input_vo_keyboard=True, sub_text_font_size=font_size, sub_text_font=font,
2017-08-30 14:07:50 +00:00
)
#player.observe_property('time-pos', lambda pos: print('Now playing at {:.2f}s'.format(pos)))
player.fullscreen = fullscreen
player.loop = 'inf'
2017-10-04 11:02:13 +00:00
player.loop_file = 'no'
2017-08-30 14:07:50 +00:00
return player
def update_playlist(playlist, prefix, position=None, shift=True):
2017-08-30 14:07:50 +00:00
files = []
videos = {}
if not prefix.endswith('/'):
prefix += '/'
if not shift:
files.append(os.path.join(STATIC_ROOT, 'title.png'))
2017-10-27 20:47:45 +00:00
2017-08-30 14:07:50 +00:00
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.mp4" % prefix, letter.lower(), i)
2017-08-30 14:07:50 +00:00
2017-10-27 20:47:45 +00:00
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
2017-08-30 14:07:50 +00:00
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)
2017-08-30 14:07:50 +00:00
files.append(f)
pos -= 1
2017-08-30 14:07:50 +00:00
with open(playlist, 'w') as f:
f.write('\n'.join(files))
f.write('\n')
return position, len(files)
2017-10-16 18:31:44 +00:00
def trigger_lights(path):
cmd = ['python3', '-m', 'cdoseaplay.lights', path]
subprocess.Popen(cmd)