juhala
Posts: 1
Joined: Sat Jan 19, 2013 5:25 am

Podcast downloader/player

Wed Jan 22, 2014 7:44 pm

Here is something I did to amuse myself while jogging or driving to work:

Setup:
- Raspberry Pi with the standard case http://www.partco.biz/verkkokauppa/prod ... scpkdve1i0
- Usb-battery
- WiFi dongle. I use a small Edimax as it does not pull +5V down and reboot the Raspberry Pi even though I unplug/plug in a case of bad connection.
- Indicator LED: On when Podget is active(and for some reason during the boot sequence, which is also fairly useful)
- A 4x4 matrix keyboard. They main functions:

1. Volume up
2. Volume down
3. Skip 60s back
4. 90s forward
5. pause
6. Next podcast on the playlist
7. reverse order of playlist
8. Generate playlist and play from beginning
9. Play live radio (in case I might have internet connection)
10. ..can't remember
11. Delete first podcast on the playlist
11. Delete currently playing podcast
12. ...can't remember
13. Podget
14. ...something
15. sudo halt
16. ...something

Installation:
1. Install Podget and configure
2. Install mplayer
3. Install Wicd(I could not get the default software to connect automatically to wlan)
4. configure crontab to start matrix.py on startup


How it principally works:
1. It starts to play the first podcast on the playlist on boot up
2. Podcasts on the Podget serverlist are downloaded with Podget by pushing button no 13
3. Playlist is regenerated by pushing button 7 and on each boot.

Playlist is in chronological order as I want to hear the latest news first.
4. I may want to skip to next podcast by pushing button 6 or delete the one on top of the list by pushing buttons 1 and 4

TO DO:
1.Memory: some code to remember the episode and position I was listening before shutdown. I've tried mplayer-resume.py but could not get it working.

2. Make a better system to delete currently played podcast. Now podcasts are numbered 1.2.3. and the currently playing podcast is memorized by the skip button press. If I listen to the whole podcast the index number will not change. Pressing button 12 will delete a podcast and the row from the playlist according to the index number.

3. Smaller form factor, Smaller power consumption with wifi dongle attached. Now I can listen for the whole day without the wifi dongle and maybe four hours with the dongle attached.

4. ...


Sample code(I owe a lot of beer for this code):

Code: Select all

#IN
# 1. vol+
# 2. vol-
# 3. -60s
# 4. +90s
# 5. pause
# 6. skip fwd 1 file
# 7. reverse playlist and play
# 8. podplayer(make playlist and start from new)
# 9. sudo pkill mplayer| mplayer ylepuhe
# 10. filename=$(date -u +"%Y%m%d_%H%M%S").h264 ; raspivid -ex antishake -vs -t 60000 -o  /home/pi/$filename
# 11. delete top of playlist
# 12. delete current
# 13. podget
# 14. sudo ifdown wlan0 ##festival "wlan power"
# 15. sudo halt
# 16. sudo ifup wlan0 ##festival "IP"

#OUT:
# 8 green: podget
# 10 yellow:
# 23 red1:
# 24 red2:
# 26 red3:



import RPi.GPIO as GPIO

import subprocess, argparse, time, os, sys, os.path

GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)

#GPIO.setwarnings(False)

n = 0.2
m = 0
o = 0

MATRIX = [ [1,2,3,4],
	   [5,6,7,8],
	   [9,10,11,12],
	   [13,14,15,16] ]

ROW = [7,11,13,15]
COL = [12,16,18,22]

GPIO.setup(24, GPIO.OUT)

for j in range(4):
	GPIO.setup(COL[j], GPIO.OUT)
	GPIO.output(COL[j], 1)

for i in range(4):
	GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)


with open("playlist") as f:
    line = f.readlines()[o]
    with open("current", "w") as f1:
        f1.writelines(line)
	f.close()
while(True):
	for j in range(4):
		GPIO.output(COL[j],0)
					
		for i in range(4):
			if GPIO.input(ROW[i]) == 0:
				x = MATRIX[i][j]

				if x == 1:
					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
				 		os.system('sudo echo volume 10 > /tmp/mplayercontrol')
						print "vol+"
						time.sleep(n)
				elif x == 2:
					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
						os.system('sudo echo volume -10 > /tmp/mplayercontrol')
						print "vol-"
						time.sleep(n)
				elif x == 3:
 					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
						os.system('sudo echo seek -60 0 > /tmp/mplayercontrol')
						print "-60s"
						time.sleep(n)
				elif x == 4:
					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
						os.system('sudo echo seek 90 0 > /tmp/mplayercontrol')
						print "+90s"
						time.sleep(n)
				elif x == 5:
					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
						os.system('sudo echo pause > /tmp/mplayercontrol')
						print "pause"
						time.sleep(n)
				elif x == 6:
					o = o+1
					os.system('sudo echo seek 100 1 > /tmp/mplayercontrol')
					with open("playlist") as f:
    					    line = f.readlines()[o]
    					    with open("current", "w") as f1:
        				        f1.writelines(line)
						f.close()
					print "skip"
					print o
					time.sleep(n)
				elif x == 7:
					o = 0
					os.system('sudo pkill mplayer ; tac /home/pi/playlist > playlist1 ; cat /home/pi/playlist1 > /home/pi/playlist ; mplayer -slave -quiet -input file=/tmp/mplayercontrol -playlist playlist &')
					with open("playlist") as f:
    					    line = f.readlines()[o]
    					    with open("current", "w") as f1:
        				        f1.writelines(line)
					        f.close()
					print "playlist reverse and play"
					print o
					time.sleep(n)
				elif x == 8:
					o = 0
					print "generate playlist and start"
					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
						os.system('pkill mplayer ; ./podplayer &')
					with open("playlist") as f:
    					    line = f.readlines()[o]
    					    with open("current", "w") as f1:
        				        f1.writelines(line)
					        f.close()
					print o
					time.sleep(n)
				elif x == 9:
					os.system('sudo pkill mplayer ; mplayer -slave -quiet -input file=/tmp/mplayercontrol mms://mediau.yle.fi/liveradiopuhe')
					time.sleep(n)
				elif x == 10:
					print o
#                                       os.system('sudo pkill mplayer ; mplayer -slave -quiet -input file=/tmp/mplayercontrol -playlist playlist')
#					print "raspivid 30s"
                                        time.sleep(n)
#					os.system('filename=$(date -u +"%Y%m%d_%H%M%S").h264 ; raspivid -ex antishake -vs -t 60000 -o /home/pi/$filename')
				elif x == 11:
					o = 0
					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
						os.system('head -n 1 playlist | xargs sudo rm ; pkill mplayer ; ./podplayer &')
					with open("playlist") as f:
    					    line = f.readlines()[o]
    					    with open("current", "w") as f1:
        				        f1.writelines(line)
	                                        print "skip"
        	                                time.sleep(n)
				elif x == 12:
					o = 0
					ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
					if "mplayer -slave" in ps:
						os.system('cat current | xargs sudo rm ; pkill mplayer ; ./podplayer &')
					with open("playlist") as f:
    					    line = f.readlines()[o]
    					    with open("current", "w") as f1:
        				        f1.writelines(line)
					        f.close()
	                                        print "skip"
					print o
       	                                time.sleep(n)
				elif x == 13:
					GPIO.output(8,True)
					os.system('sudo -u pi podget')
					GPIO.output(8,False)
					print "podget"
					time.sleep(n)
				elif x == 14:	
					GPIO.output(8,True)
					time.sleep(0.2)
					GPIO.output(8,False)
					time.sleep(0.2)
					GPIO.output(10,True)
					time.sleep(0.2)
					GPIO.output(10,False)
					time.sleep(0.5)
					GPIO.output(23,True)
					time.sleep(0.2)
					GPIO.output(23,False)
					time.sleep(0.2)
					GPIO.output(24,True)
					time.sleep(0.2)
					GPIO.output(24,False)
					time.sleep(0.2)
					GPIO.output(26,True)
					time.sleep(0.2)
					GPIO.output(26,False)
					time.sleep(0.2)
#					os.system('sudo ifdown --force  wlan0')  #sudo iwlist wlan0 scan|grep Quality|cut -d"-" -f2|cut -d" " -f1 | festival --tts')
#					print "wlan power down"
					time.sleep(n)
				elif x == 15:
					os.system('sudo halt')
					print "sudo halt"
					time.sleep(n)
				elif x == 16:
					os.system('sudo ifup --force wlan0')#hostname -I | festival --tts') 
					time.sleep(n)
					print "wlan up"
				else:
					print "EI"
#			if m == 1:
#				os.system('date +"%T" | festival --tts')
#				exit

				while(GPIO.input(ROW[i]) == 0):
					pass
		GPIO.output(COL[j],1)

and the podcast starter applet "podplayer" looks like this:

Code: Select all

o=1
find . -iname "*/sn/*.mp3*" -type f  -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //'  > playlist;
find . -iname "*.mp3*" -type f  -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //' >> playlist;
find . -iname "*.m4a*" -type f  -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //'  >> playlist;

mkfifo /tmp/mplayercontrol ; 
mplayer -slave -quiet -input file=/tmp/mplayercontrol -playlist playlist


I have also configured a web server on the device to show the playlist. This is all based on "Podget Podcast Lister 0.12 © by Martin Eberhardt" This web page has a link to the details: http://podget.sourceforge.net/

Return to “Graphics, sound and multimedia”