Tag Archives: command line

iheartradio – command line (mplayer)

Well after some experimentation and playing around I have found some new information out regarding listening to iheartradio from the command line in Linux.

Newer versions of mplayer have support to play the rtmp:// protocol eliminating the need for rtmpdump.

Quick Recap on how to grab the rtmp:// url from an iheartradio stream

  • Go to http://p2.STATION_ID.ccomrcdn.com/player/player_dispatcher.html?section=radio&action=listen_live; where station id is the call letters (ex: wtfx-fm)
  • The attribute primary_location in <stream> contains the rtmp url

NOTE: The RTMP URL changes every 5-10 minutes! You must fetch the new url everytime.

To play the stream with mplayer:

mplayer "rtmp://cp21366.live.edgefcs.net/live/Lou_KY_WTFX-FM_OR..." -novideo

The -novideo option is very improtant otherwise mplayer will take 5+ minutes trying to find video for the stream (there is none).

This is all great, but this is a lot of work everytime you want to listen to a iheartradio stream. Therefore I have coded up a script.

The Script:

  • Asks you for the station id (ex: wtfx-fm)
  • Asks how long the stream should play for
  • Check for song information every 10 seconds
  • Not Perfect (Alpha Quality)
#!/usr/bin/env python

import subprocess
import time
import urllib2
import xml.etree.ElementTree as ET
import os
import datetime

#Location of Stream to be SAVED
DefaultStation="wtfx-fm"

def getXML():
    data=urllib2.urlopen('http://p2.'+station+'.ccomrcdn.com/player/player_dispatcher.html?section=radio&action=listen_live').read()
    xml=ET.fromstring(data)
    return xml

def getSongInfo():
    xml=getXML()
    artist=xml.find('ListenLiveInitialize/JustPlayed/song/artist').attrib['name']
    title=xml.find('ListenLiveInitialize/JustPlayed/song/track').attrib['track_title']
    return artist,title



while True:
    station=raw_input("Enter Station ID [" + DefaultStation + "]: ")

    if not station:
        station=DefaultStation
    try:
        xml=getXML()
    except urllib2.URLError:
        print "Error - Invalid Station ID or Web Server Problem - Try Again"
    else:
        break

while True:
    try:
        TIME=int(raw_input("Time Stream will Play (in minutes): "))
    except ValueError:
        print "Error - Invalid Time - Try Again"
    else:
        break

rtmpurl=xml.find("ListenLiveInitialize/StreamInfo/stream").attrib['primary_location']

mp=subprocess.Popen(['/usr/bin/mplayer', rtmpurl, '-novideo', '-ao', 'alsa', '-quiet'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
mpPID=mp.pid

endTime = datetime.datetime.now() + datetime.timedelta(minutes=TIME)

OldSongInfo=[]

while datetime.datetime.now() < endTime:
    SongInfo=getSongInfo()
    if not SongInfo == OldSongInfo:
        OldSongInfo = SongInfo
        print SongInfo[0] + " - " + SongInfo[1]
    time.sleep(5)



print("Stopping MPlayer...")
os.kill(mpPID, 2)

print "Done"

N900

I got this script working on the N900 by downloading the Maemo 5 SDK and compiling the mplayer binary from a recent svn snapshot. Luckily, I didn't run into any problems. Although mplayer works with the N900, without any patches, it is not flawless. For example, video  gets jittery and skips when the backlight switches off. I would recommend leaving mplayer form extra installed and storing the mplayer you compiled in /opt. With audio playback I  do not have any issues, especially with the iheartradio rtmp stream!