크레아토 완료
This commit is contained in:
1 parent
586dd5ccc9
commit
52520d770b
6 files changed
+362
-60
No files matched your search
@@ -35,7 +35,7 @@ from app.video.schemas.video_schema import (
|
||||
VideoListItem,
|
||||
VideoRenderData,
|
||||
)
|
||||
from app.video.worker.video_task import download_and_save_video
|
||||
from app.video.worker.video_task import download_and_upload_video_to_blob
|
||||
from app.utils.creatomate import CreatomateService
|
||||
from app.utils.pagination import PaginatedResponse
|
||||
|
||||
@@ -53,7 +53,7 @@ Creatomate API를 통해 영상 생성을 요청합니다.
|
||||
- **task_id**: Project/Lyric/Song의 task_id (필수) - 연관된 프로젝트, 가사, 노래를 조회하는 데 사용
|
||||
|
||||
## 요청 필드
|
||||
- **template_id**: Creatomate 템플릿 ID (필수)
|
||||
- **orientation**: 영상 방향 (horizontal: 가로형, vertical: 세로형, 기본값: vertical) - 선택
|
||||
- **image_urls**: 영상에 사용할 이미지 URL 목록 (필수)
|
||||
- **lyrics**: 영상에 표시할 가사 (필수)
|
||||
- **music_url**: 배경 음악 URL (필수)
|
||||
@@ -68,8 +68,29 @@ Creatomate API를 통해 영상 생성을 요청합니다.
|
||||
```
|
||||
POST /video/generate/019123ab-cdef-7890-abcd-ef1234567890
|
||||
{
|
||||
"template_id": "abc123...",
|
||||
"image_urls": ["https://...", "https://..."],
|
||||
"image_urls": [
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_189/1715688030436xT14o_JPEG/1.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_48/1715688030574wTtQd_JPEG/2.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_92/17156880307484bvpH_JPEG/3.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_7/1715688031000y8Y5q_JPEG/4.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_259/17156880311809wCnY_JPEG/5.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_64/1715688031601oGNsV_JPEG/6.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_175/1715688031657oXc7l_JPEG/7.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_192/1715688031798MbFDj_JPEG/8.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_205/17156880318681JLwX_JPEG/9.jpg",
|
||||
"https://naverbooking-phinf.pstatic.net/20240514_142/1715688031946hhxHz_JPEG/10.jpg"
|
||||
],
|
||||
"lyrics": "가사 내용...",
|
||||
"music_url": "https://ado2mediastoragepublic.blob.core.windows.net/ado2-media-public-access/ado2-media-original/0694e2d8-7ae2-730c-8000-308aacaa582d/song/스테이 머뭄.mp3"
|
||||
}
|
||||
```
|
||||
|
||||
## 가로형 영상 생성 예시
|
||||
```
|
||||
POST /video/generate/019123ab-cdef-7890-abcd-ef1234567890
|
||||
{
|
||||
"orientation": "horizontal",
|
||||
"image_urls": [...],
|
||||
"lyrics": "가사 내용...",
|
||||
"music_url": "https://..."
|
||||
}
|
||||
@@ -95,10 +116,10 @@ async def generate_video(
|
||||
|
||||
1. task_id로 Project, Lyric, Song 조회
|
||||
2. Video 테이블에 초기 데이터 저장 (status: processing)
|
||||
3. Creatomate API 호출
|
||||
3. Creatomate API 호출 (orientation에 따른 템플릿 자동 선택)
|
||||
4. creatomate_render_id 업데이트 후 응답 반환
|
||||
"""
|
||||
print(f"[generate_video] START - task_id: {task_id}, template_id: {request_body.template_id}")
|
||||
print(f"[generate_video] START - task_id: {task_id}, orientation: {request_body.orientation}")
|
||||
try:
|
||||
# 1. task_id로 Project 조회
|
||||
project_result = await session.execute(
|
||||
@@ -158,23 +179,43 @@ async def generate_video(
|
||||
await session.flush() # ID 생성을 위해 flush
|
||||
print(f"[generate_video] Video saved (processing) - task_id: {task_id}")
|
||||
|
||||
# 5. Creatomate API 호출
|
||||
# 5. Creatomate API 호출 (POC 패턴 적용)
|
||||
print(f"[generate_video] Creatomate API generation started - task_id: {task_id}")
|
||||
creatomate_service = CreatomateService()
|
||||
# orientation에 따른 템플릿과 duration 자동 설정
|
||||
creatomate_service = CreatomateService(orientation=request_body.orientation)
|
||||
print(f"[generate_video] Using template_id: {creatomate_service.template_id}, duration: {creatomate_service.target_duration}")
|
||||
|
||||
# 템플릿에 리소스 매핑
|
||||
modifications = creatomate_service.template_connect_resource_blackbox(
|
||||
template_id=request_body.template_id,
|
||||
# 5-1. 템플릿 조회 (비동기, CreatomateService에서 orientation에 맞는 template_id 사용)
|
||||
template = await creatomate_service.get_one_template_data_async(creatomate_service.template_id)
|
||||
print(f"[generate_video] Template fetched - task_id: {task_id}")
|
||||
|
||||
# 5-2. elements에서 리소스 매핑 생성
|
||||
modifications = creatomate_service.elements_connect_resource_blackbox(
|
||||
elements=template["source"]["elements"],
|
||||
image_url_list=request_body.image_urls,
|
||||
lyric=request_body.lyrics,
|
||||
music_url=request_body.music_url,
|
||||
)
|
||||
print(f"[generate_video] Modifications created - task_id: {task_id}")
|
||||
|
||||
# 렌더링 요청
|
||||
render_response = creatomate_service.make_creatomate_call(
|
||||
template_id=request_body.template_id,
|
||||
modifications=modifications,
|
||||
# 5-3. elements 수정
|
||||
new_elements = creatomate_service.modify_element(
|
||||
template["source"]["elements"],
|
||||
modifications,
|
||||
)
|
||||
template["source"]["elements"] = new_elements
|
||||
print(f"[generate_video] Elements modified - task_id: {task_id}")
|
||||
|
||||
# 5-4. duration 확장 (target_duration: 영상 길이)
|
||||
final_template = creatomate_service.extend_template_duration(
|
||||
template,
|
||||
creatomate_service.target_duration,
|
||||
)
|
||||
print(f"[generate_video] Duration extended to {creatomate_service.target_duration}s - task_id: {task_id}")
|
||||
|
||||
# 5-5. 커스텀 렌더링 요청 (비동기)
|
||||
render_response = await creatomate_service.make_creatomate_custom_call_async(
|
||||
final_template["source"],
|
||||
)
|
||||
print(f"[generate_video] Creatomate API response - task_id: {task_id}, response: {render_response}")
|
||||
|
||||
@@ -265,7 +306,7 @@ async def get_video_status(
|
||||
print(f"[get_video_status] START - creatomate_render_id: {creatomate_render_id}")
|
||||
try:
|
||||
creatomate_service = CreatomateService()
|
||||
result = creatomate_service.get_render_status(creatomate_render_id)
|
||||
result = await creatomate_service.get_render_status_async(creatomate_render_id)
|
||||
print(f"[get_video_status] Creatomate API response - creatomate_render_id: {creatomate_render_id}, status: {result.get('status')}")
|
||||
|
||||
status = result.get("status", "unknown")
|
||||
@@ -293,7 +334,8 @@ async def get_video_status(
|
||||
)
|
||||
video = video_result.scalar_one_or_none()
|
||||
|
||||
if video:
|
||||
if video and video.status != "completed":
|
||||
# 이미 완료된 경우 백그라운드 작업 중복 실행 방지
|
||||
# task_id로 Project 조회하여 store_name 가져오기
|
||||
project_result = await session.execute(
|
||||
select(Project).where(Project.id == video.project_id)
|
||||
@@ -302,14 +344,16 @@ async def get_video_status(
|
||||
|
||||
store_name = project.store_name if project else "video"
|
||||
|
||||
# 백그라운드 태스크로 MP4 다운로드 및 DB 업데이트
|
||||
# 백그라운드 태스크로 MP4 다운로드 → Blob 업로드 → DB 업데이트 → 임시 파일 삭제
|
||||
print(f"[get_video_status] Background task args - task_id: {video.task_id}, video_url: {video_url}, store_name: {store_name}")
|
||||
background_tasks.add_task(
|
||||
download_and_save_video,
|
||||
download_and_upload_video_to_blob,
|
||||
task_id=video.task_id,
|
||||
video_url=video_url,
|
||||
store_name=store_name,
|
||||
)
|
||||
elif video and video.status == "completed":
|
||||
print(f"[get_video_status] SKIPPED - Video already completed, creatomate_render_id: {creatomate_render_id}")
|
||||
|
||||
render_data = VideoRenderData(
|
||||
id=result.get("id"),
|
||||
@@ -344,7 +388,7 @@ async def get_video_status(
|
||||
|
||||
@router.get(
|
||||
"/download/{task_id}",
|
||||
summary="영상 다운로드 상태 조회",
|
||||
summary="영상 생성 URL 조회",
|
||||
description="""
|
||||
task_id를 기반으로 Video 테이블의 상태를 polling하고,
|
||||
completed인 경우 Project 정보와 영상 URL을 반환합니다.
|
||||
|
||||
Reference in new issue
Block a user