Plsplay: Difference between revisions

From XPUB & Lens-Based wiki
(New page: A very handy script to allow easy command line playback of a "pls" file. Uses a regular expression to suck out a URL and feed it to mplayer for playback. Annoyingly, mplayer can't be ...)
 
No edit summary
Line 1: Line 1:
A very handy script to allow easy command line playback of a "pls" file. Uses a [[regular expression]] to suck out a URL and feed it to mplayer for playback.
A very handy script to allow easy command line playback of a "pls" file. Uses a [[regular expression]] to suck out a URL and feed it to mplayer for playback.
== Discussion ==


Annoyingly, mplayer can't be used to directly play a "pls" file (common format for Internet radio stations). However, mplayer almost always *can* be used to play the stream, you just need to feed it the direct URL of the stream, which listed in the pls file itself (the pls is simply a text file giving one or more possible URLs of the feed). In this case, a simple Python script extracts the URLs and passes it on to mplayer to do the actual work. An optional numeric parameter inidicates which URL to use if multiple URLs are present in the file.
Annoyingly, mplayer can't be used to directly play a "pls" file (common format for Internet radio stations). However, mplayer almost always *can* be used to play the stream, you just need to feed it the direct URL of the stream, which listed in the pls file itself (the pls is simply a text file giving one or more possible URLs of the feed). In this case, a simple Python script extracts the URLs and passes it on to mplayer to do the actual work. An optional numeric parameter inidicates which URL to use if multiple URLs are present in the file.
== Code ==


<source lang="python">
<source lang="python">

Revision as of 21:55, 18 July 2009

A very handy script to allow easy command line playback of a "pls" file. Uses a regular expression to suck out a URL and feed it to mplayer for playback.

Discussion

Annoyingly, mplayer can't be used to directly play a "pls" file (common format for Internet radio stations). However, mplayer almost always *can* be used to play the stream, you just need to feed it the direct URL of the stream, which listed in the pls file itself (the pls is simply a text file giving one or more possible URLs of the feed). In this case, a simple Python script extracts the URLs and passes it on to mplayer to do the actual work. An optional numeric parameter inidicates which URL to use if multiple URLs are present in the file.

Code

#!/usr/bin/python

import re, sys, os

"""
usage:
	plsplay pls-or-other-textfile-path [index] [dumpfile]

where:
	index is a 1-based index in case of multiple URL matches
"""

# urlfinder = re.compile(r"([a-z]+://[-a-zA-Z0-9./?=&+%_:]*)")
urlfinder = re.compile(r"[a-z]+://[-a-zA-Z0-9./?=&+%_:]*")

# read groupindex (1-based), ie which URL match to use
# default is first matched URL
groupindex = 1
if len(sys.argv) >= 3:
	groupindex = int(sys.argv[2])

# optional dump file
dumpfile = None
if len(sys.argv) >= 4:
	dumpfile = sys.argv[3]

inp = open(sys.argv[1])
fc = inp.read()

matches=urlfinder.findall(fc)
if len(matches) > 0:
	url = matches[groupindex-1]

if url:
	print "found url:", url
	if dumpfile:
		os.system("mplayer -slave %s -dumpstream -dumpfile %s & sleep 5; (cat %s | mplayer -cache 32000 -)" % (url, dumpfile, dumpfile))
	else:
		os.system("mplayer %s" % url)
		
else:
	print "no url found"