Skip to content

Commit

Permalink
Added Streaming Internet Radio support
Browse files Browse the repository at this point in the history
Removed all dependencies, ready for merge.
  • Loading branch information
d4m4s74 committed Mar 8, 2014
1 parent 8f6a0ac commit 0b67639
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions PirateRadio.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def build_file_list():
folders.sort()
files.sort()
for filename in files:
if re.search(".(aac|mp3|wav|flac|m4a)$", filename) != None:
if re.search(".(aac|mp3|wav|flac|m4a|pls|m3u)$", filename) != None:
file_list.append(os.path.join(root, filename))
return file_list

Expand All @@ -53,14 +53,25 @@ def play_songs(file_list):
print("Playing songs to frequency ", str(frequency))
print("Shuffle is " + on_off[shuffle])
print("Repeat All is " + on_off[repeat_all])
#print("Stereo playback is " + on_off[play_stereo])
# print("Stereo playback is " + on_off[play_stereo])

if shuffle == True:
random.shuffle(file_list)
with open(os.devnull, "w") as dev_null:
for filename in file_list:
print("Playing ",filename)
subprocess.call(["ffmpeg","-i",filename,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null)
if re.search(".pls$", filename) != None:
streamurl = parse_pls(filename, 1)
if streamurl != None:
print("streaming radio from " + streamurl)
subprocess.call(["ffmpeg","-i",streamurl,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null)
elif re.search(".m3u$", filename) != None:
streamurl = parse_m3u(filename, 1)
if streamurl != None:
print("streaming radio from " + streamurl)
subprocess.call(["ffmpeg","-i",streamurl,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null)
else:
subprocess.call(["ffmpeg","-i",filename,"-f","s16le","-acodec","pcm_s16le","-ac", "2" if play_stereo else "1" ,"-ar","44100","-"],stdout=music_pipe_w, stderr=dev_null)



Expand All @@ -81,6 +92,44 @@ def read_config():
shuffle = config.getboolean("pirateradio",'shuffle',fallback=False)
repeat_all = config.getboolean("pirateradio",'repeat_all', fallback=False)

def parse_pls(src, titleindex):
# breaking up the pls file in separate strings
lines = []
with open( src, "r" ) as f:
lines = f.readlines()

# and parse the lines
if lines != None:
for line in lines:
# search for the URI
match = re.match( "^[ \\t]*file" + str(titleindex) + "[ \\t]*=[ \\t]*(.*$)", line, flags=re.IGNORECASE )
if match != None:
if match.group( 1 ) != None:
# URI found, it's saved in the second match group
# output the URI to the destination file
return match.group( 1 )

return None

def parse_m3u(src, titleindex):
# create a list of strings, one per line in the source file
lines = []
searchindex = int(1)
with open( src, "r" ) as f:
lines = f.readlines()

if lines != None:
for line in lines:
# search for the URI
if '://' in line:
if searchindex == titleindex:
return line
else:
searchindex += 1

return None



def daemonize():
fpid=os.fork()
Expand Down

0 comments on commit 0b67639

Please sign in to comment.