diff --git a/backend/api/routes/jobs.py b/backend/api/routes/jobs.py index d19a04af..54a6f877 100644 --- a/backend/api/routes/jobs.py +++ b/backend/api/routes/jobs.py @@ -51,6 +51,7 @@ from backend.models.requests import ChangeVisibilityRequest, ChangeVisibilityRes from backend.services.user_service import get_user_service from backend.services.youtube_download_service import get_youtube_download_service from backend.services.pricing import duration_to_credits, is_blocked +from backend.workers.style_helper import screen_included from pydantic import BaseModel, Field, HttpUrl, validator @@ -588,8 +589,10 @@ def _is_auto_retry_pending(state_data: Optional[Dict[str, Any]]) -> bool: return expires_at > datetime.now(timezone.utc) -def _has_title_screen(file_urls: Dict[str, Any]) -> bool: - """Return True if a title screen image (jpg or png) was generated. +def _has_title_screen(file_urls: Dict[str, Any], state_data: Optional[Dict[str, Any]] = None) -> bool: + """Return True if the screens stage is done: a title screen image (jpg or png) + was generated, or the theme leaves the title screen out and the screens worker + finished. Used by the /retry endpoint to decide whether to resume from the render stage or rewind further. The actual screen URLs live under format-specific @@ -598,9 +601,15 @@ def _has_title_screen(file_urls: Dict[str, Any]) -> bool: completed-review jobs back to AWAITING_REVIEW. """ screens = file_urls.get('screens') or {} - if not isinstance(screens, dict): - return False - return any(k == 'title' or k.startswith('title_') for k in screens) + if isinstance(screens, dict) and any(k == 'title' or k.startswith('title_') for k in screens): + return True + # No title image by design: rely on the screens worker's completion marker + # (cleared by every re-render/regeneration, so a stale "omitted" can't count). + state_data = state_data or {} + return ( + not screen_included(state_data, 'title') + and (state_data.get('screens_progress') or {}).get('stage') == 'complete' + ) def _prune_state_data(data: Dict[str, Any]) -> Dict[str, Any]: @@ -2092,7 +2101,7 @@ async def retry_job( # resume it correctly. theme_rerender = state_data.get('theme_rerender') or {} if (theme_rerender.get('theme_id') and original_status == JobStatus.FAILED - and not _has_title_screen(file_urls)): + and not _has_title_screen(file_urls, state_data)): from backend.services.theme_rerender_service import RerenderError, ThemeRerenderService try: await ThemeRerenderService(job_manager).start( @@ -2113,7 +2122,7 @@ async def retry_job( # keeping the admin's notify_customer choice. Its published outputs were # already removed on the first attempt (recorded in the marker). if (admin_rerender and original_status in (JobStatus.FAILED, JobStatus.CANCELLED) - and not _has_title_screen(file_urls)): + and not _has_title_screen(file_urls, state_data)): from backend.services.admin_rerender_service import AdminRerenderService from backend.services.theme_rerender_service import RerenderError try: @@ -2138,7 +2147,7 @@ async def retry_job( from backend.services.theme_rerender_service import RerenderError regenerate = active_regenerate(job) if (regenerate and original_status in (JobStatus.FAILED, JobStatus.CANCELLED) - and (not _has_title_screen(file_urls) or stems_need_restore(job))): + and (not _has_title_screen(file_urls, state_data) or stems_need_restore(job))): try: await RegenerateService(job_manager).start( job, @@ -2217,7 +2226,7 @@ async def retry_job( # Review completion is proven by instrumental_selection in state_data # (set only when user submits the review endpoint) elif (file_urls.get('lyrics', {}).get('corrections') and - _has_title_screen(file_urls) and + _has_title_screen(file_urls, state_data) and state_data.get('instrumental_selection')): logger.info(f"Job {job_id}: Has corrections, screens, and completed review — retrying from render stage") @@ -2254,7 +2263,7 @@ async def retry_job( # If we have corrections and screens but review was NOT completed, # return to awaiting_review so the user can review lyrics elif (file_urls.get('lyrics', {}).get('corrections') and - _has_title_screen(file_urls)): + _has_title_screen(file_urls, state_data)): logger.info(f"Job {job_id}: Has corrections and screens but review not completed — returning to review") diff --git a/backend/api/routes/tenant_theme.py b/backend/api/routes/tenant_theme.py index 9d205adf..dc232a6b 100644 --- a/backend/api/routes/tenant_theme.py +++ b/backend/api/routes/tenant_theme.py @@ -87,8 +87,10 @@ class PreviewRequest(BaseModel): class PreviewResponse(BaseModel): - title_card: str # data:image/jpeg;base64,... + # data:image/jpeg;base64,... — null for a screen the theme leaves out of videos. + title_card: Optional[str] = None karaoke_frame: str + end_screen: Optional[str] = None class SaveRequest(BaseModel): diff --git a/backend/services/encoding_interface.py b/backend/services/encoding_interface.py index beffecbe..965f5d3e 100644 --- a/backend/services/encoding_interface.py +++ b/backend/services/encoding_interface.py @@ -137,7 +137,7 @@ class EncodingInput: Contains all the paths and metadata needed to encode a karaoke video. """ # Required input files - title_video_path: str # Title card video (MOV) + title_video_path: Optional[str] # Title card video (MOV); None = theme omits it karaoke_video_path: str # Main karaoke video with vocals (MOV/MKV) instrumental_audio_path: str # Instrumental audio track (FLAC) @@ -465,6 +465,12 @@ class GCEEncodingBackend(EncodingBackend): if existing_instrumental: encoding_config["existing_instrumental"] = existing_instrumental + # Screens the theme leaves out: the encoder must skip them rather than + # rebuild them from whatever matching PNG it finds in the job folder. + for flag in ("include_title_screen", "include_end_screen"): + if input_config.options.get(flag) is False: + encoding_config[flag] = False + countdown_padding = input_config.options.get("countdown_padding_seconds") if countdown_padding: encoding_config["countdown_padding_seconds"] = countdown_padding diff --git a/backend/services/gce_encoding/main.py b/backend/services/gce_encoding/main.py index e297d80c..ae97915e 100644 --- a/backend/services/gce_encoding/main.py +++ b/backend/services/gce_encoding/main.py @@ -1086,15 +1086,20 @@ def run_encoding(job_id: str, work_dir: Path, config: dict): # Title/end screens: look for MOV first, fall back to generating from PNG. # Since PR #640 right-sized Cloud Run to 2Gi, the screens_worker only generates # PNG/JPG images — MOV video generation now happens here on the GCE worker. - title_video = find_file(work_dir, "screens/title.mov", "*Title*.mov", "*title*.mov") - end_video = find_file(work_dir, "screens/end.mov", "*End*.mov", "*end*.mov") + # A theme may leave a screen out. Don't look for it at all then: the loose globs + # below also match theme assets (style/cdg_title_background.png, + # style/end_background.png) and old finals, which would bring it back. + include_title = config.get("include_title_screen", True) is not False + include_end = config.get("include_end_screen", True) is not False + title_video = find_file(work_dir, "screens/title.mov", "*Title*.mov", "*title*.mov") if include_title else None + end_video = find_file(work_dir, "screens/end.mov", "*End*.mov", "*end*.mov") if include_end else None # Get intro/end video durations from style config (default 5s) intro_duration = config.get("intro_video_duration", 5) end_duration = config.get("end_video_duration", 5) # Generate title MOV from PNG if missing or invalid (< 1KB = empty shell) - if not title_video or title_video.stat().st_size < 1024: + if include_title and (not title_video or title_video.stat().st_size < 1024): title_png = find_file(work_dir, "screens/title.png", "*Title*.png", "*title*.png") if title_png: title_mov_path = title_png.parent / "title.mov" @@ -1104,7 +1109,7 @@ def run_encoding(job_id: str, work_dir: Path, config: dict): logger.warning(f"Title MOV is only {title_video.stat().st_size} bytes and no PNG found") # Generate end MOV from PNG if missing or invalid - if not end_video or (end_video and end_video.stat().st_size < 1024): + if include_end and (not end_video or end_video.stat().st_size < 1024): end_png = find_file(work_dir, "screens/end.png", "*End*.png", "*end*.png") if end_png: end_mov_path = end_png.parent / "end.mov" @@ -1159,8 +1164,10 @@ def run_encoding(job_id: str, work_dir: Path, config: dict): logger.info(f" Countdown padding: {countdown_padding_seconds}s - will be handled by LocalEncodingService") # Validate required files - if not title_video: + if include_title and not title_video: raise ValueError(f"No title video found in {work_dir}. Check screens/ subdirectory.") + if not include_title or not include_end: + logger.info(f" Theme omits screens: title={'kept' if include_title else 'omitted'}, end={'kept' if include_end else 'omitted'}") if not karaoke_video: raise ValueError(f"No karaoke video found in {work_dir}") if not instrumental: @@ -1174,7 +1181,7 @@ def run_encoding(job_id: str, work_dir: Path, config: dict): # Build encoding config with proper file names # Note: countdown_padding_seconds is passed to LocalEncodingService which handles padding encoding_config = EncodingConfig( - title_video=str(title_video), + title_video=str(title_video) if title_video else None, karaoke_video=str(karaoke_video), instrumental_audio=str(instrumental), end_video=str(end_video) if end_video else None, diff --git a/backend/services/local_encoding_service.py b/backend/services/local_encoding_service.py index 61d7e810..e295c177 100644 --- a/backend/services/local_encoding_service.py +++ b/backend/services/local_encoding_service.py @@ -25,7 +25,7 @@ logger = logging.getLogger(__name__) @dataclass class EncodingConfig: """Configuration for video encoding.""" - title_video: str # Path to title video + title_video: Optional[str] # Path to title video (None = theme omits the title screen) karaoke_video: str # Path to karaoke video (with vocals) instrumental_audio: str # Path to instrumental audio end_video: Optional[str] = None # Optional path to end credits video @@ -369,16 +369,16 @@ class LocalEncodingService: def encode_lossless_mp4( self, - title_video: str, + title_video: Optional[str], karaoke_video: str, output_file: str, end_video: Optional[str] = None, ) -> bool: """ - Create lossless 4K MP4 by concatenating title, karaoke, and optionally end videos. + Create lossless 4K MP4 by concatenating the title (if any), karaoke and end (if any) videos. Args: - title_video: Path to title video + title_video: Path to title video, or None when the theme omits the title screen karaoke_video: Path to karaoke video output_file: Path for output file end_video: Optional path to end credits video @@ -386,34 +386,31 @@ class LocalEncodingService: Returns: True if successful, False otherwise """ - # Quote file paths - title_quoted = shlex.quote(os.path.abspath(title_video)) - karaoke_quoted = shlex.quote(os.path.abspath(karaoke_video)) - - # Build filter and inputs for concatenation + # (path, hardware-decode) per concat segment, in order + segments = [] + if title_video: + segments.append((title_video, True)) + segments.append((karaoke_video, True)) if end_video and os.path.isfile(end_video): - end_quoted = shlex.quote(os.path.abspath(end_video)) - extra_input = f"-i {end_quoted}" - concat_filter = ( - '-filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]' - 'concat=n=3:v=1:a=1[outv][outa]"' - ) - else: - extra_input = "" - concat_filter = ( - '-filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0]' - 'concat=n=2:v=1:a=1[outv][outa]"' + segments.append((end_video, False)) + + def inputs(hwaccel: bool) -> str: + return " ".join( + (f"{self.hwaccel_decode_flags} " if hwaccel and hw else "") + f"-i {shlex.quote(os.path.abspath(path))}" + for path, hw in segments ) + streams = "".join(f"[{i}:v:0][{i}:a:0]" for i in range(len(segments))) + concat_filter = f'-filter_complex "{streams}concat=n={len(segments)}:v=1:a=1[outv][outa]"' + gpu_command = ( - f"{self._ffmpeg_base_command} {self.hwaccel_decode_flags} -i {title_quoted} " - f"{self.hwaccel_decode_flags} -i {karaoke_quoted} {extra_input} " + f"{self._ffmpeg_base_command} {inputs(True)} " f'{concat_filter} -map "[outv]" -map "[outa]" -c:v {self.video_encoder} ' f'{self._get_nvenc_quality_settings("lossless")} -c:a pcm_s16le ' f'{self.MP4_FLAGS} "{output_file}"' ) cpu_command = ( - f"{self._ffmpeg_base_command} -i {title_quoted} -i {karaoke_quoted} {extra_input} " + f"{self._ffmpeg_base_command} {inputs(False)} " f'{concat_filter} -map "[outv]" -map "[outa]" -c:v libx264 -c:a pcm_s16le ' f'{self.MP4_FLAGS} "{output_file}"' ) diff --git a/backend/services/tenant_theme_service.py b/backend/services/tenant_theme_service.py index 98722a38..f7fab9ea 100644 --- a/backend/services/tenant_theme_service.py +++ b/backend/services/tenant_theme_service.py @@ -153,6 +153,9 @@ def sanitize_style_params( for section in ("intro", "end"): if "existing_image" in styles[section]: styles[section]["existing_image"] = None + # Optional on/off switch for including the screen in videos (absent = on). + if "enabled" in styles[section] and not isinstance(styles[section]["enabled"], bool): + raise TenantValidationError(f"{section}.enabled must be true or false.") for section, field in IMAGE_FIELDS: value = styles[section].get(field) diff --git a/backend/services/theme_preview_service.py b/backend/services/theme_preview_service.py index dcf619d2..6f152dfe 100644 --- a/backend/services/theme_preview_service.py +++ b/backend/services/theme_preview_service.py @@ -1,11 +1,13 @@ """ -Exact previews of a (draft) theme: the real title card and one karaoke-video frame. +Exact previews of a (draft) theme: the real title card, one karaoke-video frame and +the real end screen. Used by the tenant theme editor. Both images come from the SAME renderers production uses, fed the draft ``style_params`` exactly the way a render job would see them: - **Title card**: ``karaoke_gen.video_generator.VideoGenerator.create_title_video`` with ``intro_video_duration=0`` (PNG only — what ``screens_worker`` does on Cloud Run). +- **End screen**: ``VideoGenerator.create_end_video`` the same way (``end_video_duration=0``). - **Karaoke frame**: ``SegmentResizer`` → ``SubtitlesGenerator.generate_ass`` (the generator's 4K layout: ``font_size`` default 250, ``line_height = font_size``, ``max_line_length`` default 36) → one frame burned with the same ffmpeg ``ass`` @@ -87,12 +89,18 @@ def font_family_name(font_file: str) -> str: @dataclass class PreviewImages: - title_card: bytes # JPEG - karaoke_frame: bytes # JPEG + # JPEGs. A screen the theme omits from videos (``enabled: false``) isn't rendered. + title_card: Optional[bytes] + karaoke_frame: bytes + end_screen: Optional[bytes] = None - def as_data_urls(self) -> Dict[str, str]: - enc = lambda b: "data:image/jpeg;base64," + base64.b64encode(b).decode() # noqa: E731 - return {"title_card": enc(self.title_card), "karaoke_frame": enc(self.karaoke_frame)} + def as_data_urls(self) -> Dict[str, Optional[str]]: + enc = lambda b: "data:image/jpeg;base64," + base64.b64encode(b).decode() if b else None # noqa: E731 + return { + "title_card": enc(self.title_card), + "karaoke_frame": enc(self.karaoke_frame), + "end_screen": enc(self.end_screen), + } # --------------------------------------------------------------------------- @@ -220,22 +228,26 @@ def _to_jpeg(png_path: str) -> bytes: return buf.getvalue() -def render_title_card(styles: Dict, artist: str, title: str, workdir: str) -> bytes: - """Title card exactly as screens_worker renders it (PNG via PIL, no video).""" - from karaoke_gen.style_loader import DEFAULT_INTRO_STYLE +def _screen_generator(): from karaoke_gen.video_generator import VideoGenerator - fmt = {**DEFAULT_INTRO_STYLE, **(styles.get("intro") or {})} - generator = VideoGenerator( + return VideoGenerator( logger=logger, ffmpeg_base_command="ffmpeg -hide_banner -nostats -loglevel error", render_bounding_boxes=False, output_png=True, output_jpg=False, ) + + +def render_title_card(styles: Dict, artist: str, title: str, workdir: str) -> bytes: + """Title card exactly as screens_worker renders it (PNG via PIL, no video).""" + from karaoke_gen.style_loader import DEFAULT_INTRO_STYLE + + fmt = {**DEFAULT_INTRO_STYLE, **(styles.get("intro") or {})} noext = os.path.join(workdir, "title") try: - generator.create_title_video( + _screen_generator().create_title_video( artist=artist, title=title, format=fmt, @@ -249,6 +261,27 @@ def render_title_card(styles: Dict, artist: str, title: str, workdir: str) -> by return _to_jpeg(f"{noext}.png") +def render_end_screen(styles: Dict, artist: str, title: str, workdir: str) -> bytes: + """End screen exactly as screens_worker renders it (PNG via PIL, no video).""" + from karaoke_gen.style_loader import DEFAULT_END_STYLE + + fmt = {**DEFAULT_END_STYLE, **(styles.get("end") or {})} + noext = os.path.join(workdir, "end") + try: + _screen_generator().create_end_video( + artist=artist, + title=title, + format=fmt, + output_image_filepath_noext=noext, + output_video_filepath=os.path.join(workdir, "end.mov"), + existing_end_image=fmt.get("existing_image"), + end_video_duration=0, + ) + except Exception as exc: + raise ThemePreviewError(f"End screen render failed: {exc}") from exc + return _to_jpeg(f"{noext}.png") + + def _sample_segments(lines: List[str], translations: Optional[List[str]] = None): """Evenly-timed segments: line i starts at 2+3i s, words spread over 2.5 s. @@ -406,11 +439,14 @@ def render_theme_preview( _CACHE.move_to_end(key) return _CACHE[key] + from karaoke_gen.style_loader import screen_enabled + styles = resolve_assets(style_params, theme_id, storage) with tempfile.TemporaryDirectory(prefix="theme-preview-") as workdir: result = PreviewImages( - title_card=render_title_card(styles, artist, title, workdir), + title_card=render_title_card(styles, artist, title, workdir) if screen_enabled(styles, "intro") else None, karaoke_frame=render_karaoke_frame(styles, lines, workdir), + end_screen=render_end_screen(styles, artist, title, workdir) if screen_enabled(styles, "end") else None, ) with _cache_lock: _CACHE[key] = result diff --git a/backend/tests/test_tenant_theme_route.py b/backend/tests/test_tenant_theme_route.py index 9b106e6e..e937384c 100644 --- a/backend/tests/test_tenant_theme_route.py +++ b/backend/tests/test_tenant_theme_route.py @@ -99,9 +99,20 @@ def test_preview_returns_data_urls(client_for): assert resp.status_code == 200 body = resp.json() assert body["title_card"].startswith("data:image/jpeg;base64,") + assert body["end_screen"] is None # not rendered (e.g. the theme omits it) assert render.call_args.kwargs["artist"] == "Randy Vild" +def test_preview_returns_end_screen_and_null_for_omitted_title(client_for): + with patch.object(tenant_theme, "prepare_preview_styles", return_value={"intro": {}}), \ + patch.object(tenant_theme, "render_theme_preview", return_value=PreviewImages(None, b"k", b"e")): + resp = client_for().post("/api/tenant/theme/preview", json={"style_params": {"intro": {}}}) + assert resp.status_code == 200 + body = resp.json() + assert body["title_card"] is None + assert body["end_screen"].startswith("data:image/jpeg;base64,") + + def test_preview_validation_and_render_errors(client_for): client = client_for() with patch.object(tenant_theme, "prepare_preview_styles", side_effect=TenantValidationError("bad path")): diff --git a/backend/tests/test_tenant_theme_service.py b/backend/tests/test_tenant_theme_service.py index bd119d30..bed023cc 100644 --- a/backend/tests/test_tenant_theme_service.py +++ b/backend/tests/test_tenant_theme_service.py @@ -264,3 +264,15 @@ def test_missing_theme_file_is_not_found(): storage.download_json = lambda path: (_ for _ in ()).throw(FileNotFoundError(path)) with pytest.raises(tts.ThemeNotFoundError): tts.get_theme_for_editor(_config(), storage=storage) + + +@pytest.mark.parametrize("section", ["intro", "end"]) +def test_screen_enabled_flag_must_be_boolean(section): + style = _style() + style[section]["enabled"] = False + styles, _ = tts.sanitize_style_params(style, available_assets=ASSETS) + assert styles[section]["enabled"] is False + + style[section]["enabled"] = "no" + with pytest.raises(TenantValidationError, match=f"{section}.enabled"): + tts.sanitize_style_params(style, available_assets=ASSETS) diff --git a/backend/tests/test_theme_preview_service.py b/backend/tests/test_theme_preview_service.py index 93e3b717..506c4614 100644 --- a/backend/tests/test_theme_preview_service.py +++ b/backend/tests/test_theme_preview_service.py @@ -139,3 +139,59 @@ def test_failed_download_leaves_no_partial_cache_file(tmp_path, monkeypatch): with pytest.raises(IOError): tps._local_theme_asset(Storage, "t1", "bg.jpg") assert not any(f.is_file() for f in tmp_path.rglob("*")) + + +def _with_end(theme, **end): + theme["end"] = { + "background_color": "#301020", + "background_image": None, + "font": "AvenirNext-Bold.ttf", + "title_color": "#ffffff", + "artist_color": "#ffdf6b", + "title_region": "370,900,3100,350", + "artist_region": "370,1450,3100,200", + "extra_text": "THANK YOU FOR SINGING!", + "extra_text_color": "#ff7acc", + "extra_text_region": "370,400,3100,400", + **end, + } + return theme + + +def test_renders_the_end_screen_with_its_own_background_and_layout(): + tps._CACHE.clear() + result = tps.render_theme_preview("t1", _with_end(_theme()), artist="Randy Vild", storage=_NoGcsAssets()) + end = _img(result.end_screen) + assert end.size == (1280, 720) + r, g, b = end.getpixel((8, 8)) # the end section's own #301020, not the title card's + assert abs(r - 0x30) <= 6 and abs(g - 0x10) <= 6 and abs(b - 0x20) <= 6 + assert result.as_data_urls()["end_screen"].startswith("data:image/jpeg;base64,") + + +def test_end_screen_text_size_changes_only_the_end_screen(): + """Randy Vild's report: the end screen's text size is its own setting.""" + tps._CACHE.clear() + big = tps.render_theme_preview("t1", _with_end(_theme()), storage=_NoGcsAssets()) + small = tps.render_theme_preview( + "t1", _with_end(_theme(), title_region="370,900,3100,120", extra_text_region="370,400,3100,120"), + storage=_NoGcsAssets(), + ) + assert big.title_card == small.title_card + assert big.end_screen != small.end_screen + + def text_pixels(data): + return sum(1 for r, g, b in _img(data).getdata() if r > 200 and g > 200 and b > 200) + + assert text_pixels(small.end_screen) < text_pixels(big.end_screen) * 0.6 + + +def test_omitted_screens_are_not_rendered(): + tps._CACHE.clear() + theme = _with_end(_theme()) + theme["intro"]["enabled"] = False + theme["end"]["enabled"] = False + result = tps.render_theme_preview("t1", theme, storage=_NoGcsAssets()) + assert result.title_card is None and result.end_screen is None + assert _img(result.karaoke_frame).size == (1280, 720) + urls = result.as_data_urls() + assert urls["title_card"] is None and urls["end_screen"] is None diff --git a/backend/tests/test_theme_screen_omission.py b/backend/tests/test_theme_screen_omission.py new file mode 100644 index 00000000..7c9692cd --- /dev/null +++ b/backend/tests/test_theme_screen_omission.py @@ -0,0 +1,331 @@ +""" +Themes can leave the title and/or end screen out of rendered videos +(``intro.enabled`` / ``end.enabled`` = false). + +Covers the cloud pipeline end to end: the screens worker records which screens +it made (``state_data.screens_included``), the render prerequisites and /retry +ladder accept a missing omitted screen, the orchestrator/encoding config carry +explicit include flags, and the encoders (GCE worker + LocalEncodingService) +concatenate only the included segments, without rebuilding an omitted screen +from a theme background PNG that the encoder's loose globs would match. +""" +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import MagicMock, patch + +import pytest + +from backend.workers.style_helper import SCREENS_INCLUDED_KEY, screen_included + + +# --- marker helper -------------------------------------------------------------- + + +def test_screen_included_defaults_to_true_for_older_jobs(): + assert screen_included({}, "title") and screen_included(None, "end") + assert screen_included({SCREENS_INCLUDED_KEY: {"title": True}}, "end") + assert not screen_included({SCREENS_INCLUDED_KEY: {"title": False, "end": True}}, "title") + + +# --- screens worker ------------------------------------------------------------- + + +@pytest.mark.asyncio +@pytest.mark.parametrize("include_title,include_end", [(False, True), (True, False), (False, False), (True, True)]) +async def test_screens_worker_skips_omitted_screens_and_records_them(tmp_path, include_title, include_end): + from datetime import datetime, timezone + + from backend.models.job import Job, JobStatus + from backend.workers import screens_worker as sw + + job = Job( + job_id="j1", artist="A", title="T", status=JobStatus.DOWNLOADING, + created_at=datetime.now(timezone.utc), updated_at=datetime.now(timezone.utc), + state_data={"lyrics_complete": True, "audio_complete": True}, + ) + jm = MagicMock() + jm.get_job.return_value = job + jm.transition_to_state.return_value = True + style_config = MagicMock(include_title_screen=include_title, include_end_screen=include_end) + + async def load_style(*args, **kwargs): + return style_config + + async def make_png(name): + png = tmp_path / f"{name}.png" + png.write_bytes(b"png") + return str(png) + + async def noop(*args, **kwargs): + return {} + + title_gen = MagicMock(side_effect=lambda **kw: make_png("title")) + end_gen = MagicMock(side_effect=lambda **kw: make_png("end")) + upload = MagicMock(side_effect=noop) + with patch.object(sw, "JobManager", return_value=jm), \ + patch.object(sw, "StorageService"), \ + patch.object(sw, "get_settings"), \ + patch.object(sw, "create_job_logger"), \ + patch.object(sw, "setup_job_logging"), \ + patch.object(sw, "_validate_prerequisites", return_value=True), \ + patch("backend.services.stems_restore.maybe_start_stems_restore", return_value="not_needed"), \ + patch.object(sw, "load_style_config", side_effect=load_style), \ + patch.object(sw, "_create_video_generator"), \ + patch.object(sw, "_generate_title_screen", title_gen), \ + patch.object(sw, "_generate_end_screen", end_gen), \ + patch.object(sw, "_upload_screens", upload), \ + patch.object(sw, "_apply_countdown_padding_if_needed", side_effect=noop), \ + patch.object(sw, "_transcode_review_audio", side_effect=noop), \ + patch("backend.services.auto_approval.executor.maybe_auto_complete_review", side_effect=noop), \ + patch("backend.services.auto_approval.pre_apply.ensure_and_pre_apply", side_effect=noop): + result = await sw.generate_screens("j1") + assert result is True, jm.mark_job_failed.call_args + + assert title_gen.called is include_title + assert end_gen.called is include_end + kwargs = upload.call_args.kwargs + assert (kwargs["title_screen_path"] is not None) is include_title + assert (kwargs["end_screen_path"] is not None) is include_end + jm.update_state_data.assert_any_call("j1", SCREENS_INCLUDED_KEY, {"title": include_title, "end": include_end}) + + +def test_upload_screens_skips_an_omitted_screen(tmp_path): + from backend.workers.screens_worker import _upload_screens_sync + + (tmp_path / "End.png").write_bytes(b"png") + (tmp_path / "End.jpg").write_bytes(b"jpg") + storage, jm = MagicMock(), MagicMock() + storage.upload_file.side_effect = lambda local, gcs: f"gs://b/{gcs}" + _upload_screens_sync("j1", jm, storage, None, str(tmp_path / "End.png")) + assert [c.args[1] for c in storage.upload_file.call_args_list] == ["jobs/j1/screens/end.png", "jobs/j1/screens/end.jpg"] + jm.update_file_url.assert_any_call("j1", "screens", "end_png", "gs://b/jobs/j1/screens/end.png") + jm.update_file_url.assert_any_call("j1", "screens", "end_jpg", "gs://b/jobs/j1/screens/end.jpg") + + +# --- render prerequisites + retry ladder ----------------------------------------- + + +def _video_job(screens, included=None): + state = {"instrumental_selection": "clean"} + if included is not None: + state[SCREENS_INCLUDED_KEY] = included + return SimpleNamespace( + job_id="j1", + state_data=state, + file_urls={"screens": screens, "videos": {"with_vocals": "gs://v"}, "stems": {"instrumental_clean": "gs://i"}}, + ) + + +@pytest.mark.parametrize( + "screens,included,ok", + [ + ({"title_png": "x", "end_png": "y"}, None, True), + ({"end_png": "y"}, None, False), # older job: a missing title is still an error + ({"end_png": "y"}, {"title": False, "end": True}, True), + ({"title_png": "x"}, {"title": True, "end": False}, True), + ({}, {"title": False, "end": False}, True), + ({}, {"title": True, "end": False}, False), + ], +) +def test_video_prerequisites_accept_omitted_screens(screens, included, ok): + from backend.workers import video_worker + + with patch.object(video_worker, "validate_worker_can_run", return_value=None): + assert video_worker._validate_prerequisites(_video_job(screens, included)) is ok + + +@pytest.mark.parametrize( + "file_urls,state_data,expected", + [ + ({"screens": {"title_jpg": "x"}}, {}, True), + ({"screens": {}}, {}, False), + # Title omitted by the theme and the screens worker finished + ({"screens": {"end_png": "y"}}, {SCREENS_INCLUDED_KEY: {"title": False}, "screens_progress": {"stage": "complete"}}, True), + # ...but a re-render cleared screens_progress: the stale marker alone isn't enough + ({"screens": {}}, {SCREENS_INCLUDED_KEY: {"title": False}}, False), + ], +) +def test_retry_screens_check_understands_omitted_title(file_urls, state_data, expected): + from backend.api.routes.jobs import _has_title_screen + + assert _has_title_screen(file_urls, state_data) is expected + + +# --- orchestrator + encoding config --------------------------------------------- + + +def _orch_job(included): + return SimpleNamespace( + job_id="j1", artist="A", title="T", + state_data={"instrumental_selection": "clean", SCREENS_INCLUDED_KEY: included}, + file_urls={"screens": {"end_png": "gs://b/jobs/j1/screens/end.png"}, "videos": {"with_vocals": "gs://b/v.mkv"}}, + ) + + +def test_orchestrator_config_drops_omitted_screen_paths(tmp_path): + from backend.workers import video_worker_orchestrator as vwo + + dist = MagicMock(enable_youtube_upload=False, brand_prefix=None, discord_webhook_url=None, + youtube_description=None, dropbox_path=None, gdrive_folder_id=None) + with patch("backend.services.job_defaults_service.get_effective_distribution_for_job", return_value=dist): + config = vwo.create_orchestrator_config_from_job(_orch_job({"title": False, "end": True}), str(tmp_path)) + assert config.title_video_path is None + assert config.end_video_path.endswith("A - T (End).png") + assert config.include_title_screen is False and config.include_end_screen is True + + +@pytest.mark.asyncio +async def test_orchestrator_passes_include_flags_to_the_encoder(): + from backend.workers.video_worker_orchestrator import OrchestratorConfig, VideoWorkerOrchestrator + + config = OrchestratorConfig( + job_id="j1", artist="A", title="T", title_video_path=None, karaoke_video_path="k.mkv", + instrumental_audio_path="i.flac", include_title_screen=False, include_end_screen=True, + ) + orch = VideoWorkerOrchestrator(config=config, job_manager=MagicMock(), storage=MagicMock()) + backend = MagicMock() + backend.name = "local" + backend.encode = MagicMock(side_effect=RuntimeError("stop")) + orch._get_encoding_backend = MagicMock(return_value=backend) + with pytest.raises(RuntimeError, match="stop"): + await orch._run_encoding() + encoding_input = backend.encode.call_args.args[0] + assert encoding_input.title_video_path is None + assert encoding_input.options["include_title_screen"] is False + assert encoding_input.options["include_end_screen"] is True + + +@pytest.mark.asyncio +async def test_original_vocals_guide_has_no_intro_offset_without_a_title_card(): + from backend.workers.video_worker_orchestrator import OrchestratorConfig, VideoWorkerOrchestrator + + config = OrchestratorConfig( + job_id="j1", artist="A", title="T", title_video_path=None, karaoke_video_path="k", + instrumental_audio_path="i", include_title_screen=False, + ) + orch = VideoWorkerOrchestrator(config=config, job_manager=MagicMock(), storage=MagicMock()) + assert await orch._resolve_intro_seconds() == 0.0 + + +@pytest.mark.asyncio +async def test_gce_backend_sends_include_flags_only_when_a_screen_is_omitted(): + from backend.services.encoding_interface import EncodingInput, GCEEncodingBackend + + def make_input(**flags): + return EncodingInput( + title_video_path=None, karaoke_video_path="k", instrumental_audio_path="i", artist="A", title="T", + options={"job_id": "j1", "input_gcs_path": "gs://b/in/", "output_gcs_path": "gs://b/out/", **flags}, + ) + + sent = [] + + async def encode_videos(job_id, input_gcs_path, output_gcs_path, encoding_config, **kwargs): + sent.append(encoding_config) + raise RuntimeError("stop") + + gce = GCEEncodingBackend() + service = MagicMock() + service.is_enabled = True + service.encode_videos = encode_videos + gce._service = service + with patch.object(GCEEncodingBackend, "_get_service", return_value=service): + await gce.encode(make_input(include_title_screen=False, include_end_screen=True)) + await gce.encode(make_input(include_title_screen=True, include_end_screen=True)) + assert sent[0]["include_title_screen"] is False and "include_end_screen" not in sent[0] + assert "include_title_screen" not in sent[1] and "include_end_screen" not in sent[1] + + +# --- encoders ------------------------------------------------------------------- + + +@pytest.mark.parametrize( + "with_title,with_end,n", + [(True, True, 3), (False, True, 2), (True, False, 2), (False, False, 1)], +) +def test_lossless_concat_includes_only_present_segments(tmp_path, with_title, with_end, n): + from backend.services.local_encoding_service import LocalEncodingService + + end = tmp_path / "end.mov" + end.write_bytes(b"x") + service = LocalEncodingService(logger=MagicMock()) + with patch.object(service, "_execute_command_with_fallback", return_value=True) as run: + assert service.encode_lossless_mp4( + str(tmp_path / "title.mov") if with_title else None, + str(tmp_path / "karaoke.mkv"), + str(tmp_path / "out.mp4"), + str(end) if with_end else None, + ) + gpu, cpu = run.call_args.args[:2] + for cmd in (gpu, cpu): + assert f"concat=n={n}:v=1:a=1" in cmd + assert cmd.count(" -i ") == n + assert ("title.mov" in cmd) is with_title + assert ("end.mov" in cmd) is with_end + assert "karaoke.mkv" in cmd + # Segment order: title, karaoke, end + assert cpu.index("karaoke.mkv") > (cpu.index("title.mov") if with_title else -1) + if with_end: + assert cpu.index("end.mov") > cpu.index("karaoke.mkv") + + +def _gce_work_dir(tmp_path: Path) -> Path: + work = tmp_path / "work" + (work / "style").mkdir(parents=True) + (work / "videos").mkdir() + (work / "stems").mkdir() + # Theme assets the encoder's loose "*title*.png" / "*end*.png" globs would match + (work / "style" / "cdg_title_background.png").write_bytes(b"png" * 1000) + (work / "style" / "end_background.png").write_bytes(b"png" * 1000) + (work / "videos" / "with_vocals.mkv").write_bytes(b"mkv" * 1000) + (work / "stems" / "instrumental_clean.flac").write_bytes(b"flac" * 1000) + return work + + +@pytest.mark.parametrize("include_title,include_end", [(False, False), (False, True), (True, False)]) +def test_gce_worker_never_rebuilds_an_omitted_screen_from_theme_pngs(tmp_path, include_title, include_end): + from backend.services.gce_encoding import main + + work = _gce_work_dir(tmp_path) + (work / "screens").mkdir() + for screen, included in (("title", include_title), ("end", include_end)): + if included: + (work / "screens" / f"{screen}.png").write_bytes(b"png" * 1000) + + made = [] + + def fake_mov(png, mov, duration=5): + made.append(png.name) + mov.write_bytes(b"mov" * 1000) + return mov + + service = MagicMock() + service.encode_all_formats.return_value = MagicMock(success=True) + main.jobs["j1"] = {"status": "pending", "progress": 0} + with patch.object(main, "generate_mov_from_png", side_effect=fake_mov), \ + patch.object(main, "render_portrait_into_outputs"), \ + patch.object(main, "_persist"), \ + patch("backend.services.local_encoding_service.LocalEncodingService", return_value=service): + main.run_encoding("j1", work, { + "artist": "A", "title": "T", + "include_title_screen": include_title, "include_end_screen": include_end, + }) + config = service.encode_all_formats.call_args.args[0] + assert (config.title_video is not None) is include_title + assert (config.end_video is not None) is include_end + assert sorted(made) == sorted(f"{s}.png" for s, inc in (("title", include_title), ("end", include_end)) if inc) + staged = sorted(p.name for p in (work / "outputs").glob("*.mov")) + assert ("A - T (Title).mov" in staged) is include_title + assert ("A - T (End).mov" in staged) is include_end + + +def test_gce_worker_still_requires_a_title_when_included(tmp_path): + from backend.services.gce_encoding import main + + work = _gce_work_dir(tmp_path) + (work / "style" / "cdg_title_background.png").unlink() + main.jobs["j2"] = {"status": "pending", "progress": 0} + with patch.object(main, "_persist"), \ + patch.object(main, "generate_mov_from_png", side_effect=lambda png, mov, duration=5: mov), \ + patch("backend.services.local_encoding_service.LocalEncodingService"): + with pytest.raises(Exception, match="No title video"): + main.run_encoding("j2", work, {"artist": "A", "title": "T"}) diff --git a/backend/workers/screens_worker.py b/backend/workers/screens_worker.py index f0f85948..c5450a4b 100644 --- a/backend/workers/screens_worker.py +++ b/backend/workers/screens_worker.py @@ -36,7 +36,7 @@ from backend.services.job_manager import JobManager from backend.services.storage_service import StorageService from backend.services.job_health_service import validate_worker_can_run from backend.config import get_settings -from backend.workers.style_helper import load_style_config, StyleConfig +from backend.workers.style_helper import SCREENS_INCLUDED_KEY, load_style_config, StyleConfig from backend.workers.worker_logging import create_job_logger, setup_job_logging, job_logging_context from backend.services.tracing import job_span, add_span_event, add_span_attribute @@ -187,38 +187,49 @@ async def generate_screens(job_id: str) -> bool: # Initialize video generator video_generator = _create_video_generator(temp_dir) + # A theme may leave either screen out of its videos (intro/end.enabled=false). + include_title = style_config.include_title_screen + include_end = style_config.include_end_screen + title_screen_path = end_screen_path = None + # Generate title screen with style config - with job_span("generate-title-screen", job_id): - job_log.info("Generating title screen...") - title_screen_path = await _generate_title_screen( - job_id=job_id, - job=job, - video_generator=video_generator, - style_config=style_config, - temp_dir=temp_dir, - job_log=job_log - ) - - if not title_screen_path: - raise Exception("Title screen generation failed") - job_log.info(f"Title screen generated: {title_screen_path}") - + if include_title: + with job_span("generate-title-screen", job_id): + job_log.info("Generating title screen...") + title_screen_path = await _generate_title_screen( + job_id=job_id, + job=job, + video_generator=video_generator, + style_config=style_config, + temp_dir=temp_dir, + job_log=job_log + ) + + if not title_screen_path: + raise Exception("Title screen generation failed") + job_log.info(f"Title screen generated: {title_screen_path}") + else: + job_log.info("Theme omits the title screen — skipping it") + # Generate end screen with style config - with job_span("generate-end-screen", job_id): - job_log.info("Generating end screen...") - end_screen_path = await _generate_end_screen( - job_id=job_id, - job=job, - video_generator=video_generator, - style_config=style_config, - temp_dir=temp_dir, - job_log=job_log - ) - - if not end_screen_path: - raise Exception("End screen generation failed") - job_log.info(f"End screen generated: {end_screen_path}") - + if include_end: + with job_span("generate-end-screen", job_id): + job_log.info("Generating end screen...") + end_screen_path = await _generate_end_screen( + job_id=job_id, + job=job, + video_generator=video_generator, + style_config=style_config, + temp_dir=temp_dir, + job_log=job_log + ) + + if not end_screen_path: + raise Exception("End screen generation failed") + job_log.info(f"End screen generated: {end_screen_path}") + else: + job_log.info("Theme omits the end screen — skipping it") + # Upload screens to GCS with job_span("upload-screens", job_id): await _upload_screens( @@ -228,7 +239,12 @@ async def generate_screens(job_id: str) -> bool: title_screen_path=title_screen_path, end_screen_path=end_screen_path ) - + # Read by the render/encode stages (which run before the style is + # loaded) so an omitted screen isn't treated as missing. + job_manager.update_state_data( + job_id, SCREENS_INCLUDED_KEY, {"title": include_title, "end": include_end} + ) + # Apply countdown padding if needed await _apply_countdown_padding_if_needed(job_id, job_manager, job) @@ -578,11 +594,11 @@ async def _upload_screens( job_id: str, job_manager: JobManager, storage: StorageService, - title_screen_path: str, - end_screen_path: str + title_screen_path: Optional[str], + end_screen_path: Optional[str] ) -> None: """ - Upload title and end screen images to GCS. + Upload title and end screen images to GCS (None = screen omitted by the theme). Single Responsibility: Only handles uploads. @@ -607,28 +623,21 @@ def _upload_screens_sync( job_id: str, job_manager: JobManager, storage: StorageService, - title_screen_path: str, - end_screen_path: str + title_screen_path: Optional[str], + end_screen_path: Optional[str] ) -> None: - # Upload title screen images (.png and .jpg) - title_base = title_screen_path.replace('.png', '') - for ext, key in [('.png', 'title_png'), ('.jpg', 'title_jpg')]: - image_path = f"{title_base}{ext}" - if os.path.exists(image_path): - gcs_path = f"jobs/{job_id}/screens/title{ext}" - url = storage.upload_file(image_path, gcs_path) - job_manager.update_file_url(job_id, 'screens', key, url) - logger.info(f"Job {job_id}: Uploaded title screen image ({ext})") - - # Upload end screen images (.png and .jpg) - end_base = end_screen_path.replace('.png', '') - for ext, key in [('.png', 'end_png'), ('.jpg', 'end_jpg')]: - image_path = f"{end_base}{ext}" - if os.path.exists(image_path): - gcs_path = f"jobs/{job_id}/screens/end{ext}" - url = storage.upload_file(image_path, gcs_path) - job_manager.update_file_url(job_id, 'screens', key, url) - logger.info(f"Job {job_id}: Uploaded end screen image ({ext})") + for screen, path in (("title", title_screen_path), ("end", end_screen_path)): + if not path: + continue + # Upload the screen's images (.png and .jpg) + base = path.replace('.png', '') + for ext in ('.png', '.jpg'): + image_path = f"{base}{ext}" + if os.path.exists(image_path): + gcs_path = f"jobs/{job_id}/screens/{screen}{ext}" + url = storage.upload_file(image_path, gcs_path) + job_manager.update_file_url(job_id, 'screens', f"{screen}_{ext[1:]}", url) + logger.info(f"Job {job_id}: Uploaded {screen} screen image ({ext})") async def _analyze_backing_vocals( diff --git a/backend/workers/style_helper.py b/backend/workers/style_helper.py index e7a19a47..8df6fe7e 100644 --- a/backend/workers/style_helper.py +++ b/backend/workers/style_helper.py @@ -31,11 +31,23 @@ from karaoke_gen.style_loader import ( get_end_format, get_karaoke_format, get_cdg_format, + screen_enabled, ) logger = logging.getLogger(__name__) +# state_data key the screens worker writes: {"title": bool, "end": bool} — which +# screens the job's theme includes. Absent (jobs screened before themes could omit +# screens) means both were made. +SCREENS_INCLUDED_KEY = "screens_included" + + +def screen_included(state_data: Optional[Dict[str, Any]], screen: str) -> bool: + """Whether the job's videos include its "title" or "end" screen.""" + included = (state_data or {}).get(SCREENS_INCLUDED_KEY) or {} + return included.get(screen, True) is not False + # Re-export defaults for backwards compatibility with existing code DEFAULT_INTRO_FORMAT = DEFAULT_INTRO_STYLE @@ -189,6 +201,16 @@ class StyleConfig: """Check if custom styles were provided (not using defaults).""" return self._has_custom_styles + @property + def include_title_screen(self) -> bool: + """Whether the theme puts a title screen at the start of the video.""" + return screen_enabled(self._style_params, "intro") + + @property + def include_end_screen(self) -> bool: + """Whether the theme puts an end screen at the end of the video.""" + return screen_enabled(self._style_params, "end") + @property def intro_video_duration(self) -> int: """ diff --git a/backend/workers/video_worker.py b/backend/workers/video_worker.py index 53ba33fa..7ef0ffcf 100644 --- a/backend/workers/video_worker.py +++ b/backend/workers/video_worker.py @@ -50,7 +50,7 @@ from backend.services.youtube_service import get_youtube_service from backend.services.youtube_description import translated_language_name from backend.services.encoding_service import get_encoding_service, run_with_lost_job_resubmit from backend.config import get_settings -from backend.workers.style_helper import load_style_config +from backend.workers.style_helper import load_style_config, screen_included from backend.workers.worker_logging import create_job_logger, setup_job_logging, job_logging_context from backend.services.tracing import job_span, add_span_event, add_span_attribute @@ -138,6 +138,9 @@ async def _encode_via_gce( "base_name": base_name, "instrumental_selection": instrumental_selection, "existing_instrumental": existing_instrumental, + # Screens the theme leaves out (the encoder must not rebuild them from other PNGs). + "include_title_screen": screen_included(job.state_data, "title"), + "include_end_screen": screen_included(job.state_data, "end"), # NOTE: intentionally no "ffmpeg_threads" — finalization ffmpeg commands in # LocalEncodingService carry no -threads flag, so x264 uses its all-core auto # default (measured ~66-78% of a 32-vCPU worker busy during the heavy libx264 @@ -1501,8 +1504,9 @@ def _validate_prerequisites(job) -> bool: # Check screens exist (MOV or PNG — MOV is no longer uploaded, PNG is the new default) screens = job.file_urls.get('screens', {}) - has_title = screens.get('title') or screens.get('title_png') - has_end = screens.get('end') or screens.get('end_png') + # A screen the theme omits (state_data.screens_included) isn't needed. + has_title = screens.get('title') or screens.get('title_png') or not screen_included(job.state_data, 'title') + has_end = screens.get('end') or screens.get('end_png') or not screen_included(job.state_data, 'end') if not has_title or not has_end: logger.error(f"Job {job.job_id}: Missing title or end screen") return False diff --git a/backend/workers/video_worker_orchestrator.py b/backend/workers/video_worker_orchestrator.py index 20b4fc5a..38edccdb 100644 --- a/backend/workers/video_worker_orchestrator.py +++ b/backend/workers/video_worker_orchestrator.py @@ -49,14 +49,18 @@ class OrchestratorConfig: artist: str title: str - # Input file paths (in temp_dir) - title_video_path: str + # Input file paths (in temp_dir). None = the theme leaves that screen out. + title_video_path: Optional[str] karaoke_video_path: str instrumental_audio_path: str end_video_path: Optional[str] = None lrc_file_path: Optional[str] = None title_jpg_path: Optional[str] = None + # Whether the theme includes the title / end screen (state_data.screens_included) + include_title_screen: bool = True + include_end_screen: bool = True + # Output directory output_dir: str = "" @@ -521,6 +525,8 @@ class VideoWorkerOrchestrator: "output_gcs_path": output_gcs_path, "countdown_padding_seconds": self.config.countdown_padding_seconds, "existing_instrumental": self.config.existing_instrumental_gcs_path, + "include_title_screen": self.config.include_title_screen, + "include_end_screen": self.config.include_end_screen, }, ) @@ -1088,7 +1094,10 @@ class VideoWorkerOrchestrator: async def _resolve_intro_seconds(self) -> float: """The master's silent title-card length = the job's style intro ``video_duration`` (default 5s). Read the *actual* value — long-intro styles exist. Falls back to 5s - (``get_intro_format`` raises when no theme is loaded — that's the fallback path).""" + (``get_intro_format`` raises when no theme is loaded — that's the fallback path). + 0 when the theme leaves the title card out (the master starts with the song).""" + if not self.config.include_title_screen: + return 0.0 try: from backend.workers.style_helper import load_style_config import tempfile @@ -1246,6 +1255,12 @@ def create_orchestrator_config_from_job( duet_corrections_json_path = candidate break + # Screens the theme leaves out (recorded by the screens worker). + from backend.workers.style_helper import screen_included + + include_title = screen_included(job.state_data, "title") + include_end = screen_included(job.state_data, "end") + return OrchestratorConfig( worker_generation=capture_generation(job), job_id=job.job_id, @@ -1253,11 +1268,15 @@ def create_orchestrator_config_from_job( title=job.title, # Input files — use actual extension from GCS (PNG for new jobs, MOV for legacy) - title_video_path=os.path.join(temp_dir, f"{base_name} (Title){_screen_ext(job, 'title')}"), + title_video_path=( + os.path.join(temp_dir, f"{base_name} (Title){_screen_ext(job, 'title')}") if include_title else None + ), # Use actual extension from GCS path (typically .mkv) instead of hardcoding .mov karaoke_video_path=os.path.join(temp_dir, f"{base_name} (With Vocals){os.path.splitext(job.file_urls.get('videos', {}).get('with_vocals', '.mkv'))[1] or '.mkv'}"), instrumental_audio_path=instrumental_path, - end_video_path=os.path.join(temp_dir, f"{base_name} (End){_screen_ext(job, 'end')}"), + end_video_path=os.path.join(temp_dir, f"{base_name} (End){_screen_ext(job, 'end')}") if include_end else None, + include_title_screen=include_title, + include_end_screen=include_end, lrc_file_path=os.path.join(temp_dir, f"{base_name} (Karaoke).lrc"), title_jpg_path=os.path.join(temp_dir, f"{base_name} (Title).jpg"), diff --git a/docs/API.md b/docs/API.md index 05795450..f7137dd8 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1385,7 +1385,7 @@ table, then submits each confirmed row through the standard signed-URL upload fl ``` GET /api/tenant/theme → {theme_id, style_params, images[], fonts[]} POST /api/tenant/theme/assets (multipart `file`) → {name} -POST /api/tenant/theme/preview {style_params, sample?: {artist, title, lyrics[]}} → {title_card, karaoke_frame} +POST /api/tenant/theme/preview {style_params, sample?: {artist, title, lyrics[]}} → {title_card, karaoke_frame, end_screen} PUT /api/tenant/theme {style_params} → GET fields + {refreshed_jobs, outdated_job_ids[]} GET /api/tenant/theme/outdated-jobs → {theme_updated_at, job_ids[]} POST /api/tenant/theme/rerender-outdated → {started[], failed: {job_id: reason}} @@ -1401,10 +1401,21 @@ re-checks membership (`require_tenant_member`): non-members/spoofed headers → `-.` under `themes//assets/` (`no-store`), so they never overwrite an asset existing jobs use. - **Preview** renders the draft with the production renderers (`services/theme_preview_service.py`): - title card via `VideoGenerator.create_title_video(duration=0)`, karaoke frame via + title card via `VideoGenerator.create_title_video(duration=0)`, end screen via + `VideoGenerator.create_end_video(duration=0)`, karaoke frame via `SegmentResizer` → `SubtitlesGenerator.generate_ass` → one ffmpeg frame (same `ass` filter/background as the real render) with the first line half-sung. Returns 1280×720 JPEG data URLs (~2 s; ≤2 - concurrent; identical drafts cached). 400 invalid draft, 422 render failure, 504 timeout. + concurrent; identical drafts cached). A screen the theme leaves out is `null`. 400 invalid draft, + 422 render failure, 504 timeout. +- **Leaving out the title card / end screen**: `intro.enabled` / `end.enabled` (optional bool, absent + = included; deliberately not in `DEFAULT_INTRO_STYLE`/`DEFAULT_END_STYLE`, which double as the + required-field lists). Read via `style_loader.screen_enabled`. The screens worker skips an omitted + screen and records `state_data.screens_included = {title, end}`; the render prerequisites, `/retry` + ladder (`_has_title_screen`), orchestrator and encoding config read that marker. The GCE encoder gets + explicit `include_title_screen` / `include_end_screen` = false (its loose `*title*.png` / + `*end*.png` globs would otherwise rebuild an omitted screen from `style/*_background.png`), and + `LocalEncodingService.encode_lossless_mp4` / the portrait renderer concat only the included segments. + No `(Title).mov` / `(End).mov` is delivered and the original-vocals guide gets no intro offset. - **Save** applies to new jobs **and to the tenant's tracks that haven't started rendering** (`services/theme_change_service.py`). Each such job's style snapshot is replaced (`theme_applied_at` set). Jobs whose screens may already exist (`generating_screens` → diff --git a/docs/README.md b/docs/README.md index b055e4c6..3854011a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -83,6 +83,7 @@ - Lyrics review Waveforms toggle: shows a spinner and tooltip while audio separation is still producing the vocal stem (the peaks endpoint returns 202). Previously the rows were just empty (`VocalsAudioDataLoaderContext.status`). - **Translated lyrics: Japanese/Chinese translations no longer split mid-word** (2026-10-01, v0.260.1): when a long lyric line is split across rows, its translation is shared between them. Unspaced scripts were cut at an arbitrary character, so prod job ca2e881a showed 無|駄 ("waste") across two rows. `split_text_proportionally` now prefers punctuation, then a break after a Japanese particle or before a kanji/katakana word start, and only cuts mid-word as a last resort; it no longer leaves a piece empty when there are enough characters. Thai can still split inside a word (no word-boundary data). - **Translated lyrics for language learners** (2026-10-01, v0.260.0): an "Add translated lyrics" option at job creation (any job, free for now) with a language picker (33 UI locales) and a live preview of the real video frame (`GET /api/themes/translation-preview`). After review, the render worker translates the final lyrics line by line with Gemini (`backend/services/lyrics_translation.py`, GCS-cached so re-renders are free) into `lyrics/translations.json`; the landscape and portrait videos draw each translation in smaller unsung-colour text beneath its line, 3 lines per screen (`ass/config.py:translation_layout`, `LyricsLine._create_translation_event`). Translations are their own ASS events with auto paragraph direction, so mixed scripts work (a Hebrew song keeps its right-to-left highlight with an English translation beneath, and vice versa); long translations wrap/scale to fit; a line split by the resizer shares its translation across the pieces. Not editable; a translation failure renders without it (`state_data.lyrics_translation`). See [archive/2026-10-01-translated-lyrics-plan.md](archive/2026-10-01-translated-lyrics-plan.md). +- **End screen fully editable + previewed; title card / end screen can be left out** (2026-10-08, v0.268.0): Randy Vild reported "large text at the end even though I have in my settings smaller text like in my beginning". The theme editor's sizes/positions only edited `intro.*`; the end screen keeps its own `end.*` layout (title/artist regions, artist colour, capitals), and the editor neither showed it nor previewed it. The End screen tab now has its own colours, size/position sliders, show/hide for the song title and artist name, capitals toggles, and **Copy title card layout** (matches the title card and moves the closing message above the title so nothing overlaps). The preview renders the real end screen (`create_end_video`). Both tabs have an "Include …" switch (`intro.enabled` / `end.enabled`); leaving a screen out flows through the screens worker, `/retry`, the orchestrator, the GCE encoder, `LocalEncodingService` and the portrait render (see [API.md](API.md#tenant-theme-editor-tenant-users)). - **Theme edits reach tracks in progress; outdated finished tracks get a re-render prompt** (2026-10-06, v0.267.0): Randy Vild edited his theme and the next video still had the old title card. Its job had copied the theme at creation, 30 minutes before the edit, and was waiting for encoding capacity when he saved. A theme save now re-applies the theme to every tenant track that hasn't started rendering. If a track's screens were already made, the render worker regenerates them first, at render time, so nobody is interrupted mid-review. Editing a finished tenant track also picks up the current theme. After saving, the theme editor lists finished tracks that still have the old look and offers a quiet **Re-render all N** (no per-track emails). Those tracks' cards show an "Older theme" badge. Admins no longer see two identical Re-render buttons on portal tracks (`backend/services/theme_change_service.py`). - **Re-render works for tenants that archive to Dropbox** (2026-09-30, v0.254.1): randy-vild jobs get a brand code and a Dropbox archive folder (`RVILD-0001 - …`), so v0.253.0's "never distributed" guard rejected the very jobs the button was for. A re-render now reuses the job's brand code and overwrites the same folder instead of allocating a new code. YouTube/GDrive-published jobs are still rejected. - **Tenant "Re-render" with the current theme + edited tenant tracks no longer lose their downloads** (2026-09-30, v0.253.0): Finished portal tracks have a **Re-render** button (`POST /api/jobs/{id}/rerender`). It re-applies the tenant's current theme and rebuilds the screens, lyrics video, encodes and CDG/TXT from the already-reviewed lyrics, with no review step (`backend/services/theme_rerender_service.py`, same mechanism as the private→public visibility change). The encoding worker caches finished jobs by id in memory, so a re-run submitted while the VM was still up could get the previous render back. Render and encode now submit as `{job_id}_g{worker_generation}` (`encoding_worker_job_id`). Separately, `outputs_deleted_at` (set by Edit) was only cleared when the re-render uploaded to YouTube/Dropbox/GDrive, so edited tenant jobs re-rendered but hid every download (job 2579a1ea). Every successful render now clears it. diff --git a/frontend/components/tenant-theme/TenantThemeEditorDialog.tsx b/frontend/components/tenant-theme/TenantThemeEditorDialog.tsx index e4190a5c..c31086ed 100644 --- a/frontend/components/tenant-theme/TenantThemeEditorDialog.tsx +++ b/frontend/components/tenant-theme/TenantThemeEditorDialog.tsx @@ -14,11 +14,15 @@ import { tenantThemeApi, type ThemePreviewImages, type ThemeStyleParams } from " import { useTenant } from "@/lib/tenant" import { clearOutdatedThemeJobs, setOutdatedThemeJobs } from "@/lib/tenant-theme-outdated" import { - KARAOKE_DEFAULTS, formatRegion, getField, hexToRgba, parseRegion, rgbaToHex, setField, stableStringify, + KARAOKE_DEFAULTS, copyTitleCardLayoutToEnd, formatRegion, getField, hexToRgba, parseRegion, rgbaToHex, setField, stableStringify, type Region, } from "./theme-utils" const PREVIEW_DEBOUNCE_MS = 700 +// End-screen text positions used when a theme has none (matches the stock end screen). +const END_EXTRA_FALLBACK: Region = { x: 370, y: 400, w: 3100, h: 400 } +const END_TITLE_FALLBACK: Region = { x: 370, y: 900, w: 3100, h: 350 } +const END_ARTIST_FALLBACK: Region = { x: 370, y: 1450, w: 3100, h: 200 } const selectClass = "h-9 w-full rounded-md border border-input bg-transparent px-2 text-sm focus:outline-none focus:ring-1 focus:ring-ring" @@ -320,6 +324,36 @@ export function TenantThemeEditorDialog({ open, onClose }: Props) { ) } + // A text item on the end screen is hidden when its region is null. + const regionToggle = (field: "title_region" | "artist_region", label: string, fallback: Region) => ( + + ) + + // Absent = included (older themes have no "enabled" key). + const screenToggle = (section: "intro" | "end", label: string) => ( + + ) + + // The end screen's song title / artist name take the title card's look. + const copyTitleCardLayout = () => { + setDraft((d) => (d ? copyTitleCardLayoutToEnd(d, END_EXTRA_FALLBACK) : d)) + } + const upperToggle = (section: "intro" | "end", field: string, label: string) => (