diff --git a/cdoseaplay/play.py b/cdoseaplay/play.py index 1a356c5..dd61f4c 100755 --- a/cdoseaplay/play.py +++ b/cdoseaplay/play.py @@ -47,11 +47,12 @@ def main(): update_playlist(args.playlist, args.prefix, shift=False) player.loadlist(args.playlist) player.pause = True + sub.load_playlist(args.playlist) player.register_key_binding('r', restart) update_playlist(args.playlist, args.prefix, shift=shift) - sub = SubtitleServer(player, args.playlist) player.loadlist(args.playlist) + sub = SubtitleServer(player, args.playlist) if not shift: player.pause = True diff --git a/cdoseaplay/subtitleserver.py b/cdoseaplay/subtitleserver.py index 2eb38d2..3476c48 100644 --- a/cdoseaplay/subtitleserver.py +++ b/cdoseaplay/subtitleserver.py @@ -33,9 +33,24 @@ class Subtitles(): def __init__(self, player, playlist): self.player = player + self.load_playlist(playlist) + self.player.observe_property('time-pos', self.update) + + def load_playlist(self, playlist): with open(playlist, 'r') as fd: self.playlist = fd.read().strip().split('\n') - self.player.observe_property('time-pos', self.update) + self.path = None + self.next_path = None + if self.player.path: + path = self.player.path.decode() + self.path = path + self.update_next() + self.load_subtitles() + data = {} + data['subtitles'] = {} + data['subtitles'][os.path.basename(self.path)] = self.current + data['subtitles'][os.path.basename(self.next_path)] = self.next + self.trigger(data) def update(self, pos): if pos is None: @@ -70,10 +85,16 @@ class Subtitles(): self.next_path = self.playlist[index] def load_subtitles(self): - srt = self.path.replace('.mp4', '.srt') - self.current = ox.srt.load(srt) - srt = self.next_path.replace('.mp4', '.srt') - self.next = ox.srt.load(srt) + if 'png' in self.path: + self.current = [] + else: + srt = self.path.replace('.mp4', '.srt') + self.current = ox.srt.load(srt) + if 'png' in self.next_path: + self.next = [] + else: + srt = self.next_path.replace('.mp4', '.srt') + self.next = ox.srt.load(srt) def trigger(self, data): logger.debug('trigger %s', data) @@ -188,6 +209,7 @@ class NotFoundHandler(RequestHandler): class SubtitleServer(Thread): + sub = None def __init__(self, player, playlist): Thread.__init__(self) @@ -197,15 +219,21 @@ class SubtitleServer(Thread): self.start() def run(self): - main(self.player, self.playlist) + main(self.player, self.playlist, self) def join(self): IOLoop.instance().stop() return Thread.join(self) + def load_playlist(self, playlist): + if self.sub: + self.sub.load_playlist(playlist) -def main(player, playlist): + +def main(player, playlist, parent=None): sub = Subtitles(player, playlist) + if parent: + parent.sub = sub options = { 'debug': DEBUG, @@ -221,6 +249,8 @@ def main(player, playlist): log_format = '%(asctime)s:%(levelname)s:%(name)s:%(message)s' logging.basicConfig(level=logging.DEBUG, format=log_format) http_server = HTTPServer(Application(handlers, **options)) + if parent: + parent.server = http_server main = IOLoop.instance() def shutdown():