creating multiple audio streams of an icecast2 server using python-shout

2024/11/10 13:03:46

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,choiceclass 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 icecastglobal hostname,port,passwordself.song_conter= 0self.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 = nameself.s.url = station_urlself.s.mount = channel_mountself.s.port = portself.ogv = ogvself.s.password = passwordself.s.genre = genreself.music_directory = music_directoryself.s.description = descriptionself.s.host = hostnameself.s.format = music_format #using mp3 but it can also be ogg vorbisprint self.s.open()threading.Thread.__init__ (self)#checking directories for files to streamdef 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 depthprint str(len(self.files_array))+" files" #display number of matching files foundshuffle(self.files_array) # randomize playlistdef run (self):while 1: #infinityself.scan_directories() # rescan dir, maybe in time you add some new songsself.song_counter = 0   for e in self.files_array:self.write_future()self.sendfile(e)self.song_counter = self.song_counter + 1def format_songname(self,song): # format song name - on filename (strip "mp3", change _ to " ". Formatting name of song for writing into a text fileresult = song.split("/")[-1].split(".")result = ".".join(result[:len(result)-1]).replace("_"," ").replace("-"," - ")return resultdef write_future(self): #write playlistfilename = self.s.mount.replace("/","")+"-current.txt"fa = open(filename,"w")aid = self.song_counterpos = 7 # CHANGE if you want more songs in future playlistfor s in self.files_array[aid:]:fa.write(self.format_songname(s)+"\n")pos = pos - 1if (pos==0):breakif (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" % faf = open(fa)self.s.set_metadata({'song': self.format_songname(fa)})nbuf = f.read(4096)while 1:buf = nbufnbuf = f.read(4096)if len(buf) == 0:breakself.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.

Answer

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.

https://en.xdnf.cn/q/72179.html

Related Q&A

Custom Deployment to Azure Websites

I recently started using Gulp.js to package all my CSS and JavaScript into single files, which I then include in my web app. My web app is written in Python (using Flask).I obviously dont want to track…

Python - Gspread Request Error 401

Im currently making a Discord-bot that connects to a Google-spreadsheet (gspread). But after Ive been running it for a while it starts to hand out errors and it cant connect to my gspread anymore (unle…

paramiko server mode port forwarding

I need to implement a ssh server using paramiko that only handles -R port forwarding requests like this:ssh -N -T -R 40005:destination_host:22 [email protected]So far from what i understand ill have to…

Pandas dataframe.hist() change title size on subplot?

I am manipulating DataFrame using pandas, Python. My data is 10000(rows) X 20(columns) and I am visualizing it, like this.df.hist(figsize=(150,150))However, if I make figsize bigger, each of subplots t…

Regex match back to a period or start of string

Id like to match a word, then get everything before it up to the first occurance of a period or the start of the string. For example, given this string and searching for the word "regex":s = …

Finding differences between strings

I have the following function that gets a source and a modified strings, and bolds the changed words in it.def appendBoldChanges(s1, s2):"Adds <b></b> tags to words that are changed&qu…

Python pandas: select 2nd smallest value in groupby

I have an example DataFrame like the following:import pandas as pd import numpy as np df = pd.DataFrame({ID:[1,2,2,2,3,3,], date:array([2000-01-01,2002-01-01,2010-01-01,2003-01-01,2004-01-01,2008-01-01…

How to disable SSL3 and weak ciphers with cherrypy builtin ssl module (python 3)

I have configured Cherrypy 3.8.0 with Python 3 to use SSL/TLS. However, I want to disable SSL3 to avoid POODLE. I searched through the documentation but I am unsure on how to implement it.I am using th…

cleaning big data using python

I have to clean a input data file in python. Due to typo error, the datafield may have strings instead of numbers. I would like to identify all fields which are a string and fill these with NaN using p…

Using the Python shell in Vi mode on Windows

I know that you can use the Python shell in Vi mode on Unix-like operating systems. For example, I have this line in my ~/.inputrc:set editing-mode viThis lets me use Vi-style editing inside the Python…