class MediaOmxplayerPlugin(MediaPlugin): """ Plugin to control video and media playback using OMXPlayer. Requires: * **omxplayer** installed on your system (see your distro instructions) * **omxplayer-wrapper** (``pip install omxplayer-wrapper``) """ def __init__(self, args=None, *argv, timeout: float = 20., **kwargs): """ :param args: Arguments that will be passed to the OMXPlayer constructor (e.g. subtitles, volume, start position, window size etc.) see https://github.com/popcornmix/omxplayer#synopsis and https://python-omxplayer-wrapper.readthedocs.io/en/latest/omxplayer/#omxplayer.player.OMXPlayer :type args: list :param timeout: How long the plugin should wait for a video to start upon play request (default: 20 seconds). """ super().__init__(*argv, **kwargs) if args is None: args = [] self.args = args self.timeout = timeout self._player = None self._handlers = {e.value: [] for e in PlayerEvent} self._play_started = threading.Event() @action def play(self, resource=None, subtitles=None, *args, **kwargs): """ Play or resume playing a resource. :param resource: Resource to play. Supported types: * Local files (format: ``file://<path>/<file>``) * Remote videos (format: ``https://<url>/<resource>``) * YouTube videos (format: ``https://www.youtube.com/watch?v=<id>``) * Torrents (format: Magnet links, Torrent URLs or local Torrent files) :param subtitles: Subtitles file """ if not resource: if not self._player: self.logger.warning('No OMXPlayer instances running') else: self._player.play() return self.status() else: self._play_started.clear() self._post_event(MediaPlayRequestEvent, resource=resource) if subtitles: args += ('--subtitles', subtitles) resource = self._get_resource(resource) if self._player: try: self._player.stop() self._player = None except Exception as e: self.logger.exception(e) self.logger.warning( 'Unable to stop a previously running instance ' + 'of OMXPlayer, trying to play anyway') from dbus import DBusException try: from omxplayer import OMXPlayer self._player = OMXPlayer(resource, args=self.args) except DBusException as e: self.logger.warning( 'DBus connection failed: you will probably not ' + 'be able to control the media') self.logger.exception(e) self._post_event(MediaPlayEvent, resource=resource) self._init_player_handlers() if not self._play_started.wait(timeout=self.timeout): self.logger.warning( f'The player has not sent a play started event within {self.timeout}' ) return self.status() @action def pause(self): """ Pause the playback """ if self._player: self._player.play_pause() return self.status() @action def stop(self): """ Stop the playback (same as quit) """ return self.quit() @action def quit(self): """ Quit the player """ from omxplayer.player import OMXPlayerDeadError if self._player: try: try: self._player.stop() except Exception as e: self.logger.warning(f'Could not stop player: {str(e)}') self._player.quit() except OMXPlayerDeadError: pass finally: self._player = None return {'status': 'stop'} def get_volume(self) -> float: """ :return: The player volume in percentage [0, 100]. """ if self._player: return self._player.volume() * 100 @action def voldown(self, step=10.0): """ Decrease the volume. :param step: Volume decrease step between 0 and 100 (default: 10%). :type step: float """ if self._player: self.set_volume(max(0, self.get_volume() - step)) return self.status() @action def volup(self, step=10.0): """ Increase the volume. :param step: Volume increase step between 0 and 100 (default: 10%). :type step: float """ if self._player: self.set_volume(min(100, self.get_volume() + step)) return self.status() @action def back(self, offset=30): """ Back by (default: 30) seconds """ if self._player: self._player.seek(-offset) return self.status() @action def forward(self, offset=30): """ Forward by (default: 30) seconds """ if self._player: self._player.seek(offset) return self.status() @action def next(self): """ Play the next track/video """ if self._player: self._player.stop() if self._videos_queue: video = self._videos_queue.pop(0) self.play(video) return self.status() @action def hide_subtitles(self): """ Hide the subtitles """ if self._player: self._player.hide_subtitles() return self.status() @action def hide_video(self): """ Hide the video """ if self._player: self._player.hide_video() return self.status() @action def is_playing(self): """ :returns: True if it's playing, False otherwise """ return self._player.is_playing() @action def load(self, resource, pause=False, **kwargs): """ Load a resource/video in the player. :param resource: URL or filename to load :type resource: str :param pause: If set, load the video in paused mode (default: False) :type pause: bool """ if self._player: self._player.load(resource, pause=pause) return self.status() @action def metadata(self): """ Get the metadata of the current video """ if self._player: return self._player.metadata() return self.status() @action def mute(self): """ Mute the player """ if self._player: self._player.mute() return self.status() @action def unmute(self): """ Unmute the player """ if self._player: self._player.unmute() return self.status() @action def seek(self, position): """ Seek to the specified number of seconds from the start. :param position: Number of seconds from the start :type position: float """ if self._player: self._player.set_position(position) return self.status() @action def set_position(self, position): """ Seek to the specified number of seconds from the start (same as :meth:`.seek`). :param position: Number of seconds from the start :type position: float """ return self.seek(position) @action def set_volume(self, volume): """ Set the volume :param volume: Volume value between 0 and 100 :type volume: float """ if self._player: self._player.set_volume(volume / 100) return self.status() @action def status(self): """ Get the current player state. :returns: A dictionary containing the current state. Format:: output = { "duration": Duration in seconds, "filename": Media filename, "fullscreen": true or false, "mute": true or false, "path": Media path "pause": true or false, "position": Position in seconds "seekable": true or false "state": play, pause or stop "title": Media title "url": Media url "volume": Volume between 0 and 100 "volume_max": 100, } """ from omxplayer.player import OMXPlayerDeadError from dbus import DBusException if not self._player: return {'state': PlayerState.STOP.value} try: state = self._player.playback_status().lower() except (OMXPlayerDeadError, DBusException) as e: self.logger.warning(f'Could not retrieve player status: {e}') if isinstance(e, OMXPlayerDeadError): self._player = None return {'state': PlayerState.STOP.value} if state == 'playing': state = PlayerState.PLAY.value elif state == 'stopped': state = PlayerState.STOP.value elif state == 'paused': state = PlayerState.PAUSE.value return { 'duration': self._player.duration(), 'filename': urllib.parse.unquote(self._player.get_source()).split('/')[-1] if self._player.get_source().startswith('file://') else None, 'fullscreen': self._player.fullscreen(), 'mute': self._player._is_muted, 'path': self._player.get_source(), 'pause': state == PlayerState.PAUSE.value, 'position': max(0, self._player.position()), 'seekable': self._player.can_seek(), 'state': state, 'title': urllib.parse.unquote(self._player.get_source()).split('/')[-1] if self._player.get_source().startswith('file://') else None, 'url': self._player.get_source(), 'volume': self.get_volume(), 'volume_max': 100, } def add_handler(self, event_type, callback): if event_type not in self._handlers.keys(): raise AttributeError( '{} is not a valid PlayerEvent type'.format(event_type)) self._handlers[event_type].append(callback) @staticmethod def _post_event(evt_type, **evt): bus = get_bus() bus.post(evt_type(player='local', plugin='media.omxplayer', **evt)) def on_play(self): def _f(player): if self.volume and not self._play_started.is_set(): self.set_volume(self.volume) self._play_started.set() resource = player.get_source() self._post_event(MediaPlayEvent, resource=resource) for callback in self._handlers[PlayerEvent.PLAY.value]: callback(resource) return _f def on_pause(self): def _f(player): resource = player.get_source() self._post_event(MediaPauseEvent, resource=resource) for callback in self._handlers[PlayerEvent.PAUSE.value]: callback(resource) return _f def on_stop(self): def _f(*_, **__): self._post_event(MediaStopEvent) for callback in self._handlers[PlayerEvent.STOP.value]: callback() return _f def on_seek(self): def _f(player, *_, **__): self._post_event(MediaSeekEvent, position=player.position()) return _f def _init_player_handlers(self): if not self._player: return self._player.playEvent += self.on_play() self._player.pauseEvent += self.on_pause() self._player.stopEvent += self.on_stop() self._player.exitEvent += self.on_stop() self._player.positionEvent += self.on_seek() self._player.seekEvent += self.on_seek() def toggle_subtitles(self, *args, **kwargs): raise NotImplementedError def set_subtitles(self, filename, *args, **kwargs): raise NotImplementedError def remove_subtitles(self, *args, **kwargs): raise NotImplementedError
class VideoOmxplayerPlugin(Plugin): video_extensions = { '.avi', '.flv', '.wmv', '.mov', '.mp4', '.m4v', '.mpg', '.mpeg', '.rm', '.swf', '.vob' } default_torrent_ports = [6881, 6891] torrent_state = {} def __init__(self, args=[], media_dirs=[], download_dir=None, torrent_ports=[], *argv, **kwargs): super().__init__(*argv, **kwargs) self.args = args self.media_dirs = set( filter( lambda _: os.path.isdir(_), map(lambda _: os.path.abspath(os.path.expanduser(_)), media_dirs))) if download_dir: self.download_dir = os.path.abspath( os.path.expanduser(download_dir)) if not os.path.isdir(self.download_dir): raise RuntimeError( 'download_dir [{}] is not a valid directory'.format( self.download_dir)) self.media_dirs.add(self.download_dir) self.player = None self.videos_queue = [] self.torrent_ports = torrent_ports if torrent_ports else self.default_torrent_ports def play(self, resource): if resource.startswith('youtube:') \ or resource.startswith('https://www.youtube.com/watch?v='): resource = self._get_youtube_content(resource) elif resource.startswith('magnet:?'): response = self.download_torrent(resource) resources = response.output if resources: self.videos_queue = resources resource = self.videos_queue.pop(0) else: error = 'Unable to download torrent {}'.format(resource) logging.warning(error) return Response(errors=[error]) logging.info('Playing {}'.format(resource)) if self.player: try: self.player.stop() self.player = None except Exception as e: logging.exception(e) logging.warning( 'Unable to stop a previously running instance ' + 'of OMXPlayer, trying to play anyway') try: self.player = OMXPlayer(resource, args=self.args) self._init_player_handlers() except DBusException as e: logging.warning('DBus connection failed: you will probably not ' + 'be able to control the media') logging.exception(e) return self.status() def pause(self): if self.player: self.player.play_pause() def stop(self): if self.player: self.player.stop() self.player.quit() self.player = None return self.status() def voldown(self): if self.player: self.player.set_volume(max(-6000, self.player.volume() - 1000)) return self.status() def volup(self): if self.player: self.player.set_volume(min(0, self.player.volume() + 1000)) return self.status() def back(self): if self.player: self.player.seek(-30) return self.status() def forward(self): if self.player: self.player.seek(+30) return self.status() def next(self): if self.player: self.player.stop() if self.videos_queue: video = self.videos_queue.pop(0) return self.play(video) return Response(output={'status': 'no media'}, errors=[]) def hide_subtitles(self): if self.player: self.player.hide_subtitles() return self.status() def hide_video(self): if self.player: self.player.hide_video() return self.status() def is_playing(self): if self.player: return self.player.is_playing() else: return False def load(self, source, pause=False): if self.player: self.player.load(source, pause) return self.status() def metadata(self): if self.player: return Response(output=self.player.metadata()) return self.status() def mute(self): if self.player: self.player.mute() return self.status() def unmute(self): if self.player: self.player.unmute() return self.status() def seek(self, relative_position): if self.player: self.player.seek(relative_position) return self.status() def set_position(self, position): if self.player: self.player.set_seek(position) return self.status() def set_volume(self, volume): # Transform a [0,100] value to an OMXPlayer volume in [-6000,0] volume = 60.0 * volume - 6000 if self.player: self.player.set_volume(volume) return self.status() def status(self): state = PlayerState.STOP.value if self.player: state = self.player.playback_status().lower() if state == 'playing': state = PlayerState.PLAY.value elif state == 'stopped': state = PlayerState.STOP.value elif state == 'paused': state = PlayerState.PAUSE.value return Response( output=json.dumps({ 'source': self.player.get_source(), 'state': state, 'volume': self.player.volume(), 'elapsed': self.player.position(), 'duration': self.player.duration(), 'width': self.player.width(), 'height': self.player.height(), })) else: return Response( output=json.dumps({'state': PlayerState.STOP.value})) def on_play(self): def _f(player): get_bus().post(VideoPlayEvent(video=self.player.get_source())) return _f def on_pause(self): def _f(player): get_bus().post(VideoPauseEvent(video=self.player.get_source())) return _f def on_stop(self): def _f(player): get_bus().post(VideoStopEvent()) return _f def _init_player_handlers(self): if not self.player: return self.player.playEvent += self.on_play() self.player.pauseEvent += self.on_pause() self.player.stopEvent += self.on_stop() def search(self, query, types=None, queue_results=False, autoplay=False): results = [] if types is None: types = {'youtube', 'file', 'torrent'} if 'file' in types: file_results = self.file_search(query).output results.extend(file_results) if 'torrent' in types: torrent_results = self.torrent_search(query).output results.extend(torrent_results) if 'youtube' in types: yt_results = self.youtube_search(query).output results.extend(yt_results) if results: if queue_results: self.videos_queue = [_['url'] for _ in results] if autoplay: self.play(self.videos_queue.pop(0)) elif autoplay: self.play(results[0]['url']) return Response(output=results) @classmethod def _is_video_file(cls, filename): is_video = False for ext in cls.video_extensions: if filename.lower().endswith(ext): is_video = True break return is_video def file_search(self, query): results = [] query_tokens = [_.lower() for _ in re.split('\s+', query.strip())] for media_dir in self.media_dirs: logging.info('Scanning {} for "{}"'.format(media_dir, query)) for path, dirs, files in os.walk(media_dir): for f in files: if not self._is_video_file(f): continue matches_query = True for token in query_tokens: if token not in f.lower(): matches_query = False break if not matches_query: continue results.append({ 'url': 'file://' + path + os.sep + f, 'title': f, }) return Response(output=results) def youtube_search(self, query): logging.info('Searching YouTube for "{}"'.format(query)) query = urllib.parse.quote(query) url = "https://www.youtube.com/results?search_query=" + query response = urllib.request.urlopen(url) html = response.read() soup = BeautifulSoup(html, 'lxml') results = [] for vid in soup.findAll(attrs={'class': 'yt-uix-tile-link'}): m = re.match('(/watch\?v=[^&]+)', vid['href']) if m: results.append({ 'url': 'https://www.youtube.com' + m.group(1), 'title': vid['title'], }) logging.info( '{} YouTube video results for the search query "{}"'.format( len(results), query)) return Response(output=results) @classmethod def _get_youtube_content(cls, url): m = re.match('youtube:video:(.*)', url) if m: url = 'https://www.youtube.com/watch?v={}'.format(m.group(1)) proc = subprocess.Popen(['youtube-dl', '-f', 'best', '-g', url], stdout=subprocess.PIPE) return proc.stdout.read().decode("utf-8", "strict")[:-1] def torrent_search(self, query): logging.info( 'Searching matching movie torrents for "{}"'.format(query)) request = urllib.request.urlopen( urllib.request.Request( 'https://api.apidomain.info/list?' + urllib.parse.urlencode({ 'sort': 'relevance', 'quality': '720p,1080p,3d', 'page': 1, 'keywords': query, }), headers={ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' + '(KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' })) results = [{ 'url': _['items'][0]['torrent_magnet'], 'title': _['title'], } for _ in json.loads(request.read())['MovieList']] return Response(output=results) def download_torrent(self, magnet): import libtorrent as lt if not self.download_dir: raise RuntimeError( 'No download_dir specified in video.omxplayer configuration') ses = lt.session() ses.listen_on(*self.torrent_ports) info = lt.parse_magnet_uri(magnet) logging.info('Downloading "{}" to "{}" from [{}]'.format( info['name'], self.download_dir, magnet)) params = { 'save_path': self.download_dir, 'storage_mode': lt.storage_mode_t.storage_mode_sparse, } transfer = lt.add_magnet_uri(ses, magnet, params) status = transfer.status() files = [] self.torrent_state = { 'url': magnet, 'title': info['name'], } while (not status.is_seeding): status = transfer.status() torrent_file = transfer.torrent_file() if torrent_file: files = [ os.path.join(self.download_dir, torrent_file.files().file_path(i)) for i in range(0, torrent_file.files().num_files()) if self._is_video_file(torrent_file.files().file_name(i)) ] self.torrent_state['progress'] = 100 * status.progress self.torrent_state['download_rate'] = status.download_rate self.torrent_state['upload_rate'] = status.upload_rate self.torrent_state['num_peers'] = status.num_peers self.torrent_state['state'] = status.state logging.info( ('Torrent download: {:.2f}% complete (down: {:.1f} kb/s ' + 'up: {:.1f} kB/s peers: {} state: {})').format( status.progress * 100, status.download_rate / 1000, status.upload_rate / 1000, status.num_peers, status.state)) time.sleep(5) return Response(output=files) def get_torrent_state(self): return self.torrent_state
def play_video(source, commercials, max_commercials_per_break): if source==None: return #os.system("killall -9 omxplayer"); try: global next_video_to_play global last_video_played err_pos = -1.0 if next_video_to_play=='': print("Last video played: " + last_video_played) urlcontents = urllib2.urlopen("http://127.0.0.1/?getshowname=" + urllib.quote_plus(source)).read() print("Response: ") print(urlcontents) acontents = urlcontents.split("|") #if last_video_played == contents and contents!="News": if acontents[1]!="0" and acontents[0]!="News": print("Just played this show, skipping") return last_video_played = acontents[0] print("Last video played: " + last_video_played) next_video_to_play='' err_pos = 0.0 comm_source = get_random_commercial() err_pos = 0.1 comm_player = OMXPlayer(comm_source, args=['--no-osd', '--blank'], dbus_name="omxplayer.player1") err_pos = 0.12 comm_player.set_video_pos(40,10,660,470); comm_player.set_aspect_mode('fill'); err_pos = 0.13 comm_player.hide_video() err_pos = 0.14 comm_player.pause() err_pos = 0.15 #comm_player.set_volume(1) err_pos = 0.2 print('Main video file:' + source) err_pos = 0.21 contents = urllib2.urlopen("http://127.0.0.1/?current_video=" + urllib.quote_plus(source)).read() err_pos = 0.23 player = OMXPlayer(source, args=['--no-osd', '--blank'], dbus_name="omxplayer.player0") err_pos = 0.3 print('Boosting Volume by ' + str(get_volume(source)) + 'db') err_pos = 0.31 player.set_video_pos(40,10,660,470); player.set_aspect_mode('fill'); err_pos = 0.32 #player.set_volume(get_volume(source)) err_pos = 0.33 sleep(1) err_pos = 1.0 player.pause() err_pos = 1.1 strSubtitles = player.list_subtitles() if(strSubtitles): player.hide_subtitles() err_pos = 1.2 player.play() err_pos = 1.3 lt = 0 while (1): err_pos = 2.0 try: position = player.position() except: break if check_video()==True: #check if an outside source wants to play a new video print('outside source video return') err_pos = 8.0 player.hide_video() player.quit() comm_player.hide_video() comm_player.quit() sleep(0.5) return if len(commercials) > 0: #found a commercial break, play some commercials if math.floor(position)==commercials[0]: commercials.pop(0) player.hide_video() player.pause() sleep(0.5) comm_i = max_commercials_per_break err_pos = 3.0 while(comm_i>=0): if check_video()==True or next_video_to_play!='': #check if an outside source wants to play a new video print('outside source video return') err_pos = 6.0 player.hide_video() player.quit() comm_player.hide_video() comm_player.quit() sleep(0.5) return comm_source = get_random_commercial() print('Playing commercial #' + str(comm_i), comm_source) contents = urllib2.urlopen("http://127.0.0.1/?current_comm=" + urllib.quote_plus(comm_source)).read() comm_player.load(comm_source) comm_player.pause() sleep(0.1) if comm_i==4: comm_player.show_video() comm_player.play() err_pos = 4.0 while (1): try: comm_position = math.floor(comm_player.position()) except: break comm_i = comm_i - 1 sleep(1) err_pos = 5.0 player.show_video() player.play() err_pos = 7.0 player.hide_video() sleep(0.5) except Exception as e: if(err_pos!=7.0): contents = urllib2.urlopen("http://127.0.0.1/?error=MAIN_" + str(err_pos) + "_" + urllib.quote_plus(str(e))).read() print("error main " + str(e)) try: comm_player.quit() except Exception as ex: #contents = urllib2.urlopen("http://127.0.0.1/?error=COMMERCIAL_" + str(err_pos) + "_" + urllib.quote_plus(str(ex))).read() print("error comm quit " + str(ex)) try: player.quit() except Exception as exx: #contents = urllib2.urlopen("http://127.0.0.1/?error=PLAYER_" + str(err_pos) + "_" + urllib.quote_plus(str(exx))).read() print("error player quit " + str(exx)) return