本文介绍了在 Python 中将合成的文本转语音录制到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来获取合成语音并将其录制到音频文件中.我目前使用 pyttsx 作为我的文本到语音库,但没有机制将输出保存到文件中,仅直接从扬声器播放.我研究了检测和录制音频以及PyAudio,但这些似乎从麦克风获取输入,而不是将传出的音频重定向到文件.有没有已知的方法可以做到这一点?

I am attempting to find a way to take synthesized speech and record it to an audio file. I am currently using pyttsx as my text-to-speech library, but there isn't a mechanism for saving the output to a file, only playing it directly from the speakers. I've looked into detecting and recording audio as well as PyAudio, but these seem to take input from a microphone rather than redirecting outgoing audio to a file. Is there a known way to do this?

推荐答案

您可以通过以下方式调用 espeak -w 参数使用 subprocess.

You can call espeak with the -w argument using subprocess.

import subprocess

def textToWav(text,file_name):
   subprocess.call(["espeak", "-w"+file_name+".wav", text])

textToWav('hello world','hello')

这将写入 file_name.wav 而不大声朗读.如果您的文本在文件中(例如 text.txt),您需要使用 -f 参数(-f"+text)调用 espeak.我建议您阅读 espeak 手册页以查看您拥有的所有选项.

This will write file_name.wav without reading out loud. If your text is in a file (e.g. text.txt) you need to call espeak with the -f parameter ("-f"+text). I'd recommend reading the espeak man pages to see all the options you have.

希望这会有所帮助.

这篇关于在 Python 中将合成的文本转语音录制到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 14:33