How do I connect to Icecast with Python?

163 Views Asked by At

For the past few days I've been trying to stream audio from Python to Icecast, but it simply refused connection.

Here's the code:

import requests
from pydub import AudioSegment
import base64

login = "admin"
password = "hackme"
auth = base64.b64encode(f"{login}:{password}".encode()).decode()

# Download and encode an audio file
AudioSegment.converter = "C://ffmpeg/bin/ffmpeg.exe"
AudioSegment.ffprobe = "C://ffmpeg/bin/ffprobe.exe"
audio = AudioSegment.from_file("C:/Users/User/Desktop/Stuff/Coding_Stuff/PythonPlayer/testsong.mp3")
audio = audio.set_frame_rate(44100) # Set the sample rate
audio = audio.set_channels(2) # Set the number of channels
audio = audio.export(format="mp3", bitrate="128k") # Export to MP3 format with a bitrate of 128 kbps

# Preparing data for the request
url = "http://localhost:8000/stream" 
headers = {
     "Content-Type": "audio/mpeg", 
     "Authorization": f"Basic {auth}",
     "User-Agent": "Admin",
     "Ice-Public": "1", 
     "Ice-Name": "TestStream", 
     "Ice-Description": "stream", 
     "Ice-URL": "http://example.com", 
     "Ice-Genre": "1" 
}

# Sending a request to the icecast server
response = requests.put(url, headers=headers, data=audio)

# Checking response status
if response.status_code == 200:
     print("Stream started successfully")
else:
     print("Stream failed: ", response.reason)

# Change stream metadata
metadata_url = "http://localhost:8000/admin/metadata"
metadata_params = {
     "mount": "/stream", 
     "mode": "updinfo", 
     "song": "42" 
}
metadata_response = requests.get(metadata_url, headers=headers, params=metadata_params)

# Checking response status
if metadata_response.status_code == 200:
     print("Metadata updated successfully")
else:
     print("Metadata update failed: ", metadata_response.reason)

Here's the error it gave:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\User\Desktop\Stuff\Coding_Stuff\PythonPlayer\pythonplayer.py", line 31, in <module>
    response = requests.put(url, headers=headers, data=audio)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\requests\api.py", line 130, in put
    return request("put", url, data=data, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\requests\adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

I would greatly appreciate any help you can provide.

I tried to connect to icecast. It didn't connect.

0

There are 0 best solutions below