问题描述
我没有找到任何使用 pyttsx3 的方法,所以我将语音存储在一个文件中 然后尝试使用诱变剂获取音频信息
import pyttsx3
from mutagen.mp3 import MP3
# the engine
engine = pyttsx3.init()
# 'Hello World' is just an example
engine.save_to_file('Hello world','test.mp3')
engine.runAndWait()
# load the mp3 as an audio
audio = MP3('test.mp3')
# the line above gives an error
我收到以下错误 mutagen.mp3.HeaderNotFoundError: can't sync to mpeg frame
为什么我会收到这个错误? 还有其他方法可以获取 pyttsx 生成的语音的长度吗?
解决方法
如果没有特别需要使用mutagen
,我推荐使用pydub
。下面的代码以秒为单位给出持续时间
代码:
import pyttsx3
from pydub import AudioSegment
# the engine
engine = pyttsx3.init()
# 'Hello World' is just an example
engine.save_to_file('Hello world','test.mp3')
engine.runAndWait()
# load the mp3 as an audio
audio = AudioSegment.from_file("test.mp3")
print(audio.duration_seconds)
输出:
0.9205442176870748