jiaaro / pydub

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

Calling AudioSegment.export() with an integer bitrate causes Python subprocess exception #784

Open rwmnau opened 6 months ago

rwmnau commented 6 months ago

Steps to reproduce

from pydub import AudioSegment
audio:AudioSegment = AudioSegment.from_file("./anyAudioFile.wav")
audio.export(out_f="./anyAudioFile.mp3", format="mp3", bitrate=50049) # using str(50049) instead works as expected

Expected behavior

Calling AudioSegment.export() with a bitrate value set to an integer (like 50049) should work as expected.

Actual behavior

This integer is passed directly to the Python subprocess.execute_child as an argument and throws an exception ("TypeError: expected str, bytes or os.PathLike object, not int") since the arguments collection only supports those types and doesn't know how to process an int. The .export() definition says it accepts any type for the bitrate parameter, and then extends the args collection for ffmpeg with that parameter in whatever type it's given. Python subprocess.execute_child then throws an exception because it doesn't allow an int arg.

Workaround

This issue can be worked around by converting the bitrate to a string before calling AudioSegment.export(), but this isn't clear in the documentation and could be enforced through the method definition. I'm passing an audio stream from pytube, which hands the bitrate back as an int by default, so it's something developers have to know to do.

Your System configuration

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

Any audio file seems to produce this issue, it's a problem running the ffmpeg command with an integer parameter when Python expects all parameters to be strings/bytes/path.

rwmnau commented 6 months ago

I created a PR for this fix - not sure if I did this correctly and if there's something I missed, please let me know and I'll read and follow the guidance.