Search code examples
pythonffmpegtypeerrorpydub

TypeError a bytes-like object is required, not 'str' (Pydub)


I'm getting:

Exception has occurred: TypeError
a bytes-like object is required, not 'str'

When I run the following code:

from pydub import AudioSegment
from pydub.utils import which

AudioSegment.converter = which('ffmpeg')
AudioSegment.ffmpeg = r"C:\PATH_Programs\bin\ffmpeg.exe"

audio = AudioSegment(r'C:\Users\jack_l\Documents\Cm - 120 BPM.wav') #ERROR HERE

What I know:

  • I have ffmpeg on my path (also set in code)
  • I'm running python 3.9.13 and pydub version 0.25.1
  • Error happens with or without the AudioSegment.converter and AudioSegment.ffmpeg lines – I just have them there to show I've tried that.

What am I doing wrong? Thanks


Solution

  • Error message seems pretty straightforward : AudioSegment class constructor is expecting a bytes object and you are passing a String instead.

    According to the pydub doc, you could simply call from_wav() method by passing your filepath as a string.

    Example (from the doc) :

    song = AudioSegment.from_wav("never_gonna_give_you_up.wav")