Plsplay: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
Line 1: Line 1:
This is a script I often use to play Internet radio stations from the command line.
= plsplay =
[[User:Murtaugh|Michael]]


== Discussion ==
A script to ....
 
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.


== Usage ==
== Usage ==


<source lang="bash">
<source lang="bash">
ls *.pls
plsplay wfmu.pls
plsplay wfmu.pls
</source>
</source>


== Sample pls file ==


Here's a sample of a pls file for [http://wfmu.org WFMU].


<source lang="text">
<source lang="bash">
[playlist]
plsplay wfmu.pls 2
numberofentries=2
Title1=WFMU - Freeform Radio the Way it Oughta Be
File1=http://mp3stream.wfmu.org
Title2=WFMU - Freeform Radio the Way it Oughta Be
File2=http://archivevirt.wfmu.org:8006
</source>
</source>


== Code ==
== Code ==


<source lang="python">
[[Include(source:cookbook plsplay)]]
#!/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"
</source>

Latest revision as of 20:29, 23 September 2010

plsplay

A script to ....

Usage

ls *.pls
plsplay wfmu.pls


plsplay wfmu.pls 2


Code

Include(source:cookbook plsplay)