jiaaro / pydub

Manipulate audio with a simple and easy high level interface
http://pydub.com
MIT License
8.98k stars 1.05k forks source link

JSONDecodeError with AudioSegment when reading from bytesio #770

Open Bloctans opened 10 months ago

Bloctans commented 10 months ago

Steps to reproduce

put this code in a script:

from pydub import AudioSegment
from io import BytesIO

real_audio = BytesIO(open("a.mp3", "rb").read())
real_audio.seek(0)

audio = AudioSegment.from_file(real_audio)

make sure to put the attached audio file in the same folder and run the script

Expected behavior

The script should not error??

Actual behavior

When i run the script i get this:

Traceback (most recent call last):
  File "C:\Users\MYUSERNAME\Documents\GitHub\WZBot\tests\pydubtest.py", line 7, in <module>
    audio = AudioSegment.from_file(real_audio)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python12\Lib\site-packages\pydub\audio_segment.py", line 728, in from_file
    info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python12\Lib\site-packages\pydub\utils.py", line 279, in mediainfo_json
    info = json.loads(output)
           ^^^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python12\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python12\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Program Files\Python12\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Your System configuration

Is there an audio file you can include to help us reproduce?

You can include the audio file in this issue - just put it in a zip file and drag/drop the zip file into the github issue. a.zip

my workaround would be to take the raw bytes and put them into a temp folder as file, running it and then removing the file I have tried just inputting the file directly and it works, however i cannot do this for my use case of pydub (unless there is no choice but to use the workaround i listed above) (most likely not an issue with ffmpeg)