연령제한 곡 재생: 유튜브가 퍼가기 플레이어에서 막는(오류 150) 연령제한 영상은 서버가 성인 인증된 계정 쿠키로 소리만 받아 두고(/data/audio, yt-dlp) 게임은 그 파일을 숨긴 <audio>로 재생. 찾기는 소리 크기·하이라이트 수집(로그인 없는 페이지의 성인 인증 화면) + 게임 중 오류 시 age-audio 작업이 다시 확인해 되돌림. 받기 전에는 출제 안 함. prepare·catchup에 videoId 대신 문제마다 새 무작위 주소(/api/audio/<토큰>, Range 지원)라 영상 id가 안 보임. 같은 시각 시작·위치 정지·새로고침 이어 듣기·소리 크기(유튜브 -14 보정 없이) 그대로. 관리자 수집 화면·cli age-audio, Dockerfile에 yt-dlp(버전·체크섬 고정), 수집 CLI가 서버 DB 경로를 물려받게(테스트가 기본 DB를 건드리던 것). 실제 크롬 e2e로 받은 음원+유튜브 곡 섞어 재생 확인
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
37
apps/server/src/songs/audio.ts
Normal file
37
apps/server/src/songs/audio.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 연령제한 곡 (docs/08 §2-1): YouTube refuses age-restricted videos in every embedded player (error 150), whoever watches.
|
||||
* The collector downloads their audio with the adult-verified account (collector/age-audio.ts); the game plays the file
|
||||
* from here through a random short-lived address, so the page never sees the video id.
|
||||
*/
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { dirname, join } from 'node:path';
|
||||
|
||||
/** Where downloaded audio lives: AUDIO_DIR, else `audio/` next to the DB (the /data volume in the container). */
|
||||
export function audioDir(dbPath: string, env: Record<string, string | undefined> = process.env): string {
|
||||
return env.AUDIO_DIR ?? join(dirname(dbPath), 'audio');
|
||||
}
|
||||
|
||||
export const audioFile = (dir: string, videoId: string) => join(dir, `${videoId}.m4a`);
|
||||
|
||||
export class AudioLinks {
|
||||
private links = new Map<string, { file: string; exp: number }>();
|
||||
constructor(
|
||||
readonly dir: string,
|
||||
private ttlMs = 6 * 3600_000,
|
||||
private now: () => number = Date.now,
|
||||
) {}
|
||||
|
||||
/** A new address for this video's file (one per question; replays and rejoins reuse it). */
|
||||
link(videoId: string): string {
|
||||
const now = this.now();
|
||||
for (const [k, v] of this.links) if (v.exp < now) this.links.delete(k);
|
||||
const token = randomBytes(18).toString('base64url');
|
||||
this.links.set(token, { file: audioFile(this.dir, videoId), exp: now + this.ttlMs });
|
||||
return `/api/audio/${token}`;
|
||||
}
|
||||
|
||||
file(token: string): string | null {
|
||||
const l = this.links.get(token);
|
||||
return l && l.exp >= this.now() ? l.file : null;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
* Which songs a room's settings select (docs/04, docs/12 Q7·Q9).
|
||||
* - 장르: same group "또는", different groups "그리고"; empty = 전체
|
||||
* - 난이도 N: a song qualifies when any applicable scope has level N: 'all', each chosen genre, each decade the year range touches
|
||||
* - 재생 가능: listening modes need a confirmed (matched, embeddable) YouTube video; the lyrics mode needs lyrics
|
||||
* - 재생 가능: listening modes need a confirmed (matched, embeddable) YouTube video — an age-restricted one only once the
|
||||
* server has its audio (연령제한 곡, songs/audio.ts); the lyrics mode needs lyrics
|
||||
*/
|
||||
import type { Database } from 'bun:sqlite';
|
||||
import type { RoomConfig } from '@mq/shared';
|
||||
@@ -46,7 +47,7 @@ function where(db: Database, c: RoomConfig): { sql: string; args: Args } {
|
||||
}
|
||||
|
||||
function playable(c: RoomConfig): string {
|
||||
return c.mode === 'lyrics' ? 'EXISTS (SELECT 1 FROM lyrics y WHERE y.song_id = s.id)' : "s.youtube_status = 'matched' AND s.embeddable = 1";
|
||||
return c.mode === 'lyrics' ? 'EXISTS (SELECT 1 FROM lyrics y WHERE y.song_id = s.id)' : "s.youtube_status = 'matched' AND s.embeddable = 1 AND (COALESCE(s.age_video, '') <> s.youtube_id OR s.audio_video = s.youtube_id)";
|
||||
}
|
||||
|
||||
/** playable = songs a game can use now; total = also those still waiting for a YouTube match (lyrics mode: same). */
|
||||
|
||||
@@ -14,6 +14,9 @@ export interface QuizSong {
|
||||
/** YouTube's loudness of that video (LUFS), null when not measured (collector/loudness.ts). */
|
||||
loudness: number | null;
|
||||
highlightSec: number | null;
|
||||
/** 연령제한: YouTube refuses this video in embedded players; `audio` = the server has its audio file (songs/audio.ts). */
|
||||
ageRestricted: boolean;
|
||||
audio: boolean;
|
||||
genres: string[];
|
||||
/** Level shown on the reveal card: the 'all' scope level when there is one. */
|
||||
level: number | null;
|
||||
@@ -27,9 +30,9 @@ export interface QuizSong {
|
||||
export function quizSong(db: Database, id: number): QuizSong | null {
|
||||
const s = db
|
||||
.query<
|
||||
{ id: number; title: string; artist: string; year: number | null; duration_sec: number | null; youtube_id: string | null; highlight_sec: number | null; youtube_status: string; deleted_at: number | null; loudness_lufs: number | null; loudness_video: string | null },
|
||||
{ id: number; title: string; artist: string; year: number | null; duration_sec: number | null; youtube_id: string | null; highlight_sec: number | null; youtube_status: string; deleted_at: number | null; loudness_lufs: number | null; loudness_video: string | null; age_video: string | null; audio_video: string | null },
|
||||
[number]
|
||||
>('SELECT id, title, artist, year, duration_sec, youtube_id, highlight_sec, youtube_status, deleted_at, loudness_lufs, loudness_video FROM songs WHERE id = ?')
|
||||
>('SELECT id, title, artist, year, duration_sec, youtube_id, highlight_sec, youtube_status, deleted_at, loudness_lufs, loudness_video, age_video, audio_video FROM songs WHERE id = ?')
|
||||
.get(id);
|
||||
if (!s || s.deleted_at) return null;
|
||||
const col = <T>(sql: string) => db.query<{ v: T }, [number]>(sql).all(id).map((r) => r.v);
|
||||
@@ -42,6 +45,8 @@ export function quizSong(db: Database, id: number): QuizSong | null {
|
||||
videoId: s.youtube_status === 'matched' ? s.youtube_id : null,
|
||||
loudness: s.youtube_id && s.loudness_video === s.youtube_id ? s.loudness_lufs : null,
|
||||
highlightSec: s.highlight_sec,
|
||||
ageRestricted: !!s.youtube_id && s.age_video === s.youtube_id,
|
||||
audio: !!s.youtube_id && s.age_video === s.youtube_id && s.audio_video === s.youtube_id,
|
||||
genres: col<string>('SELECT genre_id v FROM song_genres WHERE song_id = ?'),
|
||||
level: db.query<{ level: number }, [number]>("SELECT level FROM song_levels WHERE song_id = ? ORDER BY scope = 'all' DESC LIMIT 1").get(id)?.level ?? null,
|
||||
titleAliases: col<string>("SELECT alias v FROM song_aliases WHERE song_id = ? AND part = 'title'"),
|
||||
@@ -85,12 +90,16 @@ export const YOUTUBE_TARGET_LUFS = -14;
|
||||
*/
|
||||
export const TARGET_LUFS = -18;
|
||||
|
||||
/** A downloaded file (연령제한 곡) whose loudness is unknown: assume a loud modern master. */
|
||||
export const FILE_UNMEASURED_LUFS = -9;
|
||||
|
||||
/**
|
||||
* 소리 크기 맞추기: factor (0..1) for the player volume so this song sounds like TARGET_LUFS — on top of YouTube's own
|
||||
* turn-down. Not measured yet: the gain of a typical song (louder than YouTube's target).
|
||||
* `file`: our own downloaded audio (연령제한 곡) — no YouTube turn-down, so the whole difference is ours.
|
||||
*/
|
||||
export function loudnessGain(lufs: number | null): number {
|
||||
const heard = Math.min(lufs ?? YOUTUBE_TARGET_LUFS, YOUTUBE_TARGET_LUFS);
|
||||
export function loudnessGain(lufs: number | null, file = false): number {
|
||||
const heard = file ? (lufs ?? FILE_UNMEASURED_LUFS) : Math.min(lufs ?? YOUTUBE_TARGET_LUFS, YOUTUBE_TARGET_LUFS);
|
||||
return Math.round(Math.min(1, 10 ** ((TARGET_LUFS - heard) / 20)) * 1000) / 1000;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user