Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fixed: asyncio.run() cannot be called from a running event loop

harryb5ba1e6b0906
diff --git a/app/controllers/v1/video.py b/app/controllers/v1/video.py
index 0848094e..084439c4 100644
--- a/app/controllers/v1/video.py
+++ b/app/controllers/v1/video.py
@@ -26,7 +26,7 @@ async def create_video(request: Request, body: TaskVideoRequest):
}
body_dict = body.dict()
task.update(body_dict)
- result = tm.start(task_id=task_id, params=body)
+ result = await tm.start(task_id=task_id, params=body)
task["result"] = result
logger.success(f"video created: {utils.to_json(task)}")
return utils.get_response(200, task)
diff --git a/app/services/task.py b/app/services/task.py
index 079556bd..7c8faa44 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -21,7 +21,7 @@ def _parse_voice(name: str):
return _voice, _lang
-def start(task_id, params: VideoParams):
+async def start(task_id, params: VideoParams):
"""
{
"video_subject": "",
@@ -60,7 +60,7 @@ def start(task_id, params: VideoParams):
subtitle_path = path.join(utils.task_dir(task_id), f"subtitle.srt")
logger.info("\n\n## generating audio")
- sub_maker = voice.tts(text=script, voice_name=voice_name, voice_file=audio_file)
+ sub_maker =await voice.tts(text=script, voice_name=voice_name, voice_file=audio_file)
subtitle_provider = config.app.get("subtitle_provider", "").strip().lower()
logger.info(f"\n\n## generating subtitle, provider: {subtitle_provider}")
diff --git a/app/services/voice.py b/app/services/voice.py
index b6d8336f..10db25d0 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1,4 +1,5 @@
import asyncio
+from concurrent.futures import ThreadPoolExecutor
from xml.sax.saxutils import unescape
from edge_tts.submaker import mktimestamp
from loguru import logger
@@ -7,7 +8,7 @@ import edge_tts
from app.utils import utils
-def tts(text: str, voice_name: str, voice_file: str) -> SubMaker:
+async def tts(text: str, voice_name: str, voice_file: str) -> SubMaker:
logger.info(f"start, voice name: {voice_name}")
async def _do() -> SubMaker:
@@ -21,7 +22,7 @@ def tts(text: str, voice_name: str, voice_file: str) -> SubMaker:
sub_maker.create_sub((chunk["offset"], chunk["duration"]), chunk["text"])
return sub_maker
- sub_maker = asyncio.run(_do())
+ sub_maker = await _do()
logger.info(f"completed, output file: {voice_file}")
return sub_maker
@@ -78,24 +79,33 @@ def create_subtitle(sub_maker: submaker.SubMaker, text: str, subtitle_file: str)
if __name__ == "__main__":
- temp_dir = utils.storage_dir("temp")
-
- voice_names = [
- # 女性
- "zh-CN-XiaoxiaoNeural",
- "zh-CN-XiaoyiNeural",
- # 男性
- "zh-CN-YunyangNeural",
- "zh-CN-YunxiNeural",
- ]
- text = """
-预计未来3天深圳冷空气活动频繁,未来两天持续阴天有小雨,出门带好雨具;
-10-11日持续阴天有小雨,日温差小,气温在13-17℃之间,体感阴凉;
-12日天气短暂好转,早晚清凉;
- """
- for voice_name in voice_names:
- voice_file = f"{temp_dir}/tts-{voice_name}.mp3"
- subtitle_file = f"{temp_dir}/tts.mp3.srt"
- sub_maker = tts(text=text, voice_name=voice_name, voice_file=voice_file)
- create_subtitle(sub_maker=sub_maker, text=text, subtitle_file=subtitle_file)
+ async def _do():
+ temp_dir = utils.storage_dir("temp")
+
+ voice_names = [
+ # 女性
+ "zh-CN-XiaoxiaoNeural",
+ "zh-CN-XiaoyiNeural",
+ # 男性
+ "zh-CN-YunyangNeural",
+ "zh-CN-YunxiNeural",
+ ]
+ text = """
+ 预计未来3天深圳冷空气活动频繁,未来两天持续阴天有小雨,出门带好雨具;
+ 10-11日持续阴天有小雨,日温差小,气温在13-17℃之间,体感阴凉;
+ 12日天气短暂好转,早晚清凉;
+ """
+
+ for voice_name in voice_names:
+ voice_file = f"{temp_dir}/tts-{voice_name}.mp3"
+ subtitle_file = f"{temp_dir}/tts.mp3.srt"
+ sub_maker = await tts(text=text, voice_name=voice_name, voice_file=voice_file)
+ create_subtitle(sub_maker=sub_maker, text=text, subtitle_file=subtitle_file)
+
+
+ loop = asyncio.get_event_loop_policy().get_event_loop()
+ try:
+ loop.run_until_complete(_do())
+ finally:
+ loop.close()
diff --git a/webui/Main.py b/webui/Main.py
index 7cf1617b..4b50d3c1 100644
--- a/webui/Main.py
+++ b/webui/Main.py
@@ -1,3 +1,4 @@
+import asyncio
import sys
import os
from uuid import uuid4
@@ -150,4 +151,4 @@ if start_button:
logger.info("开始生成视频")
- tm.start(task_id=task_id, params=cfg)
+ asyncio.run(tm.start(task_id=task_id, params=cfg))