#!/usr/bin/python # Use rtmpdump to download a show from publicradio.org -- their player needs # either Flash or RealPlayer otherwise. # Based on: http://stream-recorder.com/forum/archive/server-metadata-duration-segmenting-rtmp-stream-causing-t10390.html # Adam Sampson import os, sys, re, subprocess from offog import die, warn def get_ra(url): # Older shows in RealAudio: # http://www.publicradio.org/tools/media/player/phc/2008/03/08_phc # http://www.publicradio.org/tools/media/player/phc/000401/000401_phc m = re.match(r'http:.*/media/player/([^/]+)/(.*)_', url) if m is None: return None (show, date) = m.group(1, 2) # Make up a basename like the newer shows have. ds = date.split("/") if len(ds) == 2 and ds[0] == ds[1]: # Older ones are .../phc/000401/000401_phc base = show + "_20" + ds[0] + "_ra" else: # Newer ones are .../phc/2008/03/08_phc base = show + "_" + date.replace("/", "") + "_ra" cmd = [ "mplayer", "-ao", "pcm:file=" + base + ".wav", "-playlist", url, ] if subprocess.call(cmd) != 0: die("Fetch failed: ", cmd) cmd = [ "lame", "-V3", base + ".wav", base + ".mp3", ] if subprocess.call(cmd) != 0: die("Command failed: ", cmd) os.unlink(base + ".wav") return True def get_flv(url): # Newer shows in MP3 via Flash: # http://prairiehome.publicradio.org/www_publicradio/tools/media_player/popup.php?name=phc/2011/12/24/phc_20111224_64 m = re.match(r'^http:.*media_player.*name=([^&]+)$', url) if m is None: return None show = m.group(1) base = os.path.basename(show) delete = True cmd = [ "rtmpdump", "-v", "-r", "rtmp://archivemedia.publicradio.org/music", "-a", "music", "-f", "WIN 11,0,1,152", "-W", "http://pipedreams.publicradio.org/www_publicradio/tools/media_player/player.swf", "-p", url, "-y", "mp3:ondemand/" + show, "-o", base + ".flv", ] if subprocess.call(cmd) != 0: warn("Fetch failed (but converting anyway): ", cmd) delete = False cmd = [ "ffmpeg", "-i", base + ".flv", "-acodec", "copy", base + ".mp3" ] if subprocess.call(cmd) != 0: die("Command failed: ", cmd) if delete: os.unlink(base + ".flv") return True def get_show(url): for func in (get_flv, get_ra): rc = func(url) if rc is not None: return rc die("URL didn't match expected format: ", url) if __name__ == "__main__": for url in sys.argv[1:]: get_show(url)