325 lines
18 KiB
Python
325 lines
18 KiB
Python
"""长视频合成引擎接口(图1 模块④):时间线编排、配音、字幕、混音、
|
||
质检报告、合成与导出(图2 节点 10~12)。"""
|
||
from __future__ import annotations
|
||
|
||
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
||
from fastapi.responses import FileResponse
|
||
from sqlalchemy import select
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app import schemas
|
||
from app.api import deps
|
||
from app.constants import AssetKind, AudioTrackKind, Transition
|
||
from app.db import get_db
|
||
from app.models import ExportRecord, Timeline, TimelineSegment
|
||
from app.services import daemon, dispatch, events, execution, ffmpeg as ff
|
||
from app.services import storage, status, timeline as timeline_service
|
||
|
||
router = APIRouter(tags=["delivery"])
|
||
|
||
|
||
# ---------------------------------------------------------------- 时间线(图2 节点 11)
|
||
@router.post("/api/projects/{project_id}/timeline")
|
||
def build_timeline(project_id: int, data: schemas.TimelineBuild, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
try:
|
||
result = timeline_service.build_timeline(db, project, name=data.name,
|
||
only_approved=data.only_approved,
|
||
shot_ids=data.shot_ids,
|
||
settings_patch=data.settings or None)
|
||
except ValueError as exc:
|
||
raise HTTPException(400, str(exc)) from exc
|
||
timeline = deps.get_timeline(db, project_id, result["timeline_id"])
|
||
return {"ok": True, "result": {**result, "timeline": status.timeline_dict(timeline)},
|
||
"summary": deps.summary(db, project)}
|
||
|
||
|
||
@router.get("/api/projects/{project_id}/timeline")
|
||
def get_timeline(project_id: int, timeline_id: int | None = None, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id, timeline_id)
|
||
return {"timeline": status.timeline_dict(timeline),
|
||
"report": timeline_service.timeline_report(db, timeline, project=project)}
|
||
|
||
|
||
@router.get("/api/projects/{project_id}/timelines")
|
||
def list_timelines(project_id: int, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
rows = db.scalars(select(Timeline).where(Timeline.project_id == project.id)
|
||
.order_by(Timeline.version.desc())).all()
|
||
return {"timelines": [{"id": t.id, "name": t.name, "version": t.version, "is_current": t.is_current,
|
||
"segments": len(t.segments or []), "total_duration_ms": t.total_duration_ms,
|
||
"updated_at": t.updated_at} for t in rows]}
|
||
|
||
|
||
@router.post("/api/projects/{project_id}/timelines/{timeline_id}/activate")
|
||
def activate_timeline(project_id: int, timeline_id: int, db: Session = Depends(get_db)):
|
||
"""版本管理:把某个历史时间线版本设为「当前」,导出与预检默认走它。"""
|
||
project = deps.get_project(db, project_id)
|
||
target = deps.get_timeline(db, project_id, timeline_id)
|
||
rows = db.scalars(select(Timeline).where(Timeline.project_id == project.id)).all()
|
||
changed = 0
|
||
for row in rows:
|
||
want = row.id == target.id
|
||
if bool(row.is_current) != want:
|
||
row.is_current = want
|
||
changed += 1
|
||
db.commit()
|
||
events.log_event(db, "timeline.activated", project_id=project.id,
|
||
message=f"时间线切换为 v{target.version}「{target.name}」",
|
||
entity_type="timeline", entity_id=target.id, level="info")
|
||
return deps.done(db, project, {"id": target.id, "version": target.version,
|
||
"is_current": True, "changed": changed})
|
||
|
||
|
||
@router.delete("/api/projects/{project_id}/timelines/{timeline_id}")
|
||
def delete_timeline(project_id: int, timeline_id: int, db: Session = Depends(get_db)):
|
||
"""版本管理:删除历史时间线版本(只删编排记录,不动已出片的媒体文件)。"""
|
||
project = deps.get_project(db, project_id)
|
||
target = deps.get_timeline(db, project_id, timeline_id)
|
||
rows = db.scalars(select(Timeline).where(Timeline.project_id == project.id)
|
||
.order_by(Timeline.version.desc())).all()
|
||
if len(rows) <= 1:
|
||
raise HTTPException(400, "项目只剩一个时间线版本,不能删除;可先构建新版本")
|
||
was_current = bool(target.is_current)
|
||
rest = [row for row in rows if row.id != target.id]
|
||
if was_current:
|
||
for row in rest:
|
||
row.is_current = False
|
||
rest[0].is_current = True
|
||
db.delete(target)
|
||
db.commit()
|
||
events.log_event(db, "timeline.deleted", project_id=project.id,
|
||
message=f"删除时间线版本 v{target.version}「{target.name}」"
|
||
+ (",已自动切换到 v%s" % rest[0].version if was_current else ""),
|
||
entity_type="timeline", entity_id=target.id, level="info")
|
||
return deps.done(db, project, {"deleted": target.id, "version": target.version,
|
||
"remaining": len(rest),
|
||
"current_version": rest[0].version if was_current else None})
|
||
|
||
|
||
@router.patch("/api/projects/{project_id}/timeline/segments/{segment_id}")
|
||
def patch_segment(project_id: int, segment_id: int, data: schemas.SegmentPatch, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
segment = db.get(TimelineSegment, segment_id)
|
||
if segment is None:
|
||
raise HTTPException(404, "片段不存在")
|
||
timeline = deps.get_timeline(db, project_id, segment.timeline_id)
|
||
patch = data.model_dump(exclude_unset=True)
|
||
if not patch:
|
||
raise HTTPException(400, "没有需要更新的字段")
|
||
if patch.get("transition_out") and patch["transition_out"] not in Transition.ALL:
|
||
raise HTTPException(400, f"不支持的转场:{patch['transition_out']},可选 "
|
||
+ ", ".join(Transition.ALL))
|
||
result = timeline_service.update_segment(db, segment, project=project, **patch)
|
||
events.log_event(db, "timeline.segment_updated", project_id=project.id,
|
||
message=f"调整时间线片段 #{segment_id}", entity_type="timeline", entity_id=timeline.id,
|
||
data={"segment_id": segment_id, **patch})
|
||
return deps.done(db, project, {"segment_id": segment_id, "recompute": result,
|
||
"timeline": status.timeline_dict(deps.get_timeline(db, project_id, timeline.id))})
|
||
|
||
|
||
@router.post("/api/projects/{project_id}/timeline/reorder")
|
||
def reorder_timeline(project_id: int, data: schemas.Reorder, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id)
|
||
result = timeline_service.reorder_segments(db, timeline, data.shot_ids, project=project)
|
||
events.log_event(db, "timeline.reordered", project_id=project.id, message="调整时间线片段顺序",
|
||
entity_type="timeline", entity_id=timeline.id, data={"order": data.shot_ids})
|
||
return deps.done(db, project, {"recompute": result,
|
||
"timeline": status.timeline_dict(deps.get_timeline(db, project_id, timeline.id))})
|
||
|
||
|
||
@router.post("/api/projects/{project_id}/timeline/subtitles")
|
||
def patch_subtitle(project_id: int, data: schemas.SubtitlePatch, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id)
|
||
if data.shot_id:
|
||
deps.get_shot(db, project_id, data.shot_id)
|
||
try:
|
||
cue = timeline_service.set_subtitle(db, timeline, shot_id=data.shot_id, text=data.text,
|
||
start_ms=data.start_ms, end_ms=data.end_ms,
|
||
speaker=data.speaker, cue_id=data.cue_id)
|
||
except ValueError as exc:
|
||
raise HTTPException(400, str(exc)) from exc
|
||
events.log_event(db, "timeline.subtitle_updated", project_id=project.id,
|
||
message=f"更新字幕(镜头 {data.shot_id or '全局'})", entity_type="timeline",
|
||
entity_id=timeline.id, data={"text": data.text})
|
||
return deps.done(db, project, {"cue": {"id": cue.id, "shot_id": cue.shot_id, "start_ms": cue.start_ms,
|
||
"end_ms": cue.end_ms, "text": cue.text, "speaker": cue.speaker},
|
||
"timeline": status.timeline_dict(deps.get_timeline(db, project_id, timeline.id))})
|
||
|
||
|
||
@router.post("/api/projects/{project_id}/timeline/audio")
|
||
async def add_timeline_audio(project_id: int, kind: str = Form(AudioTrackKind.BGM), label: str = Form(""),
|
||
start_ms: int = Form(0), duration_ms: int = Form(0), gain_db: float = Form(0.0),
|
||
loop: bool = Form(False), ducking: bool = Form(True),
|
||
shot_id: int | None = Form(None), source_path: str = Form(""),
|
||
file: UploadFile | None = File(None), db: Session = Depends(get_db)):
|
||
"""追加音轨:上传音频文件,或引用已有素材的相对路径。"""
|
||
project = deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id)
|
||
if kind not in (AudioTrackKind.VOICEOVER, AudioTrackKind.BGM, AudioTrackKind.SFX):
|
||
raise HTTPException(400, f"不支持的音轨类型:{kind}")
|
||
path = source_path
|
||
if file is not None and file.filename:
|
||
info = await storage.save_upload(file, project_id=project.id,
|
||
kind=kind if kind in (AssetKind.BGM, AssetKind.SFX) else AssetKind.SFX,
|
||
stem=f"timeline-{kind}")
|
||
path = info["path"]
|
||
storage.register_asset(db, project_id=project.id, owner_type=AssetKind.PROJECT, owner_id=project.id,
|
||
kind=kind if kind in (AssetKind.BGM, AssetKind.SFX) else AssetKind.SFX,
|
||
path=path, name=label or info["original_name"], source="upload", info=info)
|
||
db.commit()
|
||
if not path:
|
||
raise HTTPException(400, "请上传音频文件或提供 source_path")
|
||
if shot_id:
|
||
deps.get_shot(db, project_id, shot_id)
|
||
clip = timeline_service.add_audio_clip(db, timeline, kind=kind, source_path=path, label=label,
|
||
start_ms=start_ms, duration_ms=duration_ms, gain_db=gain_db,
|
||
loop=loop, ducking=ducking, shot_id=shot_id)
|
||
events.log_event(db, "timeline.audio_added", project_id=project.id, message=f"新增{kind}音轨:{clip.label}",
|
||
entity_type="timeline", entity_id=timeline.id, data={"kind": kind, "path": path})
|
||
return deps.done(db, project, {"clip_id": clip.id,
|
||
"timeline": status.timeline_dict(deps.get_timeline(db, project_id, timeline.id))})
|
||
|
||
|
||
@router.patch("/api/projects/{project_id}/timeline/settings")
|
||
def patch_timeline_settings(project_id: int, data: schemas.TimelineSettingsPatch, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id)
|
||
previous = dict(timeline.settings_json or {})
|
||
merged = {**previous, **(data.settings or {})}
|
||
timeline.settings_json = merged
|
||
db.flush()
|
||
if str(merged.get("voice_source") or "") != str(previous.get("voice_source") or ""):
|
||
# 语音来源换的是「整条配音轨」(模型原声 ↔ 平台 TTS),片段长度和入点都得重排,
|
||
# 只重算时间会让原声镜头继续按 TTS 时长定格补帧——音画错位就是这么留下的。
|
||
shot_ids = [seg.shot_id for seg in (timeline.segments or []) if seg.shot_id]
|
||
rebuilt = timeline_service.build_timeline(
|
||
db, project, name=timeline.name, only_approved=False,
|
||
shot_ids=shot_ids or None, settings_patch=merged, actor="studio")
|
||
result = {"rebuilt": True, **rebuilt}
|
||
current_id = rebuilt.get("timeline_id") or timeline.id
|
||
else:
|
||
result = timeline_service.recompute_timings(db, timeline, project=project)
|
||
current_id = timeline.id
|
||
db.commit()
|
||
return deps.done(db, project, {"settings": deps.get_timeline(db, project_id, current_id).settings_json or {},
|
||
"recompute": result,
|
||
"timeline": status.timeline_dict(deps.get_timeline(db, project_id, current_id))})
|
||
|
||
|
||
@router.get("/api/projects/{project_id}/timeline/report")
|
||
def timeline_report(project_id: int, db: Session = Depends(get_db)):
|
||
deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id)
|
||
return {"report": timeline_service.timeline_report(db, timeline, project=deps.get_project(db, project_id))}
|
||
|
||
|
||
@router.get("/api/projects/{project_id}/timeline/subtitles")
|
||
def download_subtitles(project_id: int, fmt: str = "srt", db: Session = Depends(get_db)):
|
||
"""导出字幕文件(srt / ass)。"""
|
||
project = deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id)
|
||
cues = [{"start_ms": c.start_ms, "end_ms": c.end_ms, "text": c.text, "speaker": c.speaker}
|
||
for c in (timeline.subtitles or [])]
|
||
if fmt not in ("srt", "ass"):
|
||
raise HTTPException(400, "字幕格式只支持 srt / ass")
|
||
conf = timeline.settings_json or {}
|
||
if fmt == "ass":
|
||
target = storage.to_absolute(f"media/{project.id}/exports/timeline-{timeline.id}.ass")
|
||
path = ff.write_ass(cues, target, width=timeline.width, height=timeline.height,
|
||
style=conf.get("subtitle_style") or {})
|
||
else:
|
||
target = storage.to_absolute(f"media/{project.id}/exports/timeline-{timeline.id}.srt")
|
||
path = ff.write_srt(cues, target)
|
||
if path is None:
|
||
raise HTTPException(404, "时间线还没有可导出的字幕")
|
||
return FileResponse(path, media_type="text/plain; charset=utf-8",
|
||
filename=f"project-{project_id}-timeline-{timeline.id}.{fmt}")
|
||
|
||
|
||
# ---------------------------------------------------------------- 配音(图2 节点 11 子流程)
|
||
@router.post("/api/projects/{project_id}/voiceover")
|
||
def create_voiceovers(project_id: int, data: schemas.VoiceoverRequest, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
shots = list(project.shots or [])
|
||
if data.shot_ids:
|
||
wanted = set(data.shot_ids)
|
||
shots = [s for s in shots if s.id in wanted]
|
||
created = []
|
||
for shot in shots:
|
||
if not (shot.dialogue or "").strip():
|
||
continue
|
||
if data.only_missing and shot.voiceover_path and storage.exists(shot.voiceover_path):
|
||
continue
|
||
task = dispatch.new_voiceover_task(db, project, shot, priority=data.priority)
|
||
created.append({"task_id": task.id, "shot_id": shot.id})
|
||
return deps.done(db, project, {"created": created, "count": len(created)})
|
||
|
||
|
||
# ---------------------------------------------------------------- 合成与导出(图2 节点 11~12)
|
||
@router.post("/api/projects/{project_id}/compose")
|
||
def compose_project(project_id: int, data: schemas.ComposeRequest, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
timeline = deps.get_timeline(db, project_id, data.timeline_id)
|
||
if data.preset not in ff.EXPORT_PRESETS:
|
||
raise HTTPException(400, f"不支持的导出预设:{data.preset},可选 {', '.join(ff.EXPORT_PRESETS)}")
|
||
report = timeline_service.timeline_report(db, timeline, project=project)
|
||
if not report["ready"]:
|
||
errors = [i["message"] for i in report["issues"] if i["level"] == "error"]
|
||
raise HTTPException(409, "时间线存在阻塞问题:" + ";".join(errors[:5]))
|
||
task = dispatch.new_compose_task(db, project, timeline.id, preset=data.preset, name=data.name,
|
||
burn_subtitle=data.burn_subtitle,
|
||
subtitle_mode=data.subtitle_mode, priority=data.priority)
|
||
drained = None
|
||
if execution.backend_name() == "inline":
|
||
drained = daemon.drain_queue(db, project_id=project.id, timeout=1800)
|
||
# 队列任务由独立会话落库:本会话缓存必须失效,否则回读的是执行前的旧值
|
||
db.expire_all()
|
||
exports = db.scalars(select(ExportRecord).where(ExportRecord.task_id == task.id)
|
||
.order_by(ExportRecord.id.desc())).all()
|
||
return deps.done(db, project, {"task_id": task.id, "timeline_id": timeline.id, "preset": data.preset,
|
||
"drained": drained,
|
||
"exports": [_export_dict(e) for e in exports]})
|
||
|
||
|
||
@router.get("/api/projects/{project_id}/exports")
|
||
def list_exports(project_id: int, db: Session = Depends(get_db)):
|
||
project = deps.get_project(db, project_id)
|
||
rows = db.scalars(select(ExportRecord).where(ExportRecord.project_id == project.id)
|
||
.order_by(ExportRecord.id.desc())).all()
|
||
return {"exports": [_export_dict(e) for e in rows], "count": len(rows)}
|
||
|
||
|
||
@router.get("/api/exports/{export_id}")
|
||
def get_export(export_id: int, db: Session = Depends(get_db)):
|
||
rec = db.get(ExportRecord, export_id)
|
||
if rec is None:
|
||
raise HTTPException(404, "导出记录不存在")
|
||
return {"export": _export_dict(rec)}
|
||
|
||
|
||
@router.get("/api/exports/{export_id}/download")
|
||
def download_export(export_id: int, db: Session = Depends(get_db)):
|
||
rec = db.get(ExportRecord, export_id)
|
||
if rec is None:
|
||
raise HTTPException(404, "导出记录不存在")
|
||
if rec.status != "succeeded" or not rec.path or not storage.exists(rec.path):
|
||
raise HTTPException(409, f"成片尚未就绪(状态 {rec.status})")
|
||
absolute = storage.to_absolute(rec.path)
|
||
suffix = absolute.suffix or ".mp4"
|
||
return FileResponse(absolute, media_type="video/mp4",
|
||
filename=f"{rec.name or 'video'}-{rec.preset}{suffix}")
|
||
|
||
|
||
def _export_dict(rec: ExportRecord) -> dict:
|
||
return {"id": rec.id, "project_id": rec.project_id, "timeline_id": rec.timeline_id, "task_id": rec.task_id,
|
||
"name": rec.name, "preset": rec.preset, "status": rec.status, "path": rec.path,
|
||
"url": storage.url_for(rec.path), "size_bytes": rec.size_bytes, "duration_ms": rec.duration_ms,
|
||
"width": rec.width, "height": rec.height, "fps": rec.fps, "checksum": rec.checksum,
|
||
"subtitle_burned": rec.subtitle_burned, "error_message": rec.error_message,
|
||
"started_at": rec.started_at, "finished_at": rec.finished_at, "created_at": rec.created_at,
|
||
"elapsed_ms": status.elapsed_ms(rec.started_at, rec.finished_at)}
|