浏览代码

mod text2zip

tomoya 3 周之前
父节点
当前提交
378848db93
共有 2 个文件被更改,包括 22 次插入5 次删除
  1. 13 3
      backend/app/app/api/api_v1/endpoints/text2zip.py
  2. 9 2
      backend/app/app/core/video_utils.py

+ 13 - 3
backend/app/app/api/api_v1/endpoints/text2zip.py

@@ -15,8 +15,6 @@ from app.utils import send_new_account_email
 from pydantic import BaseModel
 import requests
 from random import choice
-import string
-import json
 import os
 import PIL.Image
 from gradio_client import Client
@@ -38,6 +36,7 @@ from app.aianchor.utils2 import check_zip, VideoMakerError
 from pathlib import Path
 import emails
 from fastapi import UploadFile, File, Form
+from edge_tts import VoicesManager
 from app.core.video_utils import update_zip
 
 BACKEND_ZIP_STORAGE = Path("/app").joinpath(settings.BACKEND_ZIP_STORAGE)
@@ -362,7 +361,18 @@ def zip_translate(
       error_msg = {"accepted": False, "error_message":f'{e}'}
       return JSONResponse(error_msg)
     path = Path(upload_file.filename)
-    update_zip(str(path), 'zh-TW', str(path.parent/(path.stem+"_"+lang+path.suffix)))
+    local = None
+    if "-" in lang:
+        local = lang
+        lang = lang.split("-")[0]
+    gender = 'Female'
+    voices = asyncio.run(VoicesManager.create())
+    if local:
+        voice = voices.find(Gender=gender, Language=lang, Locale=local)
+        update_zip(str(path), local, str(path.parent/(path.stem+f"_{lang}"+path.suffix)), voice[0]['ShortName'])
+    else:
+        voice = voices.find(Gender=gender, Language=lang)
+        update_zip(str(path), lang, str(path.parent/(path.stem+f"_{lang}"+path.suffix)), voice[0]['ShortName'])
 
     def remove_zip():
             if os.path.exists(str(path.parent/(path.stem+"_"+lang+path.suffix))):

+ 9 - 2
backend/app/app/core/video_utils.py

@@ -8,6 +8,7 @@ import zipfile
 from io import BytesIO
 from translate import Translator
 from chardet.universaldetector import UniversalDetector
+import numpy as np
 
 DEFAULT_ENCODING = "utf-8"
 
@@ -69,14 +70,17 @@ def check_zip(zip_filepath:str):
         if n != 1:
           raise ValueError(f"voice file is can't find is zip at scene {i+1}.")
       
-def update_zip(zip_path, lang):
+def update_zip(zip_path, lang, new_filename, voice):
     temp_zip_path = zip_path + ".tmp"
 
-    with zipfile.ZipFile(zip_path, 'r') as zip_in, zipfile.ZipFile(temp_zip_path, 'w') as zip_out:
+    with zipfile.ZipFile(zip_path, 'r') as zip_in, zipfile.ZipFile(new_filename, 'w') as zip_out:
         for item in zip_in.infolist():
             with zip_in.open(item.filename) as src_file:
                 if item.filename.split('.')[-1] == "xlsx":
                     table = pd.read_excel(src_file, dtype=object)
+                    table['聲音'] = np.NaN
+                    table.loc[0, ['聲音']] = voice
+                    table['發音'] = np.NaN
                     table = translate_table(table, lang)
                     table.to_excel(Path(item.filename).name ,sheet_name='Sheet_name_1')
                     zip_out.write(Path(item.filename).name, item.filename)
@@ -84,6 +88,9 @@ def update_zip(zip_path, lang):
                 elif item.filename.split('.')[-1] == "csv":
                     table = pd.read_csv(src_file, dtype=object)
                     table = translate_table(table, lang)
+                    table['聲音'] = np.NaN
+                    table.loc[0, ['聲音']] = voice
+                    table['發音'] = np.NaN
                     table.to_excel(Path(item.filename).name ,sheet_name='Sheet_name_1')
                     zip_out.write(Path(item.filename).name, item.filename)
                     os.remove(Path(item.filename).name)