import pyttsx3 import requests from openai import OpenAI import openai import random import os import time import json import threading from itertools import chain from gtts import gTTS import os client = OpenAI() def txt_to_speach(text): # text_list_1 = text.replace(" ","").replace(",",",").split("。") text_list = text.replace(" ","").replace(",",",").split("。") filename = f"static/tts/mp3/output{random.randint(1,25)}.mp3" filenames = [] text_list = [item.split(',') if len(item) > 30 else [item] for item in text_list ] text_list = list(chain.from_iterable(text_list )) # 建立存放執行序的list(存放thread) threads = [] # 放入執行序 for i,text_split in enumerate(text_list): text_split = text_split.strip() # 檢查字串是否為空 if not text_split: continue t = threading.Thread(target=text_split_to_text, args=(text_split,i,filename)) threads.append(t) # 將程序放入threads filenames.append(f"/home/mia/101/{filename}-{i}.mp3") print(filenames) # 開始 for t in threads: t.start() # 等待所有子執行緒結束 for t in threads: t.join() merge_audio_files(filenames, f"/home/mia/101/{filename}") return filename,text_list def text_split_to_text(text_split,i,filename): response = client.audio.speech.create( model="tts-1", voice="nova", input=text_split ) filename_tmp = f"/home/mia/101/{filename}-{i}.mp3" response.stream_to_file(filename_tmp) import subprocess from pydub import AudioSegment from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips def merge_audio_files(files, output_file): # 生成 ffmpeg 的命令 cmd = ['ffmpeg', '-i', 'concat:' + '|'.join(files), '-c', 'copy', '-y',output_file] # 执行命令 subprocess.run(cmd) # 刪除暫時生成的音頻文件 for filename in files: os.remove(filename) # combined = AudioSegment.empty() # # 逐一載入每個音頻文件並合併 # for file in files: # audio = AudioSegment.from_file(file, format="mp3") # combined += audio # # 將合併後的音頻保存為新文件 # combined.export(output_file, format="mp3") def download_voice(text,voice="zh-TW-HsiaoChenNeural", pronunciations=None): output_url = f"static/tts/mp/output{random.randint(1,25)}.mp3" output = "/home/mia/101/" + output_url my_data = { "voice": voice, "content": [str(text)] #["你好,很高興認識你","喜歡","討厭"] # "ssml": string[] # "title": string, // Optional # "narrationStyle": string, // Optional # "globalSpeed": string, // Optional # "pronunciations": { key: string, value: string }[], // Optional # "trimSilence": boolean, // Optional } headers = { # 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36', "Authorization":"61ddf2a47cdd42548671be21ccdcf285", "X-User-ID":'HEQLQR1WgpYtN0SEyKoWBsLiZXX2', "Content-Type": "application/json" } start_time = time.time() # 將資料加入 POST 請求中 r = requests.post('https://play.ht/api/v1/convert',headers=headers,data=json.dumps(my_data)) c1 = r.json() print(c1) c1 = r.json()['transcriptionId'] # print(c1) time.sleep(len(text)) success_flag = False r ='' while True: r = requests.post('https://play.ht/api/v1/convert',headers=headers,data=json.dumps(my_data)) c1 = r.json()['transcriptionId'] print(f"{text}:{c1}") # time.sleep(0.5+(len(text)/4)) counter = 0 while True: r = requests.get('https://play.ht/api/v1/articleStatus?transcriptionId=%s'%c1, headers=headers) if 'json' not in r.headers.get('content-type') or r.json()['converted'] == False: print(f"audio {c1} is not ready.") # time.sleep(0.5) counter += 1 if counter == 6: break else: success_flag = True break if success_flag: break else: print('redownload') file = requests.get(r.json()['audioUrl']) with open(output,"wb") as f: for chunk in file.iter_content(chunk_size=1024): if chunk: f.write(chunk) end_time = time.time() execution_time = end_time - start_time print("reply time:", execution_time, "s") return output_url,execution_time