2011-03-14 27 views
4

我試圖創建一個網絡無線電服務器來一次流3個源。我正在使用python爲使用python-shout庫的icecast2創建一個源客戶端。我對語言不太熟悉(python)。然而,我得到了一個示例程序,它可以滿足我的需要,並且我已經根據需要調整了它。但是,我只能創建兩個流,之後,我收到如下所示的錯誤消息。我不知道我做錯了什麼,所以我希望你們能幫我弄清楚。使用python-shout創建icecast2服務器的多個音頻流

hostname ="localhost" 
port= 8000 
password = "password" 

import shout 
import sys 
import threading 
from glob import glob 
from random import shuffle,choice 

class RunStream (threading.Thread): 
    def __init__ (self, channel_mount, music_directory, station_url, genre,name,   description, bitrate="128", samplerate="44100", channels="5",music_format="mp3", ogv=0): 
    #connection to icecast 
    global hostname,port,password 
    self.song_conter= 0 
    self.s = shout.Shout() 
    self.s.audio_info = {shout.SHOUT_AI_BITRATE:bitrate, shout.SHOUT_AI_SAMPLERATE:samplerate, shout.SHOUT_AI_CHANNELS:channels} 
    self.s.name = name 
    self.s.url = station_url 
    self.s.mount = channel_mount 
    self.s.port = port 
    self.ogv = ogv 
    self.s.password = password 
    self.s.genre = genre 
    self.music_directory = music_directory 
    self.s.description = description 
    self.s.host = hostname 
    self.s.format = music_format #using mp3 but it can also be ogg vorbis 
    print self.s.open() 
    threading.Thread.__init__ (self) 

#checking directories for files to stream 
    def scan_directories(self): 
     self.files_array = glob(self.music_directory+"/*.[mM][Pp]3") + glob(self.music_directory+"/*/*.[mM][Pp]3") + glob(self.music_directory+"/*/*/*.[mM][Pp]3") #checks the specified directory down to the third depth 
     print str(len(self.files_array))+" files" #display number of matching files found 
     shuffle(self.files_array) # randomize playlist 

    def run (self): 
     while 1: #infinity 
     self.scan_directories() # rescan dir, maybe in time you add some new songs 
    self.song_counter = 0 
    for e in self.files_array: 
      self.write_future() 
      self.sendfile(e) 
      self.song_counter = self.song_counter + 1 

    def format_songname(self,song): # format song name - on filename (strip "mp3", change _ to " ". Formatting name of song for writing into a text file 
     result = song.split("/")[-1].split(".") 
     result = ".".join(result[:len(result)-1]).replace("_"," ").replace("-"," - ") 
    return result 

    def write_future(self): #write playlist 
     filename = self.s.mount.replace("/","")+"-current.txt" 
     fa = open(filename,"w") 
     aid = self.song_counter 
     pos = 7 # CHANGE if you want more songs in future playlist 
     for s in self.files_array[aid:]: 
     fa.write(self.format_songname(s)+"\n") 
     pos = pos - 1 
     if (pos==0): 
      break 
     if (pos>0): 
     for s in self.files_array[:pos+1]: 
      fa.write(self.format_songname(s)+"\n") 
     fa.close() 

    def sendfile(self,fa): 
     print "opening file %s" % fa 
     f = open(fa) 
     self.s.set_metadata({'song': self.format_songname(fa)}) 
     nbuf = f.read(4096) 
     while 1: 
     buf = nbuf 
     nbuf = f.read(4096) 
     if len(buf) == 0: 
      break 
     self.s.send(buf) 
     self.s.sync() 
     f.close() 

#running the first stream 
RunStream(channel_mount = "/stream", music_directory = "/home/CUWebRadio1/music_one", station_url = "http://webradio.com", genre = "new",name = "Web Radio Channel2", description = "bla bla bla").start() 

#running the second stream 
RunStream(channel_mount = "/stream_2", music_directory = "/home/CUWebRadio1/music_twos", station_url = "http://webradio.com", genre = "music",name = "Web Radio Music", description = "bla bla bla").start() 

#running the Third Stream 
RunStream(channel_mount = "/stream_3", music_directory = "/home/CUWebRadio1/music_three", station_url = "http://webradio.com", genre = "Music",name = "CU Web Radio Music3", description = "bla bla bla").start() 

錯誤消息我得到

Traceback (most recent call last): 
    File "new_threads.py", line 96, in <module> 
    RunStream(channel_mount = "/stream_3", music_directory = "/home/CUWebRadio1/music_three", station_url = "http://webradio.com", genre = Music",name = "CU Web Radio Music3", description = "bla bla bla").start() 
    File "new_threads.py", line 37, in __init__ 
    print self.s.open() 
shout.ShoutException: Login failed 

任何幫助將不勝感激。

回答

2

所以,事實證明我的代碼是正確的。問題出在Ice​​cast的配置中。默認情況下,只能創建兩個流。在icecast配置文件中編輯完成後,它一切正常。謝謝。