Plsplay: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
Line 5: Line 5:


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.
== Usage ==
<source lang="bash">
plsplay wfmu.pls
</source>


== Sample pls file ==
== Sample pls file ==

Revision as of 21:58, 18 July 2009

This is a script I often use to play Internet radio stations from the command line. Michael

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.

Usage

plsplay wfmu.pls

Sample pls file

Here's a sample of a pls file for WFMU.

[playlist]
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

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"