Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ 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
------------

Expand Down
31 changes: 27 additions & 4 deletions src/yt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,31 @@
import urllib
import urllib2
import argparse
import os
import ConfigParser

# 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.
# 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],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:])

Expand Down Expand Up @@ -375,11 +390,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(
Expand All @@ -393,6 +410,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'
Expand Down