使用 python-shout 创建icecast2服务器的多个音频流

发布于 2024-10-22 01:47:55 字数 4343 浏览 0 评论 0原文

我正在尝试创建一个网络广播服务器来同时传输 3 个源。我正在使用 python 使用 python-shout 库为icecast2 创建源客户端。我对这种语言不太熟悉(python)。但是,我有一个示例程序,它可以满足我的需要以及更多功能,并且我已经根据我的需要对其进行了调整。但是,我只能创建两个流,之后,我收到如下所示的错误消息。我不知道我做错了什么,所以我希望你们能帮助我解决这个问题。

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

任何帮助将不胜感激。

I am trying to create a web radio server to stream 3 sources at once. I am using python to create a source client for icecast2 using the python-shout library. I am not too familiar with the language (python). however, i got a sample program which does what i need to and more and i have tweaked it for my need. However, I can only create two streams, and after that, i get the error message shown below. I don't know what i'm doing wrong so i hope you guys can help me figure that out.

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()

the Error message i get

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

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的奇迹 2024-10-29 01:47:55

所以,事实证明我的代码是正确的。问题出在 Icecast 的配置中。默认情况下,只能创建两个流。在icecast配置文件中编辑后,一切都完美运行。谢谢。

So, it turns out that my code is correct. The problem was in the configuration for Icecast. By default, only two streams can be created. after editing that in the icecast config file, It all worked perfectly. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文