Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
95558ab
chore: 자막 추출용 youtubei.js 의존성 추가
Dino0204 Jun 29, 2026
a2e2977
feat: 자막 데이터 검증용 web-llm 의존성 추가
Dino0204 Jun 29, 2026
7052355
feat: AI 분석 권한 설정 및 쿼리 추가
Dino0204 Jun 29, 2026
61f839b
feat: 유튜브 자막 가져오기
Dino0204 Jun 29, 2026
38ddc15
feat: 메인 스레드 중단 방지용 llm worker
Dino0204 Jun 29, 2026
4c059e9
change: 한국어를 우선순위로 변경
Dino0204 Jun 29, 2026
633d655
feat: 기상음악 훅에 권한 설정
Dino0204 Jun 29, 2026
a83edd9
refactor: 기상음악 추천 기능을 wake-up-music-recommend로 분리
Dino0204 Jun 29, 2026
41a6163
refactor: 기상음악 AI 분석 모듈을 wake-up-music-analysis로 구성
Dino0204 Jun 29, 2026
3c1e5c9
refactor: 기상음악 AI 분석 표시 컴포넌트 분리
Dino0204 Jun 29, 2026
af2da85
refactor: 음악 리스트 아이템에 선택·AI 상태 슬롯 추가
Dino0204 Jun 29, 2026
d77dfca
feat: 기상음악 크게 보기에 AI 분석·인앱 플레이어 추가
Dino0204 Jun 29, 2026
ee884a7
change: 기상음악 권한·훅 정리 (사감 AI 분석 제한, 미사용 날짜 헬퍼 제거)
Dino0204 Jun 29, 2026
a09f3de
fix: 기상음악 자막·캐시·선택표시 리뷰 반영
Dino0204 Jun 29, 2026
ea58171
fix: 플레이어 너비 제한
Dino0204 Jun 29, 2026
f76ed7f
fear: AI 면책 문구 추가
Dino0204 Jun 29, 2026
822e22c
feat: 자동 생성 자막 감지 및 배지 표시
Dino0204 Jun 30, 2026
cb2fa23
change: NoteText 다중 줄 옵션 추가
Dino0204 Jun 30, 2026
e02fdcd
refactor: AI 분석 결과 패널을 상태별 컴포넌트로 분리
Dino0204 Jun 30, 2026
38f8ed0
refactor: AI 모델 로드 상태를 단일 출처로 분리
Dino0204 Jun 30, 2026
55b379e
change: 크게 보기 플레이어 UI 개선
Dino0204 Jun 30, 2026
7dc1f7f
chore: 포맷
Dino0204 Jun 30, 2026
3a27c74
feat: yt-dlp docker image
Dino0204 Jun 30, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/(main)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Header } from "@/widgets/header/ui/header";
import { AiChatButton } from "@/features/ai-chat/ui/AiChatButton";
import { AiChatModal } from "@/features/ai-chat/ui/AiChatModal";
import { useAiChatPanel } from "@/features/ai-chat/model/useAiChatPanel";
import { WakeUpMusicRecommend } from "@/features/wake-up-music/ui/WakeUpMusicRecommend";
import { WakeUpMusicRecommend } from "@/features/wake-up-music-recommend/ui/WakeUpMusicRecommend";
import { RealtimeSubscriptions } from "@/widgets/realtime-subscription/ui/RealtimeSubscriptions";

export default function MainLayout({
Expand Down
41 changes: 41 additions & 0 deletions app/api/youtube/transcript/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { HttpStatusCode } from "axios";
import { NextRequest, NextResponse } from "next/server";
import { extractYoutubeVideoId } from "@/entities/music/lib/youtube";
import { getYoutubeTranscript } from "@/entities/music/lib/transcript";

export const runtime = "nodejs";

export async function GET(request: NextRequest) {
const authorization = request.headers.get("authorization");
const accessToken = authorization?.startsWith("Bearer ")
? authorization.slice("Bearer ".length)
: null;

if (!accessToken) {
return NextResponse.json(
{ error: "access token 없음" },
{ status: HttpStatusCode.Unauthorized },
);
}
Comment thread
Dino0204 marked this conversation as resolved.

const params = request.nextUrl.searchParams;
const urlParam = params.get("url");
const idParam = params.get("id");
const videoId =
idParam ?? (urlParam ? extractYoutubeVideoId(urlParam) : null);

if (!videoId) {
return NextResponse.json(
{ error: "videoId(id) 또는 url이 필요합니다." },
{ status: HttpStatusCode.BadRequest },
);
}
Comment thread
Dino0204 marked this conversation as resolved.

const result = await getYoutubeTranscript(videoId);

if ("error" in result) {
return NextResponse.json(result, { status: HttpStatusCode.NotFound });
}

return NextResponse.json(result);
}
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"format:check": "prettier --check --ignore-unknown ."
},
"dependencies": {
"@mlc-ai/web-llm": "^0.2.84",
"@sentry/nextjs": "^10.53.1",
"@sun-typeface/suit": "^2.0.5",
"@tanstack/react-query": "^5.90.20",
Expand Down
7 changes: 7 additions & 0 deletions src/entities/ai/lib/webllm.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { WebWorkerMLCEngineHandler } from "@mlc-ai/web-llm";

const handler = new WebWorkerMLCEngineHandler();

self.onmessage = (event: MessageEvent) => {
handler.onmessage(event);
};
28 changes: 28 additions & 0 deletions src/entities/ai/lib/webllmEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { InitProgressReport, WebWorkerMLCEngine } from "@mlc-ai/web-llm";

export const MUSIC_LLM_MODEL_ID = "gemma-2-2b-it-q4f16_1-MLC";

let enginePromise: Promise<WebWorkerMLCEngine> | null = null;

export function getMusicLlmEngine(
onProgress?: (report: InitProgressReport) => void,
): Promise<WebWorkerMLCEngine> {
if (!enginePromise) {
enginePromise = (async () => {
const { CreateWebWorkerMLCEngine } = await import("@mlc-ai/web-llm");
const worker = new Worker(
new URL("./webllm.worker.ts", import.meta.url),
{ type: "module" },
);

return CreateWebWorkerMLCEngine(worker, MUSIC_LLM_MODEL_ID, {
initProgressCallback: onProgress,
});
})().catch((error) => {
enginePromise = null;
throw error;
});
}

return enginePromise;
}
16 changes: 14 additions & 2 deletions src/entities/dormitory/lib/musicPermission.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import type { UserRole } from "@/entities/user/model/user";

// 기자위(DORMITORY_MANAGER) 중 AI 분석을 쓸 수 있는 사감 계정 이름
const DORM_SUPERVISOR_NAME = "사감선생님";

interface CreateMusicPermissionParams {
role?: UserRole;
name?: string;
}

export function createMusicPermission({ role }: CreateMusicPermissionParams) {
export function createMusicPermission({
role,
name,
}: CreateMusicPermissionParams) {
const isDormManager = role === "DORMITORY_MANAGER";

return {
canDeleteAnyMusic: role === "ADMIN" || role === "DORMITORY_MANAGER",
canDeleteAnyMusic: role === "ADMIN" || isDormManager,
// 어드민은 모두 가능, 기자위는 사감선생님만 가능
canUseAiAnalysis:
role === "ADMIN" || (isDormManager && name === DORM_SUPERVISOR_NAME),
Comment thread
Dino0204 marked this conversation as resolved.
};
}
32 changes: 32 additions & 0 deletions src/entities/music/api/youtubeQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ interface YoutubeVideosResponse {
videos: YoutubeVideoMetadata[];
}

export interface YoutubeTranscript {
text: string;
}

export async function getYoutubeVideos(
videoIds: string[],
signal?: AbortSignal,
Expand All @@ -35,6 +39,25 @@ export async function getYoutubeVideos(
return Object.fromEntries(data.videos.map((video) => [video.id, video]));
}

const TRANSCRIPT_TIMEOUT_MS = 65_000;

export async function getYoutubeTranscript(
videoId: string,
signal?: AbortSignal,
): Promise<YoutubeTranscript> {
const { data } = await instance.get<YoutubeTranscript>(
"/api/youtube/transcript",
{
baseURL: undefined,
params: { id: videoId },
timeout: TRANSCRIPT_TIMEOUT_MS,
signal,
},
);

return data;
}

export const youtubeQueries = {
videos: (videoIds: string[]) => {
const ids = Array.from(new Set(videoIds)).sort();
Expand All @@ -45,4 +68,13 @@ export const youtubeQueries = {
enabled: ids.length > 0,
});
},

transcript: (videoId: string) =>
queryOptions({
queryKey: ["music", "youtube-transcript", videoId],
queryFn: ({ signal }) => getYoutubeTranscript(videoId, signal),
enabled: Boolean(videoId),
staleTime: Infinity,
gcTime: Infinity,
}),
} as const;
164 changes: 164 additions & 0 deletions src/entities/music/lib/transcript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { execFile } from "node:child_process";
import { mkdtemp, readdir, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { promisify } from "node:util";

const execFileAsync = promisify(execFile);

export interface YoutubeTranscriptSuccess {
text: string;
}

export interface YoutubeTranscriptFailure {
error: string;
}

export type YoutubeTranscriptResult =
| YoutubeTranscriptSuccess
| YoutubeTranscriptFailure;

const PREFERRED_LANGS = ["ko", "en"];
const YT_DLP_CONCURRENCY = Number(process.env.YT_DLP_CONCURRENCY ?? 20);
const YT_DLP_BIN = process.env.YT_DLP_PATH ?? "yt-dlp";
const YT_DLP_TIMEOUT_MS = 60_000;

const transcriptCache = new Map<string, string>();

let activeYtDlp = 0;
const ytDlpWaiters: Array<() => void> = [];

async function withYtDlpLimit<T>(task: () => Promise<T>): Promise<T> {
if (activeYtDlp >= YT_DLP_CONCURRENCY) {
await new Promise<void>((resolve) => ytDlpWaiters.push(resolve));
}
activeYtDlp += 1;
try {
return await task();
} finally {
activeYtDlp -= 1;
ytDlpWaiters.shift()?.();
}
}

function toTimestamp(h: number, m: number, s: number): string {
const total = h * 3600 + m * 60 + s;
const sec = String(total % 60).padStart(2, "0");
const min = Math.floor(total / 60) % 60;
const hr = Math.floor(total / 3600);
return hr > 0
? `${hr}:${String(min).padStart(2, "0")}:${sec}`
: `${min}:${sec}`;
}

function cleanVtt(vtt: string): string {
const entries: string[] = [];
let time = "0:00";
let previous: string | null = null;

for (const rawLine of vtt.split(/\r?\n/)) {
const line = rawLine.trim();
if (
!line ||
line.startsWith("WEBVTT") ||
line.startsWith("Kind:") ||
line.startsWith("Language:") ||
/^\d+$/.test(line)
) {
continue;
}

const cue = line.match(/^(\d{2}):(\d{2}):(\d{2})[.,]\d+\s*-->/);
if (cue) {
time = toTimestamp(Number(cue[1]), Number(cue[2]), Number(cue[3]));
continue;
}

const text = line.replace(/<[^>]+>/g, "").trim();
if (!text || text === previous) continue;
entries.push(`[${time}] ${text}`);
previous = text;
}

return entries.join("\n");
}

function langOf(fileName: string): string {
return fileName.split(".").at(-2) ?? "";
}

async function extractViaYtDlp(
videoId: string,
): Promise<YoutubeTranscriptResult> {
let workDir: string | null = null;
try {
workDir = await mkdtemp(path.join(tmpdir(), "yt-transcript-"));

const args = [
"--skip-download",
"--write-sub",
"--write-auto-sub",
"--sub-lang",
PREFERRED_LANGS.join(","),
"--sub-format",
"vtt",
"--retries",
"3",
"--retry-sleep",
"5",
"--ignore-errors",
"--no-abort-on-error",
"--sleep-subtitles",
"1",
"--no-warnings",
"-o",
path.join(workDir, `${videoId}.%(ext)s`),
`https://www.youtube.com/watch?v=${videoId}`,
];

let rawStderr = "";
try {
const { stderr } = await execFileAsync(YT_DLP_BIN, args, {
timeout: YT_DLP_TIMEOUT_MS,
});
rawStderr = stderr ?? "";
} catch (execError) {
const err = execError as { stderr?: string; message?: string };
rawStderr = err.stderr ?? err.message ?? String(execError);
}

const files = (await readdir(workDir)).filter((f) => f.endsWith(".vtt"));
if (files.length === 0) {
return { error: rawStderr.trim() || "자막 파일 없음(원인 미상)" };
}

const preferredFile =
PREFERRED_LANGS.map((code) => files.find((f) => langOf(f) === code)).find(
Boolean,
) ?? files[0];

const text = cleanVtt(
await readFile(path.join(workDir, preferredFile), "utf-8"),
);
if (!text) return { error: "yt-dlp: 자막 텍스트가 비어 있습니다." };

return { text };
} catch (error) {
return { error: error instanceof Error ? error.message : String(error) };
} finally {
if (workDir) {
await rm(workDir, { recursive: true, force: true }).catch(() => {});
}
}
}

export async function getYoutubeTranscript(
videoId: string,
): Promise<YoutubeTranscriptResult> {
const cached = transcriptCache.get(videoId);
if (cached) return { text: cached };

const result = await withYtDlpLimit(() => extractViaYtDlp(videoId));
if (!("error" in result)) transcriptCache.set(videoId, result.text);
return result;
}
Loading
Loading