完善fastapi接口

This commit is contained in:
puke
2025-11-05 19:46:47 +08:00
parent eee604d8e9
commit 15899afb6f
11 changed files with 595 additions and 56 deletions

73
api/routers/frame.py Normal file
View File

@@ -0,0 +1,73 @@
"""
Frame/Template rendering endpoints
"""
from fastapi import APIRouter, HTTPException
from loguru import logger
from api.dependencies import PixelleVideoDep
from api.schemas.frame import FrameRenderRequest, FrameRenderResponse
from pixelle_video.services.frame_html import HTMLFrameGenerator
from pixelle_video.utils.template_util import parse_template_size, resolve_template_path
router = APIRouter(prefix="/frame", tags=["Frame Rendering"])
@router.post("/render", response_model=FrameRenderResponse)
async def render_frame(
request: FrameRenderRequest,
pixelle_video: PixelleVideoDep
):
"""
Render a single frame using HTML template
Generates a frame image by combining template, title, text, and image.
This is useful for previewing templates or generating custom frames.
- **template**: Template key (e.g., '1080x1920/default.html')
- **title**: Optional title text
- **text**: Frame text content
- **image**: Image path (can be local path or URL)
Returns path to generated frame image.
Example:
```json
{
"template": "1080x1920/modern.html",
"title": "Welcome",
"text": "This is a beautiful frame with custom styling",
"image": "resources/example.png"
}
```
"""
try:
logger.info(f"Frame render request: template={request.template}")
# Resolve template path (handles both "default.html" and "1080x1920/default.html")
template_path = resolve_template_path(request.template)
full_template_path = f"templates/{template_path}"
# Parse template size
width, height = parse_template_size(full_template_path)
# Create HTML frame generator
generator = HTMLFrameGenerator(full_template_path)
# Generate frame
frame_path = await generator.generate_frame(
title=request.title,
text=request.text,
image=request.image
)
return FrameRenderResponse(
frame_path=frame_path,
width=width,
height=height
)
except Exception as e:
logger.error(f"Frame render error: {e}")
raise HTTPException(status_code=500, detail=str(e))