Hello all. Here's my second Ubuntu forums thread. This time I've written a rough Python script to record streaming Pandora audio.
The goals of the project were as follows:
1. No use of the web interface which would mean using flash. Use pianobar program as backend instead,
2. Sort songs into a folder hierarchy. Artist/Album/Song. Basically create the non-existent ones on the fly.
3. Save as either .ogg or .mp3 with same quality as the sound coming out of the speakers.
The goals at this time do not include interaction. It's just run it and sit back while it builds you a local library.
This script uses python's popen methods to communicate with a pianobar process(play) and a avconv process(record). Avconv simply grabs the audio output from the speakers and records it in this case. Unfortunately, this means that anything else coming through the sound card will be recorded along with the song. This wasn't a concern for me because I was thinking of just having a headless machine in the corner set up to record.
For this to work you will need to:
1. Install libav-tools package and pianobar package from the software center.
2. edit the line in /etc/pulse/daemon.conf that looks like this:
Code:
;default-sample-rate = whatever it was before
and change it to this:
Code:
default-sample-rate = 48000
So basically uncomment that line and change the rate to 48000.
3.
Here is the original code for recording indefinetely to a file from your speakers:
Code:
avconv -f pulse -i alsa_output.pci-0000_00_14.2.analog-stereo.monitor -acodec libvorbis -ac 2 -ar 48000 -vn out.ogg
Note that this is set up for my specific sound card output. So this is the next task.
Open a terminal and enter this:
Your most likely looking for Source #0 and you will have to copy and paste what shows up under Name: into my python code in place of what I have there for my soundcard.
Mine was this:
Code:
alsa_output.pci-0000_00_14.2.analog-stereo.monitor
So just edit that one line of my script, but be careful not to remove the quotes.
To run the script, open a terminal and cd to the script, then do:
Code:
python record-pandora_1.0.py
Here's the script:
Code:
import subprocess, time, os
def startRecord(playingInfo1, playingInfo2, playingInfo3):
global p2
try: #try to create new artist dir if not already there
if(not os.path.exists("music/" + playingInfo1)):
os.mkdir("music/" + playingInfo1)
print "Created new Artist dir"
#create new Album dir if not already there
if(not os.path.exists("music/" + playingInfo1 + "/" + playingInfo2)):
os.mkdir("music/" + playingInfo1 + "/" + playingInfo2)
print "Created new Album dir"
#record new song if not already recorded
if(not os.path.exists("music/" + playingInfo1 + "/" + playingInfo2 + "/" + playingInfo3 + ".ogg")):
try: ### the next line is for specific sound card, edit it
avconvCmd = "avconv", "-f", "pulse", "-i", "alsa_output.pci-0000_00_14.2.analog-stereo.monitor", "-acodec", "libvorbis", "-ac", "2", "-ar", "48000", "-vn" , 'music/' + playingInfo1 + '/' + playingInfo2 + '/' + playingInfo3 + '.ogg'
p2 = subprocess.Popen(avconvCmd)
print "Created new Song file, now recording...\n"
except:
print "Error opening avconv! Is libav-tools installed?\n"
except:
print "Couldn't create new Song file!\n"
### program start here ###
#variables and lists
p2 = ""
#create music dir if it doesn't exist
if(not os.path.exists("music")):
os.mkdir("music")
#try to run pianobar
try:
cmd = "pianobar"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
output = p.stdout.readline()
except:
print "Error opening pianobar! Is it installed?\n"
while(output):
if('|> "' in output): #beginning of songs playing
try:
p2.kill() #kill recording of last song
except:
print
try: #try to clean up and format pianobar's output
cleanPianobarChars = output.split('|> "', 1)
songName = cleanPianobarChars[1].split('" by "', 1)
artistName = songName[1].split('" on "', 1)
albumName = artistName[1].split('"', 1)
#put Artist name, Album name and Song name into a list
playingInfo1 = artistName[0]
playingInfo2 = albumName[0]
playingInfo3 = songName[0]
print "\nArtist: " + playingInfo1 + "\nAlbum: " + playingInfo2 + "\nSong: " + playingInfo3 + "\n"
#start dir check or creation and record to file
startRecord(playingInfo1, playingInfo2, playingInfo3)
except:
print "Something went wrong with cleaning up pianobar output...\n"
output = p.stdout.readline()
Enjoy!
Bookmarks