'decode mp3 stream with python streamp3

Code:

from io import BytesIO
import requests
from streamp3 import MP3Decoder
import time
from pydub import AudioSegment
with requests.get("https://impradio.bytemasters.gr/8002/LIVE", stream=True) as peradio:
    for chunk in peradio.iter_content(chunk_size=4096):
        stream = BytesIO(chunk)
        decoder = MP3Decoder(stream)
        for decoded_chunk in decoder:
            slice = AudioSegment(decoded_chunk, sample_width=2, frame_rate=44800, channels=2)
            print(len(decoded_chunk))
            time.sleep(len(slice)/1000)

Output 1:

hip: Can't step back 430 bytes!
4608
4608
4608
4608
Traceback (most recent call last):
  File "g:\streamp3-example.py", line 9, in <module>
    decoder = MP3Decoder(stream)
  File "C:/msys64/mingw64/lib/python3.9/site-packages/streamp3/__init__.py", line 56, in __init__
    if not self._read_frame():
  File "C:/msys64/mingw64/lib/python3.9/site-packages/streamp3/__init__.py", line 210, in _read_frame
    self._pcm_length = self._decoder.decode(self._mp3_buffer,
  File "lame/hip.pyx", line 44, in lame.hip.Hip.decode
OverflowError: can't convert negative value to size_t

Another output:

hip: Can't step back 79 bytes!
4608
4608
4608
4608
Traceback (most recent call last):
  File "/home/chris/Documents/papinhio-player/src/python+/menu-1/retransmitions/preview/decode-mp3.py", line 9, in <module>
    decoder = MP3Decoder(stream)
  File "/home/chris/.local/lib/python3.9/site-packages/streamp3/__init__.py", line 56, in __init__
    if not self._read_frame():
  File "/home/chris/.local/lib/python3.9/site-packages/streamp3/__init__.py", line 184, in _read_frame
    raise Exception("invalid mp3 layer: {0:08b}".format(layer_code))
Exception: invalid mp3 layer: 00000011

I just try to decode a mp3 stream (and then write the decoded chunks to pyaudio output stream).

What i am doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source