refactor
This commit is contained in:
parent
4c3a6a8c2d
commit
7fec0d2dae
5 changed files with 139 additions and 115 deletions
27
cdosea-play
27
cdosea-play
|
@ -3,19 +3,22 @@ if [ "x$1" == "xupdate" ]; then
|
|||
shift
|
||||
exec python3 -m cdoseaplay.update $@
|
||||
else
|
||||
shift
|
||||
gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend false
|
||||
gsettings set org.gnome.desktop.screensaver lock-enabled false
|
||||
gsettings set org.gnome.desktop.session idle-delay 0
|
||||
gsettings set org.gnome.settings-daemon.plugins.power idle-dim false
|
||||
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
|
||||
gsettings set org.gnome.desktop.background color-shading-type 'solid'
|
||||
gsettings set org.gnome.desktop.background primary-color '#888888'
|
||||
gsettings set org.gnome.desktop.background secondary-color '#888888'
|
||||
gsettings set org.gnome.desktop.background picture-options 'none'
|
||||
if [ "x$1" == "xautostart" ]; then
|
||||
gsettings set org.gnome.desktop.screensaver ubuntu-lock-on-suspend false
|
||||
gsettings set org.gnome.desktop.screensaver lock-enabled false
|
||||
gsettings set org.gnome.desktop.session idle-delay 0
|
||||
gsettings set org.gnome.settings-daemon.plugins.power idle-dim false
|
||||
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
|
||||
gsettings set org.gnome.desktop.background color-shading-type 'solid'
|
||||
gsettings set org.gnome.desktop.background primary-color '#888888'
|
||||
gsettings set org.gnome.desktop.background secondary-color '#888888'
|
||||
gsettings set org.gnome.desktop.background picture-options 'none'
|
||||
shift
|
||||
fi
|
||||
if [ "x$1" == "xsync" ]; then
|
||||
exec python3 -m cdoseaplay.sync $@
|
||||
shift
|
||||
exec python3 -m cdoseaplay.sync "$@"
|
||||
else
|
||||
exec python3 -m cdoseaplay.play $@
|
||||
exec python3 -m cdoseaplay.play "$@"
|
||||
fi
|
||||
fi
|
||||
|
|
8
cdosea-play.desktop
Normal file
8
cdosea-play.desktop
Normal file
|
@ -0,0 +1,8 @@
|
|||
[Desktop Entry]
|
||||
Type=Application
|
||||
Exec=cdosea-play autostart
|
||||
Hidden=false
|
||||
NoDisplay=false
|
||||
X-GNOME-Autostart-enabled=true
|
||||
Name=cdosea-play
|
||||
Comment=
|
|
@ -1,26 +1,15 @@
|
|||
#!/usr/bin/python3
|
||||
import argparse
|
||||
from glob import glob
|
||||
import os
|
||||
import sys
|
||||
from glob import glob
|
||||
import datetime
|
||||
import time
|
||||
import string
|
||||
import random
|
||||
|
||||
import ox
|
||||
from . import mpv
|
||||
from .utils import update_playlist, get_player
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('cdosea')
|
||||
|
||||
def my_log(loglevel, component, message):
|
||||
print('[{}] {}: {}'.format(loglevel, component, message))
|
||||
|
||||
|
||||
base = os.path.dirname(os.path.abspath(__file__))
|
||||
os.chdir(base)
|
||||
player = mpv.MPV(log_handler=my_log, input_default_bindings=True, input_vo_keyboard=True)
|
||||
#player.observe_property('time-pos', lambda pos: print('Now playing at {:.2f}s'.format(pos)))
|
||||
player.fullscreen = True
|
||||
player.loop = 'inf'
|
||||
player = None
|
||||
|
||||
def q_binding(*args):
|
||||
global player
|
||||
|
@ -28,41 +17,38 @@ def q_binding(*args):
|
|||
del player
|
||||
|
||||
|
||||
player.register_key_binding('q', q_binding)
|
||||
def main():
|
||||
global player
|
||||
playlist = os.path.expanduser('~/Videos/cdosea.m3u')
|
||||
prefix = os.path.expanduser('~/Videos/CDOSEA')
|
||||
|
||||
playlist = os.path.expanduser('~/Videos/cdosea.m3u')
|
||||
prefix = os.path.expanduser('~/Videos/CDOSEA')
|
||||
parser = argparse.ArgumentParser(description='play 2 screens in sync')
|
||||
parser.add_argument('--playlist', help='play.m3u', default=playlist)
|
||||
parser.add_argument('--prefix', help='video location', default=prefix)
|
||||
parser.add_argument('--window', action='store_true', help='run in window', default=False)
|
||||
parser.add_argument('--debug', action='store_true', help='debug', default=False)
|
||||
args = parser.parse_args()
|
||||
|
||||
today = datetime.date.today()
|
||||
seconds_since_midnight = time.time() - time.mktime(today.timetuple())
|
||||
DEBUG = args.debug
|
||||
if DEBUG:
|
||||
log_format = '%(asctime)s:%(levelname)s:%(name)s:%(message)s'
|
||||
logging.basicConfig(level=logging.DEBUG, format=log_format)
|
||||
base = os.path.dirname(os.path.abspath(__file__))
|
||||
os.chdir(base)
|
||||
|
||||
files = []
|
||||
videos = {}
|
||||
for letter in string.ascii_uppercase:
|
||||
videos[letter] = glob('%s%s*.mp4' % (prefix, letter.lower()))
|
||||
random.shuffle(videos[letter])
|
||||
player = get_player()
|
||||
player.register_key_binding('q', q_binding)
|
||||
|
||||
for i in range(10):
|
||||
for letter in string.ascii_uppercase:
|
||||
files.append(videos[letter][i])
|
||||
update_playlist(args.playlist, args.prefix)
|
||||
player.loadlist(args.playlist)
|
||||
|
||||
position = 0
|
||||
while position < seconds_since_midnight:
|
||||
f = files.pop(0)
|
||||
try:
|
||||
position += ox.avinfo(f)['duration']
|
||||
files.append(f)
|
||||
except:
|
||||
pass
|
||||
while True:
|
||||
try:
|
||||
player.wait_for_playback()
|
||||
except:
|
||||
sys.exit()
|
||||
del player
|
||||
|
||||
with open(playlist, 'w') as f:
|
||||
f.write('\n'.join(files))
|
||||
|
||||
player.loadlist(playlist)
|
||||
while True:
|
||||
try:
|
||||
player.wait_for_playback()
|
||||
except:
|
||||
sys.exit()
|
||||
del player
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -13,8 +13,7 @@ from queue import Queue
|
|||
from socketserver import UDPServer, ThreadingMixIn, BaseRequestHandler
|
||||
from threading import Thread
|
||||
|
||||
import ox
|
||||
import mpv
|
||||
from .utils import mpv_log, update_playlist
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('cdosea')
|
||||
|
@ -22,45 +21,6 @@ logger = logging.getLogger('cdosea')
|
|||
DEFAULT_PORT = 2680
|
||||
DEBUG = False
|
||||
|
||||
def update_playlist(playlist, prefix='video/', position=None):
|
||||
playlist = os.path.expanduser('~/Videos/cdosea.m3u')
|
||||
prefix = os.path.expanduser('~/Videos/CDOSEA')
|
||||
|
||||
files = []
|
||||
videos = {}
|
||||
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:
|
||||
files.append(videos[letter][i])
|
||||
|
||||
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:
|
||||
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]
|
||||
|
@ -99,9 +59,10 @@ class Player(Thread):
|
|||
current = ''
|
||||
playlist_items = 260
|
||||
|
||||
def __init__(self, peer, player, playlist, lights=False):
|
||||
def __init__(self, peer, player, playlist, prefix, lights=False):
|
||||
self.peer = peer
|
||||
self.playlist = playlist
|
||||
self.prefix = prefix
|
||||
self.player = player
|
||||
self.lights = lights
|
||||
self.queue = Queue()
|
||||
|
@ -111,7 +72,7 @@ class Player(Thread):
|
|||
|
||||
def run(self):
|
||||
logger.debug("update playlist")
|
||||
self.position, self.playlist_items = update_playlist(self.playlist, position=self.get_position())
|
||||
self.position, self.playlist_items = update_playlist(self.playlist, self.prefix, position=self.get_position())
|
||||
logger.debug("load playlist")
|
||||
self.player.loadlist(self.playlist)
|
||||
self.player.pause = True
|
||||
|
@ -160,14 +121,15 @@ class Player(Thread):
|
|||
return pos
|
||||
|
||||
|
||||
def my_log(loglevel, component, message):
|
||||
logger.debug('[{}] {}: {}'.format(loglevel, component, message))
|
||||
|
||||
def main():
|
||||
playlist = os.path.expanduser('~/Videos/cdosea.m3u')
|
||||
prefix = os.path.expanduser('~/Videos/CDOSEA')
|
||||
|
||||
parser = argparse.ArgumentParser(description='play 2 screens in sync')
|
||||
parser.add_argument('--peer', help='ip[:port] of peer', required=True)
|
||||
parser.add_argument('--port', type=int, help='local port', default=DEFAULT_PORT)
|
||||
parser.add_argument('--playlist', help='play.m3u', default='play.m3u')
|
||||
parser.add_argument('--playlist', help='play.m3u', default=playlist)
|
||||
parser.add_argument('--prefix', help='video location', default=prefix)
|
||||
parser.add_argument('--window', action='store_true', help='run in window', default=False)
|
||||
parser.add_argument('--debug', action='store_true', help='debug', default=False)
|
||||
parser.add_argument('--lights', action='store_true', help='lights', default=False)
|
||||
|
@ -188,16 +150,10 @@ def main():
|
|||
if args.lights:
|
||||
time.sleep(5)
|
||||
|
||||
# 42 max
|
||||
player = mpv.MPV(
|
||||
log_handler=my_log, input_default_bindings=True,
|
||||
input_vo_keyboard=True, sub_text_font_size=28, sub_text_font='Menlo'
|
||||
)
|
||||
player.fullscreen = not args.window
|
||||
player.loop = 'inf'
|
||||
player = get_player(fullscreen=not args.window)
|
||||
|
||||
server = ThreadingUDPServer(('0.0.0.0', args.port), Handler)
|
||||
server.player = Player(peer, player, args.playlist, args.lights)
|
||||
server.player = Player(peer, player, args.playlist, args.prefix, args.lights)
|
||||
player.register_key_binding('q', server.q_binding)
|
||||
|
||||
logger.debug("listen on %s...", args.port)
|
||||
|
|
71
cdoseaplay/utils.py
Normal file
71
cdoseaplay/utils.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
import os
|
||||
import sys
|
||||
from glob import glob
|
||||
import datetime
|
||||
import time
|
||||
import string
|
||||
import random
|
||||
|
||||
import ox
|
||||
|
||||
from . import mpv
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger('cdosea')
|
||||
|
||||
def mpv_log(loglevel, component, message):
|
||||
logger.info('[{}] {}: {}'.format(loglevel, component, message))
|
||||
|
||||
def get_player(fullscreen=True):
|
||||
# 42 max
|
||||
player = mpv.MPV(
|
||||
log_handler=mpv_log, input_default_bindings=True,
|
||||
input_vo_keyboard=True, sub_text_font_size=28, sub_text_font='Menlo'
|
||||
)
|
||||
#player.observe_property('time-pos', lambda pos: print('Now playing at {:.2f}s'.format(pos)))
|
||||
player.fullscreen = fullscreen
|
||||
player.loop = 'inf'
|
||||
|
||||
return player
|
||||
|
||||
def update_playlist(playlist, prefix, position=None):
|
||||
|
||||
files = []
|
||||
videos = {}
|
||||
if not prefix.endswith('/'):
|
||||
prefix += '/'
|
||||
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:
|
||||
files.append(videos[letter][i])
|
||||
|
||||
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)
|
||||
|
Loading…
Reference in a new issue