|
|
(2 intermediate revisions by one other user not shown) |
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 == |
|
| |
|
| == Sample pls file ==
| | <source lang="bash"> |
| | | ls *.pls |
| Here's a sample of a pls file for [http://wfmu.org | WFMU].
| | plsplay wfmu.pls |
| | |
| <source lang="text"> | |
| [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
| |
| </source> | | </source> |
|
| |
|
| == Code ==
| |
|
| |
| <source lang="python">
| |
| #!/usr/bin/python
| |
|
| |
|
| import re, sys, os
| |
|
| |
|
| """ | | <source lang="bash"> |
| usage:
| | plsplay wfmu.pls 2 |
| plsplay pls-or-other-textfile-path [index] [dumpfile]
| | </source> |
|
| |
|
| where:
| |
| index is a 1-based index in case of multiple URL matches
| |
| """
| |
|
| |
|
| # urlfinder = re.compile(r"([a-z]+://[-a-zA-Z0-9./?=&+%_:]*)")
| | == Code == |
| urlfinder = re.compile(r"[a-z]+://[-a-zA-Z0-9./?=&+%_:]*")
| |
|
| |
|
| # read groupindex (1-based), ie which URL match to use
| | [[Include(source:cookbook plsplay)]] |
| # 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>
| |