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!

4 Responses to “iheartradio – command line (mplayer)”

  1. JK Says:

    Here is a one-liner bash function that will allow you to stream any iheartradio station by typing:

    iheartradio

    function iheartradio { mplayer -quiet $(wget -O – -q “http://p2.$1.ccomrcdn.com/player/player_dispatcher.html?section=radio&action=listen_live” | sed -n “s|.*primary_location=\”\(rtmp://[^\"]*\)\”.*|\1|p”) -novideo >& /dev/null; }

  2. fukinA Says:

    i don’t know how to thank you for this
    i’ve been searching for weeks and always failed and gave up on how to do this and play on my android that doesn’t come with adobe flash support like the new phones
    someone just bumped a topic with this method how to get the rtmp url for transcoding it into mp3, but it didn’t helped with the playback
    also mplayer url was the first command i thought of but failed :)
    thank god for you and for google: ccomrcdn.com/player/player_dispatcher.html?section=radio&action=listen_live
    you are first result
    now i can play all my alternative radios
    god bless you!

  3. fukinA Says:

    here’s JK’s bash script that actually works if anyone googles
    pastebin.com/Cq68Gv4h
    the blog probably messed up few quotes ” ” and a minus – and the script didn’t worked. it’s been a nightmare for me to fix, a guy that has no scripting knowledge, but someone helped me
    thank you JK, whoever you are :D

    btw i’m using his script because your python one can’t change mplayer’s volume from command line. maybe you can implement that. but both work great for linux phones anyway.
    here’s an idea for your script, adding few lines for favorite radios and enable selection at script start through 1/2/3/4/5 etc instead of remembering many radio codenames

  4. preserved Says:

    Here’s a quick patch that lets the iheartradio.py work with Talk Radio (like waeb-am) and Music Radio:

    — iheartradio.old.py 2012-05-22 21:21:19.000000000 -0400
    +++ iheartradio.py 2012-05-22 21:19:28.000000000 -0400
    @@ -17,9 +17,12 @@

    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
    + if xml.find(‘ListenLiveInitialize/JustPlayed/song/artist’) == None:
    + return “None”,”None”
    + else:
    + 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 + "]: “)
    @@ -52,9 +55,10 @@

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

    print("Stopping MPlayer…")

    May try to add this to RadioTray – like I Heart too.

Leave a Reply