영상생성 로직 및 검색 방식 추가

This commit is contained in:
skkim committed 2026-07-07 13:16:46 +09:00
1 parent b8be2a355a
commit 76485ebbcc
3 files changed
+171 -33

No files matched your search

+32 -3
View File
@@ -35,6 +35,33 @@ logger = get_logger("song")
router = APIRouter(prefix="/song", tags=["Song"])
TARGET_SONG_DURATION_SECONDS = 60.0
def _select_clip_by_duration(
clips_data: list[dict], target_seconds: float = TARGET_SONG_DURATION_SECONDS
) -> dict:
"""생성된 클립 중 목표 길이(target_seconds)에 가장 가까운 클립을 선택합니다.
길이 차이가 동일하면 먼저 등장한 클립(더 낮은 인덱스)을 선택합니다.
duration 정보가 없는 클립은 비교 대상에서 제외되며, 모든 클립에 duration이 없으면
첫 번째 클립을 반환합니다.
"""
best_clip = None
best_diff = None
for clip in clips_data:
duration = clip.get("duration")
if duration is None:
continue
diff = abs(duration - target_seconds)
if best_diff is None or diff < best_diff:
best_diff = diff
best_clip = clip
return best_clip if best_clip is not None else clips_data[0]
@router.post(
"/generate/{task_id}",
@@ -413,12 +440,14 @@ async def get_song_status(
clips_data = response_data.get("sunoData") or []
if clips_data:
# 첫 번째 클립(clips[0])의 audioUrl과 duration 사용
first_clip = clips_data[0]
# 생성된 클립(보통 2개) 중 목표 길이(1분)에 가장 가까운 클립 선택 (동일하면 첫 번째)
first_clip = _select_clip_by_duration(clips_data)
audio_url = first_clip.get("audioUrl")
clip_duration = first_clip.get("duration")
logger.debug(
f"[get_song_status] Using first clip - id: {first_clip.get('id')}, audio_url: {audio_url}, duration: {clip_duration}"
f"[get_song_status] Selected clip by duration - id: {first_clip.get('id')}, "
f"audio_url: {audio_url}, duration: {clip_duration}, "
f"candidates: {[(c.get('id'), c.get('duration')) for c in clips_data]}"
)
if audio_url: