尝试将 AudioSegment 用于 .wav 文件时出错

Error trying to use AudioSegment for .wav files

我正在尝试遍历文件夹“audios”中的所有 .wav 文件,但收到以下错误。我发现安装 ffmpeg 解决了类似的问题,但没有帮助。

    FileNotFoundError                         Traceback (most recent call last)
    <ipython-input-24-29ba732186ac> in <module>
          1 for audio_file in os.listdir(base_path+"audios"):
          2     # read wav audio file
    ----> 3     audio = AudioSegment.from_wav(audio_file)
          4 
          5     # pass audio file, start time, end time & chunk path to create chunk
    
    ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\audio_segment.py in from_wav(cls, file, parameters)
        806     @classmethod
        807     def from_wav(cls, file, parameters=None):
    --> 808         return cls.from_file(file, 'wav', parameters=parameters)
        809 
        810     @classmethod
    
    ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\audio_segment.py in from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs)
        649         except TypeError:
        650             filename = None
    --> 651         file, close_file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
        652 
        653         if format:
    
    ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pydub\utils.py in _fd_or_path_or_tempfile(fd, mode, tempfile)
         58 
         59     if isinstance(fd, basestring):
    ---> 60         fd = open(fd, mode=mode)
         61         close_fd = True
         62 

FileNotFoundError: [Errno 2] No such file or directory: 'name_of_file.wav'

os.listdir 不会 return 您提供的目录中文件的完整路径,只是它们在该目录中的名称。您需要将此目录名称添加到您传递给 AudioSegment.from_wav.

的文件名之前

尝试替换行

     audio = AudioSegment.from_wav(audio_file)

     audio = AudioSegment.from_wav(os.path.join(base_path+"audios", audio_file))