-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dev v2 #67
Closed
Dev v2 #67
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8eb961b
feat(app): 新增脚本生成 V2 接口并重构相关功能
linyqh 45fae0b
feat(v2): 新增视频裁剪和YouTube视频下载功能
linyqh 58773d6
feat(v2): 添加开始视频剪辑任务的 API 接口- 新增 StartSubclipRequest 和 StartSubclipRe…
linyqh b34d9fe
refactor(webui): 优化音频设置界面并添加代理配置
linyqh 38f2398
refactor(video_pipeline): 重构视频处理管道
linyqh 1be304a
feat(subtitle): 添加从视频提取音频并生成字幕的功能
linyqh f3248ef
feat(test): 添加与通义千问AI模型对话的功能
linyqh 86d398d
feat(audio): 改进音频合并功能,支持 OST 设置,提升时间戳精度
linyqh 73729dc
feat(utils): 优化时间戳处理并支持毫秒级精度
linyqh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from fastapi import APIRouter, Depends | ||
|
||
|
||
def v2_router(dependencies=None): | ||
router = APIRouter() | ||
router.tags = ["V2"] | ||
router.prefix = "/api/v2" | ||
# 将认证依赖项应用于所有路由 | ||
if dependencies: | ||
router.dependencies = dependencies | ||
return router | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段代码定义了一个函数 bug 风险:
改进建议:
改进后的代码示例: from fastapi import APIRouter, Depends
from typing import List, Optional
def v2_router(dependencies: Optional[List[Depends]] = None) -> APIRouter:
"""
创建一个 FastAPI 路由器,应用于 V2 API。
Args:
dependencies (List[Depends], optional): 认证依赖项列表。 Defaults to None.
Returns:
APIRouter: FastAPI 路由器实例。
"""
try:
router = APIRouter()
router.tags = ["V2"]
router.prefix = "/api/v2"
if dependencies:
router.dependencies = dependencies
return router
except Exception as e:
# 添加错误处理和日志记录
print(f"Error creating V2 router: {e}")
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
from fastapi import APIRouter, BackgroundTasks | ||
from loguru import logger | ||
import os | ||
|
||
from app.models.schema_v2 import ( | ||
GenerateScriptRequest, | ||
GenerateScriptResponse, | ||
CropVideoRequest, | ||
CropVideoResponse, | ||
DownloadVideoRequest, | ||
DownloadVideoResponse, | ||
StartSubclipRequest, | ||
StartSubclipResponse | ||
) | ||
from app.models.schema import VideoClipParams | ||
from app.services.script_service import ScriptGenerator | ||
from app.services.video_service import VideoService | ||
from app.utils import utils | ||
from app.controllers.v2.base import v2_router | ||
from app.models.schema import VideoClipParams | ||
from app.services.youtube_service import YoutubeService | ||
from app.services import task as task_service | ||
|
||
router = v2_router() | ||
|
||
|
||
@router.post( | ||
"/scripts/generate", | ||
response_model=GenerateScriptResponse, | ||
summary="同步请求;生成视频脚本 (V2)" | ||
) | ||
async def generate_script( | ||
request: GenerateScriptRequest, | ||
background_tasks: BackgroundTasks | ||
): | ||
""" | ||
生成视频脚本的V2版本API | ||
""" | ||
task_id = utils.get_uuid() | ||
|
||
try: | ||
generator = ScriptGenerator() | ||
script = await generator.generate_script( | ||
video_path=request.video_path, | ||
video_theme=request.video_theme, | ||
custom_prompt=request.custom_prompt, | ||
skip_seconds=request.skip_seconds, | ||
threshold=request.threshold, | ||
vision_batch_size=request.vision_batch_size, | ||
vision_llm_provider=request.vision_llm_provider | ||
) | ||
|
||
return { | ||
"task_id": task_id, | ||
"script": script | ||
} | ||
|
||
except Exception as e: | ||
logger.exception(f"Generate script failed: {str(e)}") | ||
raise | ||
|
||
|
||
@router.post( | ||
"/scripts/crop", | ||
response_model=CropVideoResponse, | ||
summary="同步请求;裁剪视频 (V2)" | ||
) | ||
async def crop_video( | ||
request: CropVideoRequest, | ||
background_tasks: BackgroundTasks | ||
): | ||
""" | ||
根据脚本裁剪视频的V2版本API | ||
""" | ||
try: | ||
# 调用视频裁剪服务 | ||
video_service = VideoService() | ||
task_id, subclip_videos = await video_service.crop_video( | ||
video_path=request.video_origin_path, | ||
video_script=request.video_script | ||
) | ||
logger.debug(f"裁剪视频成功,视频片段路径: {subclip_videos}") | ||
logger.debug(type(subclip_videos)) | ||
return { | ||
"task_id": task_id, | ||
"subclip_videos": subclip_videos | ||
} | ||
|
||
except Exception as e: | ||
logger.exception(f"Crop video failed: {str(e)}") | ||
raise | ||
|
||
|
||
@router.post( | ||
"/youtube/download", | ||
response_model=DownloadVideoResponse, | ||
summary="同步请求;下载YouTube视频 (V2)" | ||
) | ||
async def download_youtube_video( | ||
request: DownloadVideoRequest, | ||
background_tasks: BackgroundTasks | ||
): | ||
""" | ||
下载指定分辨率的YouTube视频 | ||
""" | ||
try: | ||
youtube_service = YoutubeService() | ||
task_id, output_path, filename = await youtube_service.download_video( | ||
url=request.url, | ||
resolution=request.resolution, | ||
output_format=request.output_format, | ||
rename=request.rename | ||
) | ||
|
||
return { | ||
"task_id": task_id, | ||
"output_path": output_path, | ||
"resolution": request.resolution, | ||
"format": request.output_format, | ||
"filename": filename | ||
} | ||
|
||
except Exception as e: | ||
logger.exception(f"Download YouTube video failed: {str(e)}") | ||
raise | ||
|
||
|
||
@router.post( | ||
"/scripts/start-subclip", | ||
response_model=StartSubclipResponse, | ||
summary="异步请求;开始视频剪辑任务 (V2)" | ||
) | ||
async def start_subclip( | ||
request: VideoClipParams, | ||
task_id: str, | ||
subclip_videos: dict, | ||
background_tasks: BackgroundTasks | ||
): | ||
""" | ||
开始视频剪辑任务的V2版本API | ||
""" | ||
try: | ||
# 构建参数对象 | ||
params = VideoClipParams( | ||
video_origin_path=request.video_origin_path, | ||
video_clip_json_path=request.video_clip_json_path, | ||
voice_name=request.voice_name, | ||
voice_rate=request.voice_rate, | ||
voice_pitch=request.voice_pitch, | ||
subtitle_enabled=request.subtitle_enabled, | ||
video_aspect=request.video_aspect, | ||
n_threads=request.n_threads | ||
) | ||
|
||
# 在后台任务中执行视频剪辑 | ||
background_tasks.add_task( | ||
task_service.start_subclip, | ||
task_id=task_id, | ||
params=params, | ||
subclip_path_videos=subclip_videos | ||
) | ||
|
||
return { | ||
"task_id": task_id, | ||
"state": "PROCESSING" # 初始状态 | ||
} | ||
|
||
except Exception as e: | ||
logger.exception(f"Start subclip task failed: {str(e)}") | ||
raise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这是一个使用FastAPI框架的API代码。以下是一些代码评审的建议和发现的潜在问题:
以下是一些具体的代码优化建议:
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
该代码看起来是一个 FastAPI 应用,提供了几个 API 端点用于管理视频和音乐文件。以下是一些代码审查的结果:
注释和文档
代码中缺乏注释和文档,这对于代码的维护和理解是很有帮助的。建议添加注释和文档来解释代码的目的和功能。
错误处理
代码中有一些错误处理,但是它并不是很完善。例如,在
upload_bgm_file
函数中,如果文件上传失败,会抛出一个HttpException
,但是这个错误信息并不详细。建议增加详细的错误信息和错误处理机制。文件路径处理
代码中使用了
os.path.join
来处理文件路径,这是正确的。但是,建议使用pathlib
库来处理文件路径,因为它更方便和安全。文件类型检查
在
upload_bgm_file
函数中,只检查了文件扩展名是否为.mp3
,但是并没有检查文件的类型是否是真正的音频文件。建议增加文件类型检查来确保文件是真正的音频文件。视频流和下载
视频流和下载功能实现了,但是建议优化这些功能来提高性能和安全性。例如,可以使用
StreamingResponse
来实现视频流,而不需要将整个视频文件加载到内存中。文件上传
文件上传功能实现了,但是建议增加文件上传的大小限制和文件类型限制来防止恶意用户上传很大的文件或非允许的文件类型。
路由
路由设计得比较混乱,建议重新设计路由来提高代码的可读性和维护性。例如,可以使用子路由来分组相关的 API 端点。
测试
代码中缺乏测试,这对于代码的可靠性和维护性是很重要的。建议增加测试来确保代码的正确性和稳定性。
其他
代码中有一些重复的代码,建议提取出重复的代码来提高代码的可读性和维护性。