|
|
import subprocess
|
|
|
import os
|
|
|
import datetime
|
|
|
from datetime import timedelta
|
|
|
import re
|
|
|
|
|
|
def speak_text_from_file(file_path, voice='zh', speed=150, pitch=50, output_file=None):
|
|
|
"""
|
|
|
从文件中读取文本并使用espeak将其转换为语音。
|
|
|
|
|
|
:param file_path: 文本文件的路径
|
|
|
:param voice: 使用的声音(例如 'zh' 用于中文)
|
|
|
:param speed: 语速(默认为 150)
|
|
|
:param pitch: 音调(默认为 50)
|
|
|
:param output_file: 如果指定,将语音输出保存为WAV文件
|
|
|
"""
|
|
|
with open(file_path, 'r', encoding='utf-8') as file:
|
|
|
text = file.read()
|
|
|
|
|
|
# 构建espeak命令
|
|
|
cmd = ['espeak', '-v', voice, '-s', str(speed), '-p', str(pitch)]
|
|
|
if output_file:
|
|
|
cmd.extend(['-w', output_file])
|
|
|
else:
|
|
|
# 如果没有指定输出文件,则直接播放语音
|
|
|
pass # 这里可以添加其他选项,如音量调整等
|
|
|
|
|
|
# 使用stdin将文本传递给espeak
|
|
|
try:
|
|
|
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
|
|
|
proc.communicate(input=text)
|
|
|
|
|
|
if proc.returncode != 0:
|
|
|
# 获取并打印错误信息
|
|
|
error_message = proc.stderr.read()
|
|
|
print(f"Error executing espeak: {error_message}")
|
|
|
except Exception as e:
|
|
|
print(f"An error occurred: {e}")
|
|
|
|
|
|
# 获取当前文件夹路径
|
|
|
current_dir = os.getcwd()
|
|
|
|
|
|
# 获取父文件夹路径
|
|
|
parent_dir = os.path.dirname(current_dir)
|
|
|
|
|
|
specific_folder_name = "txt"
|
|
|
|
|
|
specific_folder_path = os.path.join(current_dir, specific_folder_name)
|
|
|
|
|
|
# 列出当前文件夹中的所有txt文件
|
|
|
txt_files = [f for f in os.listdir(specific_folder_path) if f.endswith('.txt')]
|
|
|
|
|
|
# 提取文件名中的数字,并找出最大的数字
|
|
|
max_number = -1
|
|
|
max_file = None
|
|
|
for txt_file in txt_files:
|
|
|
# 使用正则表达式提取文件名中的数字
|
|
|
match = re.search(r'^(\d+)\.txt$', txt_file)
|
|
|
if match:
|
|
|
file_number = int(match.group(1))
|
|
|
if file_number > max_number:
|
|
|
max_number = file_number
|
|
|
max_file = txt_file
|
|
|
|
|
|
# 检查是否找到了数字命名最大的txt文件
|
|
|
if max_file:
|
|
|
file_path = os.path.join(current_dir, max_file)
|
|
|
# 调用函数来朗读文本文件
|
|
|
speak_text_from_file(file_path, voice='zh', speed=140, pitch=55)
|
|
|
|
|
|
# 如果你想要将语音保存为WAV文件,可以这样做:
|
|
|
output_wav_file = 'output/output.wav' # WAV文件的输出路径
|
|
|
speak_text_from_file(file_path, voice='zh', speed=140, pitch=55, output_file=output_wav_file)
|
|
|
else:
|
|
|
print("没有找到数字命名最大的txt文件。")
|
|
|
print(specific_folder_path) |