From e3634810808b535ad72e977216054ea7bdfb73ac Mon Sep 17 00:00:00 2001 From: George Vlahavas Date: Wed, 23 Jan 2013 20:20:30 +0200 Subject: [PATCH 1/4] Add a mode for omxplayer local sound playback Add a "--player omxplayerlocal" option, that plays audio through the local output instead of the hdmi output with omxplayer. --- src/yt/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/yt/__init__.py b/src/yt/__init__.py index 9bd1c21..d247755 100755 --- a/src/yt/__init__.py +++ b/src/yt/__init__.py @@ -15,12 +15,13 @@ # Define possible player modes. MPLAYER_MODE="mplayer" OMXPLAYER_MODE="omxplayer" +OMXPLAYERLOCAL_MODE="omxplayerlocal" def main(): # Allow the user to specify whether to use mplayer or omxplayer for playing videos. parser = argparse.ArgumentParser(prog='yt',formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument("--player",default=MPLAYER_MODE,choices=[MPLAYER_MODE,OMXPLAYER_MODE],help="specifies what program to use to play videos") + parser.add_argument("--player",default=MPLAYER_MODE,choices=[MPLAYER_MODE,OMXPLAYER_MODE,OMXPLAYERLOCAL_MODE],help="specifies what program to use to play videos") args = parser.parse_args(sys.argv[1:]) @@ -375,11 +376,13 @@ def play_url(url,player): sys.stderr.write(err) raise RuntimeError('Error getting URL.') - assert player in [MPLAYER_MODE,OMXPLAYER_MODE] + assert player in [MPLAYER_MODE,OMXPLAYER_MODE,OMXPLAYERLOCAL_MODE] if player == MPLAYER_MODE: play_url_mplayer(url) + elif player == OMXPLAYER_MODE: + play_url_omxplayer(url) else: - play_url_omxplayer(url) + play_url_omxplayerlocal(url) def play_url_mplayer(url): player = subprocess.Popen( @@ -393,6 +396,12 @@ def play_url_omxplayer(url): stdout = subprocess.PIPE, stderr = subprocess.PIPE) player.wait() +def play_url_omxplayerlocal(url): + player = subprocess.Popen( + ['omxplayer', url.decode('UTF-8').strip()], + stdout = subprocess.PIPE, stderr = subprocess.PIPE) + player.wait() + def search(terms): def fetch_cb(start, maxresults, ordering): url = 'https://gdata.youtube.com/feeds/api/videos' From 074cb486c1280a32992b0a7855f1c5d3d8e0f711 Mon Sep 17 00:00:00 2001 From: George Vlahavas Date: Wed, 23 Jan 2013 21:06:58 +0200 Subject: [PATCH 2/4] Configure yt default player using a ~/.config/yt settings file The user can set the default player in a ~/.config/yt settings file, by reading the "player" entry, in the "[General]" section of the settings file. The format of the settings file should be like this: [General] player = omxplayer If the file is not there, or if it's corrupted in any way, yt defaults to mplayer. --- src/yt/__init__.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/yt/__init__.py b/src/yt/__init__.py index d247755..0d6b23e 100755 --- a/src/yt/__init__.py +++ b/src/yt/__init__.py @@ -11,6 +11,8 @@ import urllib import urllib2 import argparse +import os +import ConfigParser # Define possible player modes. MPLAYER_MODE="mplayer" @@ -19,9 +21,21 @@ def main(): - # Allow the user to specify whether to use mplayer or omxplayer for playing videos. + # Read default settings, if they are there. + # If they are not there, or is set to something invalid, default to mplayer. + config = ConfigParser.RawConfigParser() + config_file = os.path.expanduser('~/.config/yt') + try: + config.read(config_file) + DEFAULT_MODE = config.get('General', 'player') + if DEFAULT_MODE not in [MPLAYER_MODE, OMXPLAYER_MODE, OMXPLAYERLOCAL_MODE]: + DEFAULT_MODE = MPLAYER_MODE + except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): + DEFAULT_MODE = MPLAYER_MODE + + # Allow the user to specify whether to override the default player setting. parser = argparse.ArgumentParser(prog='yt',formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument("--player",default=MPLAYER_MODE,choices=[MPLAYER_MODE,OMXPLAYER_MODE,OMXPLAYERLOCAL_MODE],help="specifies what program to use to play videos") + parser.add_argument("--player",default=DEFAULT_MODE,choices=[MPLAYER_MODE,OMXPLAYER_MODE,OMXPLAYERLOCAL_MODE],help="specifies what program to use to play videos") args = parser.parse_args(sys.argv[1:]) From 46024f664fafa1bdc8e528986472533cf19b7852 Mon Sep 17 00:00:00 2001 From: George Vlahavas Date: Wed, 23 Jan 2013 21:19:24 +0200 Subject: [PATCH 3/4] Update README.rst with info about the settings file. Add information in the README.rst file about creating the ~/.config/yt settings file. --- README.rst | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index e0d70d0..b0b02df 100644 --- a/README.rst +++ b/README.rst @@ -26,7 +26,19 @@ Usage --player {mplayer,omxplayer} specifies what program to use to play videos (default: mplayer) - + +Configuration +------------- +The default player can be set in a ~/.config/yt settings file. The format of the +settings file should be like this (example for setting omxplayer as the default): + +[General] +player = omxplayer + +Valid options are ``mplayer``, ``omxplayer`` and ``omxplayerlocal``, for setting +the default player to mplayer, omxplayer using the hdmi audio output and omxplayer +using the local audio output, respectively. + Dependancies ------------ From 7a404821324de2bfed791963624d8fa4010ca4fe Mon Sep 17 00:00:00 2001 From: George Vlahavas Date: Wed, 23 Jan 2013 21:46:20 +0200 Subject: [PATCH 4/4] Fix file configuration text in README.rst file Indent the sample settings file so it shows right in github. --- README.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index b0b02df..d4443ef 100644 --- a/README.rst +++ b/README.rst @@ -32,8 +32,10 @@ Configuration The default player can be set in a ~/.config/yt settings file. The format of the settings file should be like this (example for setting omxplayer as the default): -[General] -player = omxplayer +:: + + [General] + player = omxplayer Valid options are ``mplayer``, ``omxplayer`` and ``omxplayerlocal``, for setting the default player to mplayer, omxplayer using the hdmi audio output and omxplayer