3

我已成功导入 pydub,但代码如下:

from pydub import AudioSegment
song = AudioSegment.from_mp3("c:\mks.mp3")
first_ten_seconds = song[:10000]
song.export("d:\mks.mp3", format="mp3")

但它给出了以下错误:

python "C:\Users\mKs\Desktop\mks2.py"
Process started >>>
Traceback (most recent call last):
  File "C:\Users\mKs\Desktop\mks2.py", line 2, in <module>
    song=AudioSegment.from_mp3("c:\mks.mp3");
  File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 194, in from_mp3
    return cls.from_file(file, 'mp3')
  File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 189, in from_file
    return cls.from_wav(output)
  File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 206, in from_wav
    return cls(data=file)
  File "C:\Python27\lib\site-packages\pydub-0.5.2-py2.7.egg\pydub\audio_segment.py", line 33, in __init__
    raw = wave.open(StringIO(data), 'rb')
  File "C:\Python27\lib\wave.py", line 498, in open
    return Wave_read(f)
  File "C:\Python27\lib\wave.py", line 163, in __init__
    self.initfp(f)
  File "C:\Python27\lib\wave.py", line 128, in initfp
    self._file = Chunk(file, bigendian = 0)
  File "C:\Python27\lib\chunk.py", line 63, in __init__
    raise EOFError
EOFError

我很想获得有关此主题的帮助

4

1 回答 1

1

我在您的代码中看到的唯一问题是尾随“;” 在最后 3 行的末尾。请删除那些,看看你是否仍然得到错误。

此外,请确保您已安装ffmpeg ( http://www.ffmpeg.org/ )。它是支持所有非 wav 文件格式所必需的。

添加:

我认为您在 python 安装中破坏了模块依赖项。我已经尝试过您在上面使用 python 2.7.2 提供的代码。它对我来说很好:

>>> from pydub import AudioSegment
>>> song = AudioSegment.from_wav('goodbye.wav')
>>> first_ten_seconds = song[:10000]
>>> song.export('goodbye1.wav',format='wav')
<open file 'goodbye1.wav', mode 'wb+' at 0x10cf2b270>
于 2013-06-03T13:36:10.057 回答