Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python read mp3 livestream

import socket
from io import BytesIO
from datetime import datetime

STREAM_URL = "us4.internet-radio.com"
STREAM_PATH = "/proxy/radioestiloleblon?mp=/stream"

BUFFER_SIZE = 1024
PORT = 443

def stream_reader(sock):
    sock.recv(BUFFER_SIZE) # http response
    while data := sock.recv(BUFFER_SIZE):
        print(f"[{datetime.now()}] Received data: {data[:10]}")
        yield data

def get_socket():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((STREAM_URL, PORT))
    s.send(bytes(f"GET {STREAM_PATH}; HTTP/1.1
Host:{STREAM_URL}

", encoding="UTF-8"))
    return s

def write_buffer(filename, bytesio: BytesIO):
    buffer = bytesio.getbuffer()
    with open(filename, "wb") as file:
        file.write(buffer)
    print(f"[{datetime.now()}] Wrote buffer. Size: {buffer.nbytes} bytes")

def main():
    s = get_socket()
    with BytesIO() as bIO:
        try:
            for data in stream_reader(s):
                bIO.write(data)
        except KeyboardInterrupt: pass
        finally: write_buffer("stream.mp3", bytesio=bIO)

if __name__ == "__main__":
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: download kaggle dataset in colab 
Python :: change the color of the button on hovering tkinter 
Python :: python disable warning deprecated 
Python :: pil overlay images 
Python :: django admin image 
Python :: python iterate letters 
Python :: pandas dataframe print decimal places 
Python :: grassmann formula 
Python :: how to duplicate columns pandas 
Python :: run 2 loops simultaneously python 
Python :: python udp receive 
Python :: how to find python version 
Python :: python sorting array without inbuilt sort 
Python :: micropython network 
Python :: colab read xlsx 
Python :: how to set indian timezone in django 
Python :: convert any base to decimal python 
Python :: force utf-8 encoding python 
Python :: check django version 
Python :: python maths max value capped at x 
Python :: pandas find basic statistics on column 
Python :: set axis plt python 
Python :: como deixar todas as letras maiusculas no python 
Python :: cv2 videocapture program for python 
Python :: python move directory 
Python :: add role discord .py 
Python :: binary search algorithm python 
Python :: google colab how to upload a folder 
Python :: sort value_counts output 
Python :: remove empty rows csv python 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =