Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python read music stream

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 :: python iterate over object fields 
Python :: write list of dicts to csv python 
Python :: playsound 
Python :: how to multiply two tuples in python 
Python :: how to execute a cmd command in python 
Python :: export csv from dataframe python 
Python :: pyaudio install error ubuntu 
Python :: how to iterate through a text file in python 
Python :: how to add up everything in a list python 
Python :: pil image base64 
Python :: print random word py 
Python :: how to find the version of python command linw 
Python :: creating virtual environment python 
Python :: convert two numpy array to pandas dataframe 
Python :: median in python 
Python :: powershell get list of groups and members 
Python :: get request header flask 
Python :: print the number of times that the substring occurs in the given string 
Python :: pandas groupby histogram 
Python :: car in programming python 
Python :: python tkinter quit button 
Python :: round godot 
Python :: python element wise multiplication list 
Python :: urllib.request headers 
Python :: how to find the cube of a number in python 
Python :: how to check which python version is installed 
Python :: python read column data from text file 
Python :: python slice an array 
Python :: play music with time in python 
Python :: pandas concat / merge two dataframe within one dataframe 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =