在python中使用gts的文本到语音对话



我想用gtts制作一个不同方言的人之间的对话。我的逻辑很简单,对话框在一个列表中,如果它从person 1开始,那么它就会说话并把它作为mp3放在mp3_files中。然后我会把它们组合成一个mp3。我一直得到一个错误,我不知道如何继续前进。

import os
from gtts import gTTS
from moviepy.editor import *
import moviepy.editor as mp
from pydub import AudioSegment
# Dialog between two people
dialog = "Person 1: Hi, how are you? nPerson 2: I'm good, thanks for asking. How about you? nPerson 1: I'm great, thanks!"
# Split the dialog into separate lines
lines = dialog.split("n")
# Create an empty list to store the mp3 files
mp3_files = []
# Convert each line of dialog to speech and save as mp3
for line in lines:
if "Person 1" in line:
# Use a male voice for Person 1
tts = gTTS(text=line, lang='en', slow=False)
tts.save("temp1.mp3")
mp3_files.append("temp1.mp3")
elif "Person 2" in line:
# Use a french voice for Person 2
tts = gTTS(text=line, lang='fr', slow=False)
tts.save("temp2.mp3")
mp3_files.append("temp2.mp3")
# Create an empty audio segment
combined = AudioSegment.empty()
# Iterate through the list of mp3 files and concatenate them
for file in mp3_files:
combined += AudioSegment.from_file(file, format="mp3")
# Export the combined audio segment to a new mp3 file
combined.export("dialog.mp3", format="mp3")
# remove the temp files
for file in mp3_files:
os.remove(file)

错误:

PS C:UsersBattleShipDesktopwork> & C:/Users/BattleShip/AppData/Local/Microsoft/WindowsApps/python3.9.exe c:/Users/BattleShip/Desktop/work/Testing1.py
Traceback (most recent call last):
File "c:UsersBattleShipDesktopworkTesting1.py", line 33, in <module>
combined += AudioSegment.from_file(file, format="mp3")
File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespydubaudio_segment.py", line 728, in from_file
info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
File "C:UsersBattleShipAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespydubutils.py", line 274, in mediainfo_json
res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0libsubprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0libsubprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

请勿删除mp3文件它找不到mp3文件,因为您正在删除mp3_files中的mp3文件再试一次,注释最后两行:

#for file in mp3_files:
#    os.remove(file)

相关内容

  • 没有找到相关文章

最新更新