mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
Consolidate style loading logic into unified karaoke_gen.style_loader module
This refactor addresses code duplication where style loading was implemented separately in 5 different places, which led to bugs (like the preview video not loading custom background images that was fixed in the previous commit). New module: karaoke_gen/style_loader.py - Single source of truth for DEFAULT_STYLE_PARAMS and ASSET_KEY_MAPPINGS - load_styles_from_gcs() for backend workers to download/update styles - update_asset_paths() to replace GCS paths with local downloaded files - Helper functions: get_intro_format(), get_end_format(), get_karaoke_format() Updated files to use the unified module: - karaoke_gen/config.py: delegates to style_loader functions - backend/workers/style_helper.py: StyleConfig now wraps style_loader - backend/workers/render_video_worker.py: uses load_styles_from_gcs() - backend/api/routes/review.py: uses load_styles_from_gcs() Benefits: - Asset key mappings defined once (was duplicated in 4+ places) - Default styles defined once (was duplicated with slight variations) - Future style changes only need to be made in one place - Tests verify the unified module directly Bump version to 0.70.6
This commit is contained in:
8 files changed
+732
-688
No files matched your search
+11
-143
@@ -28,6 +28,9 @@ from lyrics_transcriber.types import CorrectionResult
|
||||
from lyrics_transcriber.core.config import OutputConfig
|
||||
from lyrics_transcriber.correction.operations import CorrectionOperations
|
||||
|
||||
# Import from the unified style loader
|
||||
from karaoke_gen.style_loader import load_styles_from_gcs
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/review", tags=["review"])
|
||||
@@ -254,147 +257,6 @@ async def add_lyrics(job_id: str, data: Dict[str, str]):
|
||||
return {"status": "success", "message": "Add lyrics not yet implemented"}
|
||||
|
||||
|
||||
def _create_minimal_styles_json(temp_dir: str) -> str:
|
||||
"""Create a minimal styles JSON file for preview generation.
|
||||
|
||||
All fields are required by the ASS subtitle generator.
|
||||
"""
|
||||
minimal_styles = {
|
||||
"karaoke": {
|
||||
# Required for video background
|
||||
"background_color": "#000000",
|
||||
# Font settings - font_path must be string (empty string OK), font is required for ASS name
|
||||
"font_path": "",
|
||||
"font": "Arial",
|
||||
"ass_name": "Default",
|
||||
# Colors in "R, G, B, A" format (required)
|
||||
"primary_color": "112, 112, 247, 255",
|
||||
"secondary_color": "255, 255, 255, 255",
|
||||
"outline_color": "26, 58, 235, 255",
|
||||
"back_color": "0, 0, 0, 0",
|
||||
# Boolean style options
|
||||
"bold": False,
|
||||
"italic": False,
|
||||
"underline": False,
|
||||
"strike_out": False,
|
||||
# Numeric style options (all required)
|
||||
"scale_x": 100,
|
||||
"scale_y": 100,
|
||||
"spacing": 0,
|
||||
"angle": 0.0,
|
||||
"border_style": 1,
|
||||
"outline": 1,
|
||||
"shadow": 0,
|
||||
"margin_l": 0,
|
||||
"margin_r": 0,
|
||||
"margin_v": 0,
|
||||
"encoding": 0
|
||||
}
|
||||
}
|
||||
styles_path = os.path.join(temp_dir, "styles.json")
|
||||
with open(styles_path, 'w') as f:
|
||||
json.dump(minimal_styles, f)
|
||||
return styles_path
|
||||
|
||||
|
||||
def _get_or_create_styles(job, temp_dir: str, storage: StorageService) -> str:
|
||||
"""
|
||||
Get styles JSON for preview video generation.
|
||||
|
||||
Downloads custom styles from job.style_params_gcs_path and job.style_assets,
|
||||
updates paths in the JSON to point to downloaded local files, otherwise uses defaults.
|
||||
|
||||
This mirrors the logic in render_video_worker._get_or_create_styles() but for
|
||||
preview generation in the review API.
|
||||
|
||||
Args:
|
||||
job: Job object with style configuration
|
||||
temp_dir: Temporary directory for writing styles file
|
||||
storage: Storage service for downloading files
|
||||
|
||||
Returns:
|
||||
Path to styles JSON file
|
||||
"""
|
||||
style_dir = os.path.join(temp_dir, "style")
|
||||
os.makedirs(style_dir, exist_ok=True)
|
||||
styles_path = os.path.join(style_dir, "styles.json")
|
||||
|
||||
# Check if job has custom style_params.json
|
||||
if job.style_params_gcs_path:
|
||||
try:
|
||||
logger.info(f"Job {job.job_id}: Downloading custom styles from {job.style_params_gcs_path}")
|
||||
storage.download_file(job.style_params_gcs_path, styles_path)
|
||||
|
||||
# Load the styles to update asset paths
|
||||
with open(styles_path, 'r') as f:
|
||||
style_data = json.load(f)
|
||||
|
||||
logger.info(f"Job {job.job_id}: Loaded style sections: {list(style_data.keys())}")
|
||||
|
||||
# Download and update paths for style assets
|
||||
local_assets = {}
|
||||
if job.style_assets:
|
||||
logger.info(f"Job {job.job_id}: Downloading {len(job.style_assets)} style assets for preview...")
|
||||
for asset_key, gcs_path in job.style_assets.items():
|
||||
if asset_key == 'style_params':
|
||||
continue # Already downloaded
|
||||
try:
|
||||
# Determine local filename from asset key
|
||||
ext = os.path.splitext(gcs_path)[1] or '.png'
|
||||
local_path = os.path.join(style_dir, f"{asset_key}{ext}")
|
||||
storage.download_file(gcs_path, local_path)
|
||||
local_assets[asset_key] = local_path
|
||||
logger.info(f"Job {job.job_id}: Downloaded {asset_key}: {local_path}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Job {job.job_id}: Failed to download {asset_key}: {e}")
|
||||
|
||||
# Update paths in style_data to point to local files
|
||||
updates_made = False
|
||||
|
||||
# Map asset keys to style JSON paths
|
||||
asset_mapping = {
|
||||
'intro_background': ('intro', 'background_image'),
|
||||
'karaoke_background': ('karaoke', 'background_image'),
|
||||
'end_background': ('end', 'background_image'),
|
||||
'font': [('intro', 'font'), ('karaoke', 'font_path'), ('end', 'font')],
|
||||
}
|
||||
|
||||
for asset_key, local_path in local_assets.items():
|
||||
if asset_key in asset_mapping:
|
||||
mappings = asset_mapping[asset_key]
|
||||
# Handle single or multiple mappings
|
||||
if isinstance(mappings[0], str):
|
||||
mappings = [mappings]
|
||||
|
||||
for section, field in mappings:
|
||||
if section in style_data and isinstance(style_data[section], dict):
|
||||
old_value = style_data[section].get(field, 'NOT SET')
|
||||
style_data[section][field] = local_path
|
||||
logger.info(f"Job {job.job_id}: Updated {section}.{field}: {old_value} -> {local_path}")
|
||||
updates_made = True
|
||||
|
||||
# Save updated styles
|
||||
if updates_made:
|
||||
with open(styles_path, 'w') as f:
|
||||
json.dump(style_data, f, indent=2)
|
||||
logger.info(f"Job {job.job_id}: Saved updated styles with local asset paths for preview")
|
||||
|
||||
# Log final karaoke style for debugging
|
||||
if 'karaoke' in style_data:
|
||||
k = style_data['karaoke']
|
||||
logger.info(f"Job {job.job_id}: Preview karaoke style: background_image={k.get('background_image', 'NOT SET')}, font_path={k.get('font_path', 'NOT SET')}")
|
||||
|
||||
return styles_path
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Job {job.job_id}: Failed to download custom styles for preview: {e}, using defaults")
|
||||
else:
|
||||
logger.info(f"Job {job.job_id}: No custom style_params_gcs_path found, using minimal styles for preview")
|
||||
|
||||
# Use minimal styles as fallback
|
||||
return _create_minimal_styles_json(temp_dir)
|
||||
|
||||
|
||||
@router.post("/{job_id}/preview-video")
|
||||
async def generate_preview_video(job_id: str, updated_data: Dict[str, Any]):
|
||||
"""
|
||||
@@ -437,10 +299,16 @@ async def generate_preview_video(job_id: str, updated_data: Dict[str, Any]):
|
||||
# 3. Load original as CorrectionResult
|
||||
correction_result = CorrectionResult.from_dict(original_data)
|
||||
|
||||
# 4. Get or create styles file for preview
|
||||
# 4. Get or create styles file for preview using unified style loader
|
||||
# This downloads custom styles from GCS if the job has them,
|
||||
# or falls back to minimal styles for basic preview
|
||||
styles_path = _get_or_create_styles(job, temp_dir, storage)
|
||||
styles_path, _ = load_styles_from_gcs(
|
||||
style_params_gcs_path=job.style_params_gcs_path,
|
||||
style_assets=job.style_assets,
|
||||
temp_dir=temp_dir,
|
||||
download_func=storage.download_file,
|
||||
logger=logger,
|
||||
)
|
||||
|
||||
# 5. Set up output config for preview
|
||||
output_dir = os.path.join(temp_dir, "output")
|
||||
|
||||
@@ -186,30 +186,22 @@ class TestCorrectionDataMerging:
|
||||
|
||||
|
||||
class TestPreviewStyleLoading:
|
||||
"""Tests for custom style loading in preview video generation.
|
||||
"""Tests for the unified style loader used in preview video generation.
|
||||
|
||||
When a job has custom styles (uploaded via --style_params_json), these
|
||||
must be loaded and applied to preview videos, not just the final render.
|
||||
|
||||
This was a bug: preview videos were using minimal styles (black background)
|
||||
even when the job had custom backgrounds and fonts configured.
|
||||
|
||||
The style loading logic is now consolidated in karaoke_gen.style_loader
|
||||
to avoid duplication between workers and API routes.
|
||||
"""
|
||||
|
||||
def test_get_or_create_styles_with_custom_styles(self, tmp_path):
|
||||
def test_load_styles_from_gcs_with_custom_styles(self, tmp_path):
|
||||
"""Test that custom styles are downloaded and applied for preview."""
|
||||
import os
|
||||
from unittest.mock import Mock, MagicMock
|
||||
from backend.api.routes.review import _get_or_create_styles
|
||||
|
||||
# Create mock job with custom styles
|
||||
mock_job = Mock()
|
||||
mock_job.job_id = "test123"
|
||||
mock_job.style_params_gcs_path = "uploads/test123/style/style_params.json"
|
||||
mock_job.style_assets = {
|
||||
"style_params": "uploads/test123/style/style_params.json",
|
||||
"karaoke_background": "uploads/test123/style/karaoke_background.png",
|
||||
"font": "uploads/test123/style/font.ttf",
|
||||
}
|
||||
from karaoke_gen.style_loader import load_styles_from_gcs
|
||||
|
||||
# Create source style params file
|
||||
source_style_params = tmp_path / "source_styles.json"
|
||||
@@ -230,8 +222,7 @@ class TestPreviewStyleLoading:
|
||||
source_font = tmp_path / "font.ttf"
|
||||
source_font.write_bytes(b"TTF font data")
|
||||
|
||||
# Create mock storage that "downloads" files by copying from source
|
||||
mock_storage = Mock()
|
||||
# Create mock download function that simulates GCS download
|
||||
def mock_download(gcs_path, local_path):
|
||||
if "style_params.json" in gcs_path:
|
||||
with open(local_path, 'w') as f:
|
||||
@@ -242,18 +233,24 @@ class TestPreviewStyleLoading:
|
||||
elif "font.ttf" in gcs_path:
|
||||
with open(local_path, 'wb') as f:
|
||||
f.write(source_font.read_bytes())
|
||||
mock_storage.download_file = mock_download
|
||||
|
||||
# Call the function
|
||||
styles_path = _get_or_create_styles(mock_job, str(tmp_path / "workdir"), mock_storage)
|
||||
# Call the unified style loader function
|
||||
style_assets = {
|
||||
"style_params": "uploads/test123/style/style_params.json",
|
||||
"karaoke_background": "uploads/test123/style/karaoke_background.png",
|
||||
"font": "uploads/test123/style/font.ttf",
|
||||
}
|
||||
|
||||
styles_path, result_styles = load_styles_from_gcs(
|
||||
style_params_gcs_path="uploads/test123/style/style_params.json",
|
||||
style_assets=style_assets,
|
||||
temp_dir=str(tmp_path / "workdir"),
|
||||
download_func=mock_download,
|
||||
)
|
||||
|
||||
# Verify styles file was created
|
||||
assert os.path.exists(styles_path)
|
||||
|
||||
# Load and verify the styles have updated paths
|
||||
with open(styles_path, 'r') as f:
|
||||
result_styles = json.load(f)
|
||||
|
||||
# The paths should now point to the local downloaded files, not the original paths
|
||||
assert "karaoke" in result_styles
|
||||
assert result_styles["karaoke"]["background_image"] != "/original/path/background.png"
|
||||
@@ -261,54 +258,49 @@ class TestPreviewStyleLoading:
|
||||
assert result_styles["karaoke"]["font_path"] != "/original/path/font.ttf"
|
||||
assert "font.ttf" in result_styles["karaoke"]["font_path"]
|
||||
|
||||
def test_get_or_create_styles_falls_back_to_minimal(self, tmp_path):
|
||||
def test_load_styles_from_gcs_falls_back_to_minimal(self, tmp_path):
|
||||
"""Test that minimal styles are used when job has no custom styles."""
|
||||
import os
|
||||
from unittest.mock import Mock
|
||||
from backend.api.routes.review import _get_or_create_styles
|
||||
from karaoke_gen.style_loader import load_styles_from_gcs
|
||||
|
||||
# Create mock job WITHOUT custom styles
|
||||
mock_job = Mock()
|
||||
mock_job.job_id = "test456"
|
||||
mock_job.style_params_gcs_path = None # No custom styles
|
||||
mock_job.style_assets = {}
|
||||
|
||||
mock_storage = Mock()
|
||||
|
||||
# Call the function
|
||||
styles_path = _get_or_create_styles(mock_job, str(tmp_path), mock_storage)
|
||||
# Call with no custom styles
|
||||
styles_path, result_styles = load_styles_from_gcs(
|
||||
style_params_gcs_path=None, # No custom styles
|
||||
style_assets={},
|
||||
temp_dir=str(tmp_path),
|
||||
download_func=lambda gcs_path, local_path: None, # Won't be called
|
||||
)
|
||||
|
||||
# Verify styles file was created
|
||||
assert os.path.exists(styles_path)
|
||||
|
||||
# Load and verify minimal styles
|
||||
with open(styles_path, 'r') as f:
|
||||
result_styles = json.load(f)
|
||||
|
||||
# Should have karaoke section with minimal/default values
|
||||
assert "karaoke" in result_styles
|
||||
assert result_styles["karaoke"]["background_color"] == "#000000"
|
||||
assert result_styles["karaoke"]["font"] == "Arial"
|
||||
# Should NOT have background_image (minimal styles don't include it)
|
||||
assert "background_image" not in result_styles["karaoke"] or result_styles["karaoke"].get("background_image") is None
|
||||
# Minimal styles have background_image as None (default)
|
||||
assert result_styles["karaoke"].get("background_image") is None
|
||||
|
||||
def test_asset_mapping_is_complete(self):
|
||||
"""Verify all required asset mappings are defined for preview styles."""
|
||||
from backend.api.routes.review import _get_or_create_styles
|
||||
import inspect
|
||||
|
||||
# Get the source code of the function to check the asset_mapping
|
||||
source = inspect.getsource(_get_or_create_styles)
|
||||
"""Verify all required asset mappings are defined in the unified style loader."""
|
||||
from karaoke_gen.style_loader import ASSET_KEY_MAPPINGS
|
||||
|
||||
# These mappings must be present for styles to work correctly
|
||||
required_mappings = [
|
||||
"'karaoke_background'",
|
||||
"'intro_background'",
|
||||
"'end_background'",
|
||||
"'font'",
|
||||
"('karaoke', 'background_image')",
|
||||
"('karaoke', 'font_path')",
|
||||
required_keys = [
|
||||
"karaoke_background",
|
||||
"intro_background",
|
||||
"end_background",
|
||||
"font",
|
||||
]
|
||||
|
||||
for mapping in required_mappings:
|
||||
assert mapping in source, f"Asset mapping {mapping} not found in _get_or_create_styles"
|
||||
for key in required_keys:
|
||||
assert key in ASSET_KEY_MAPPINGS, f"Asset mapping '{key}' not found in ASSET_KEY_MAPPINGS"
|
||||
|
||||
# Verify karaoke_background maps to the correct path
|
||||
karaoke_mapping = ASSET_KEY_MAPPINGS["karaoke_background"]
|
||||
assert karaoke_mapping == ("karaoke", "background_image")
|
||||
|
||||
# Verify font maps to multiple sections
|
||||
font_mappings = ASSET_KEY_MAPPINGS["font"]
|
||||
assert isinstance(font_mappings, list)
|
||||
assert ("karaoke", "font_path") in font_mappings
|
||||
@@ -117,10 +117,11 @@ class TestStyleHelper:
|
||||
pytest.importorskip("google.cloud.storage", reason="GCP libraries not available")
|
||||
from backend.workers.style_helper import StyleConfig, DEFAULT_INTRO_FORMAT
|
||||
|
||||
# Mock job with no style assets
|
||||
# Mock job with no style assets and no style_params_gcs_path
|
||||
job = Mock()
|
||||
job.job_id = "test-123"
|
||||
job.style_assets = {}
|
||||
job.style_params_gcs_path = None # Explicitly set to None
|
||||
|
||||
storage = Mock()
|
||||
|
||||
|
||||
@@ -33,6 +33,9 @@ from lyrics_transcriber.output.generator import OutputGenerator
|
||||
from lyrics_transcriber.types import CorrectionResult
|
||||
from lyrics_transcriber.core.config import OutputConfig
|
||||
|
||||
# Import from the unified style loader
|
||||
from karaoke_gen.style_loader import load_styles_from_gcs
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -46,46 +49,6 @@ RENDER_VIDEO_WORKER_LOGGERS = [
|
||||
]
|
||||
|
||||
|
||||
# Default styles for video generation
|
||||
# All karaoke fields are required by the ASS subtitle generator
|
||||
DEFAULT_STYLES = {
|
||||
"karaoke": {
|
||||
# Required for video background
|
||||
"background_color": "#000000",
|
||||
# Font settings - font_path must be string (empty OK), ass_name required
|
||||
"font": "Arial",
|
||||
"font_path": "", # Must be string, not None
|
||||
"ass_name": "Default", # Required for ASS style name
|
||||
# Colors in "R, G, B, A" format (required)
|
||||
"primary_color": "112, 112, 247, 255",
|
||||
"secondary_color": "255, 255, 255, 255",
|
||||
"outline_color": "26, 58, 235, 255",
|
||||
"back_color": "0, 0, 0, 0",
|
||||
# Boolean style options
|
||||
"bold": False,
|
||||
"italic": False,
|
||||
"underline": False,
|
||||
"strike_out": False,
|
||||
# Numeric style options (all required for ASS)
|
||||
"scale_x": 100,
|
||||
"scale_y": 100,
|
||||
"spacing": 0,
|
||||
"angle": 0.0,
|
||||
"border_style": 1,
|
||||
"outline": 1,
|
||||
"shadow": 0,
|
||||
"margin_l": 0,
|
||||
"margin_r": 0,
|
||||
"margin_v": 0,
|
||||
"encoding": 0,
|
||||
# Additional layout settings
|
||||
"max_line_length": 40,
|
||||
"top_padding": 200,
|
||||
"font_size": 100
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async def process_render_video(job_id: str) -> bool:
|
||||
"""
|
||||
Render karaoke video with corrected lyrics.
|
||||
@@ -206,11 +169,18 @@ async def process_render_video(job_id: str) -> bool:
|
||||
storage.download_file(audio_gcs_path, audio_path)
|
||||
job_log.info(f"Audio downloaded: {os.path.getsize(audio_path)} bytes")
|
||||
|
||||
# 4. Get or create styles (downloads from GCS if custom styles exist)
|
||||
# 4. Get or create styles using the unified style loader
|
||||
job_log.info("Loading style configuration...")
|
||||
job_log.info(f" job.style_params_gcs_path: {job.style_params_gcs_path}")
|
||||
job_log.info(f" job.style_assets: {list(job.style_assets.keys()) if job.style_assets else 'None'}")
|
||||
styles_path = _get_or_create_styles(job, temp_dir, storage, job_log)
|
||||
|
||||
styles_path, style_data = load_styles_from_gcs(
|
||||
style_params_gcs_path=job.style_params_gcs_path,
|
||||
style_assets=job.style_assets,
|
||||
temp_dir=temp_dir,
|
||||
download_func=storage.download_file,
|
||||
logger=job_log,
|
||||
)
|
||||
|
||||
# 5. Configure OutputGenerator
|
||||
output_dir = os.path.join(temp_dir, "output")
|
||||
@@ -323,115 +293,5 @@ def _extract_gcs_path(url: str) -> str:
|
||||
return url
|
||||
|
||||
|
||||
def _get_or_create_styles(job, temp_dir: str, storage: StorageService, job_log=None) -> str:
|
||||
"""
|
||||
Get styles JSON for video generation.
|
||||
|
||||
Downloads custom styles from job.style_params_gcs_path and job.style_assets,
|
||||
updates paths in the JSON to point to downloaded local files, otherwise uses defaults.
|
||||
|
||||
Args:
|
||||
job: Job object
|
||||
temp_dir: Temporary directory for writing styles file
|
||||
storage: Storage service for downloading files
|
||||
job_log: Optional job logger for remote debugging
|
||||
|
||||
Returns:
|
||||
Path to styles JSON file
|
||||
"""
|
||||
style_dir = os.path.join(temp_dir, "style")
|
||||
os.makedirs(style_dir, exist_ok=True)
|
||||
styles_path = os.path.join(style_dir, "styles.json")
|
||||
|
||||
def log_info(msg):
|
||||
if job_log:
|
||||
job_log.info(msg)
|
||||
logger.info(msg)
|
||||
|
||||
def log_warning(msg):
|
||||
if job_log:
|
||||
job_log.warning(msg)
|
||||
logger.warning(msg)
|
||||
|
||||
# Check if job has custom style_params.json (the correct location!)
|
||||
if job.style_params_gcs_path:
|
||||
try:
|
||||
log_info(f"Downloading custom styles from {job.style_params_gcs_path}")
|
||||
storage.download_file(job.style_params_gcs_path, styles_path)
|
||||
|
||||
# Load the styles to update asset paths
|
||||
with open(styles_path, 'r') as f:
|
||||
style_data = json.load(f)
|
||||
|
||||
log_info(f"Loaded style sections: {list(style_data.keys())}")
|
||||
|
||||
# Download and update paths for style assets
|
||||
local_assets = {}
|
||||
if job.style_assets:
|
||||
log_info(f"Downloading {len(job.style_assets)} style assets...")
|
||||
for asset_key, gcs_path in job.style_assets.items():
|
||||
if asset_key == 'style_params':
|
||||
continue # Already downloaded
|
||||
try:
|
||||
# Determine local filename from asset key
|
||||
ext = os.path.splitext(gcs_path)[1] or '.png'
|
||||
local_path = os.path.join(style_dir, f"{asset_key}{ext}")
|
||||
storage.download_file(gcs_path, local_path)
|
||||
local_assets[asset_key] = local_path
|
||||
log_info(f" Downloaded {asset_key}: {local_path}")
|
||||
except Exception as e:
|
||||
log_warning(f" Failed to download {asset_key}: {e}")
|
||||
|
||||
# Update paths in style_data to point to local files
|
||||
updates_made = False
|
||||
|
||||
# Map asset keys to style JSON paths
|
||||
asset_mapping = {
|
||||
'intro_background': ('intro', 'background_image'),
|
||||
'karaoke_background': ('karaoke', 'background_image'),
|
||||
'end_background': ('end', 'background_image'),
|
||||
'font': [('intro', 'font'), ('karaoke', 'font_path'), ('end', 'font')],
|
||||
}
|
||||
|
||||
for asset_key, local_path in local_assets.items():
|
||||
if asset_key in asset_mapping:
|
||||
mappings = asset_mapping[asset_key]
|
||||
# Handle single or multiple mappings
|
||||
if isinstance(mappings[0], str):
|
||||
mappings = [mappings]
|
||||
|
||||
for section, field in mappings:
|
||||
if section in style_data and isinstance(style_data[section], dict):
|
||||
old_value = style_data[section].get(field, 'NOT SET')
|
||||
style_data[section][field] = local_path
|
||||
log_info(f" Updated {section}.{field}: {old_value} -> {local_path}")
|
||||
updates_made = True
|
||||
|
||||
# Save updated styles
|
||||
if updates_made:
|
||||
with open(styles_path, 'w') as f:
|
||||
json.dump(style_data, f, indent=2)
|
||||
log_info(f"Saved updated styles with local asset paths")
|
||||
|
||||
# Log final karaoke style for debugging
|
||||
if 'karaoke' in style_data:
|
||||
k = style_data['karaoke']
|
||||
log_info(f"Final karaoke style: background_image={k.get('background_image', 'NOT SET')}, font_path={k.get('font_path', 'NOT SET')}")
|
||||
|
||||
return styles_path
|
||||
|
||||
except Exception as e:
|
||||
log_warning(f"Failed to download custom styles: {e}, using defaults")
|
||||
else:
|
||||
log_info("No custom style_params_gcs_path found on job")
|
||||
|
||||
# Use default styles
|
||||
with open(styles_path, 'w') as f:
|
||||
json.dump(DEFAULT_STYLES, f, indent=2)
|
||||
|
||||
log_info("Using default styles for video generation")
|
||||
return styles_path
|
||||
|
||||
|
||||
# For compatibility with worker service
|
||||
render_video_worker = process_render_video
|
||||
+69
-192
@@ -3,89 +3,43 @@ Style configuration helper for workers.
|
||||
|
||||
Downloads and processes style assets from GCS, making them available
|
||||
for screen generation, video rendering, and CDG generation.
|
||||
|
||||
This module uses the unified style_loader from karaoke_gen for
|
||||
consistent style handling across local CLI and cloud backend.
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional, Tuple
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
from backend.services.storage_service import StorageService
|
||||
|
||||
# Import from the unified style loader module
|
||||
from karaoke_gen.style_loader import (
|
||||
# Defaults
|
||||
DEFAULT_INTRO_STYLE,
|
||||
DEFAULT_END_STYLE,
|
||||
DEFAULT_KARAOKE_STYLE,
|
||||
DEFAULT_CDG_STYLE,
|
||||
# Functions
|
||||
load_styles_from_gcs,
|
||||
update_asset_paths,
|
||||
save_style_params,
|
||||
get_intro_format,
|
||||
get_end_format,
|
||||
get_karaoke_format,
|
||||
get_cdg_format,
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# Default style configuration (used when no custom style is provided)
|
||||
DEFAULT_INTRO_FORMAT = {
|
||||
"video_duration": 5,
|
||||
"existing_image": None,
|
||||
"background_color": "#000000",
|
||||
"background_image": None,
|
||||
"font": None,
|
||||
"artist_color": "#CCCCCC",
|
||||
"artist_text_transform": "none",
|
||||
"title_color": "#FFFFFF",
|
||||
"title_gradient": None,
|
||||
"title_text_transform": "none",
|
||||
"title_region": "200,700,3440,400",
|
||||
"artist_region": "200,1100,3440,300",
|
||||
"artist_gradient": None,
|
||||
"extra_text": None,
|
||||
"extra_text_color": "#FFFFFF",
|
||||
"extra_text_gradient": None,
|
||||
"extra_text_region": None,
|
||||
"extra_text_text_transform": "none",
|
||||
}
|
||||
|
||||
DEFAULT_END_FORMAT = {
|
||||
"video_duration": 5,
|
||||
"existing_image": None,
|
||||
"background_color": "#000000",
|
||||
"background_image": None,
|
||||
"font": None,
|
||||
"artist_color": "#CCCCCC",
|
||||
"artist_text_transform": "none",
|
||||
"title_color": "#FFFFFF",
|
||||
"title_gradient": None,
|
||||
"title_text_transform": "none",
|
||||
"title_region": "200,900,3440,400",
|
||||
"artist_region": None,
|
||||
"artist_gradient": None,
|
||||
"extra_text": "Thank you for singing!",
|
||||
"extra_text_color": "#AAAAAA",
|
||||
"extra_text_region": "200,1300,3440,200",
|
||||
"extra_text_text_transform": "none",
|
||||
"extra_text_gradient": None,
|
||||
}
|
||||
|
||||
DEFAULT_KARAOKE_FORMAT = {
|
||||
"background_image": None,
|
||||
"background_color": "#000000",
|
||||
"font_path": None,
|
||||
"font": None,
|
||||
"ass_name": "Default",
|
||||
"primary_color": "255, 255, 255, 255",
|
||||
"secondary_color": "255, 255, 0, 255",
|
||||
"outline_color": "0, 0, 0, 255",
|
||||
"back_color": "0, 0, 0, 128",
|
||||
"bold": False,
|
||||
"italic": False,
|
||||
"underline": False,
|
||||
"strike_out": False,
|
||||
"scale_x": 100,
|
||||
"scale_y": 100,
|
||||
"spacing": 0,
|
||||
"angle": 0.0,
|
||||
"border_style": 1,
|
||||
"outline": 2,
|
||||
"shadow": 1,
|
||||
"margin_l": 0,
|
||||
"margin_r": 0,
|
||||
"margin_v": 50,
|
||||
"encoding": 0,
|
||||
}
|
||||
# Re-export defaults for backwards compatibility with existing code
|
||||
DEFAULT_INTRO_FORMAT = DEFAULT_INTRO_STYLE
|
||||
DEFAULT_END_FORMAT = DEFAULT_END_STYLE
|
||||
DEFAULT_KARAOKE_FORMAT = DEFAULT_KARAOKE_STYLE
|
||||
|
||||
|
||||
class StyleConfig:
|
||||
@@ -94,6 +48,9 @@ class StyleConfig:
|
||||
|
||||
Downloads style assets from GCS and provides processed style parameters
|
||||
with local file paths for use by video generators.
|
||||
|
||||
This class wraps the unified style_loader module to provide an
|
||||
object-oriented interface for backend workers.
|
||||
"""
|
||||
|
||||
def __init__(self, job, storage: StorageService, temp_dir: str):
|
||||
@@ -113,7 +70,9 @@ class StyleConfig:
|
||||
|
||||
self._style_params: Optional[Dict[str, Any]] = None
|
||||
self._local_assets: Dict[str, str] = {}
|
||||
self._styles_path: Optional[str] = None
|
||||
self._loaded = False
|
||||
self._has_custom_styles = False # Track if custom styles were loaded
|
||||
|
||||
async def load(self) -> None:
|
||||
"""
|
||||
@@ -130,161 +89,79 @@ class StyleConfig:
|
||||
|
||||
logger.info(f"Loading style configuration for job {self.job.job_id}")
|
||||
|
||||
# Download style assets
|
||||
# Get style assets from job
|
||||
style_assets = getattr(self.job, 'style_assets', {}) or {}
|
||||
style_params_gcs_path = getattr(self.job, 'style_params_gcs_path', None)
|
||||
|
||||
logger.info(f"Job has {len(style_assets)} style assets defined")
|
||||
for key, path in style_assets.items():
|
||||
logger.info(f" Style asset '{key}': {path}")
|
||||
|
||||
for asset_key, gcs_path in style_assets.items():
|
||||
if gcs_path:
|
||||
try:
|
||||
local_path = await self._download_asset(asset_key, gcs_path)
|
||||
if local_path:
|
||||
# Check if we have custom styles before loading
|
||||
self._has_custom_styles = bool(style_params_gcs_path)
|
||||
|
||||
# Use the unified style loader
|
||||
self._styles_path, self._style_params = load_styles_from_gcs(
|
||||
style_params_gcs_path=style_params_gcs_path,
|
||||
style_assets=style_assets,
|
||||
temp_dir=self.temp_dir,
|
||||
download_func=self.storage.download_file,
|
||||
logger=logger,
|
||||
)
|
||||
|
||||
# Track local assets for get_local_asset_path()
|
||||
if style_assets:
|
||||
for asset_key in style_assets.keys():
|
||||
if asset_key == 'style_params':
|
||||
self._local_assets['style_params'] = self._styles_path
|
||||
else:
|
||||
ext = os.path.splitext(style_assets[asset_key])[1] or '.png'
|
||||
local_path = os.path.join(self.style_dir, f"{asset_key}{ext}")
|
||||
if os.path.exists(local_path):
|
||||
self._local_assets[asset_key] = local_path
|
||||
logger.info(f"Downloaded style asset '{asset_key}' to {local_path}")
|
||||
else:
|
||||
logger.warning(f"Failed to download style asset '{asset_key}' - got None")
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to download style asset '{asset_key}': {e}")
|
||||
|
||||
logger.info(f"Successfully downloaded {len(self._local_assets)} style assets")
|
||||
|
||||
# Load and parse style_params.json
|
||||
if 'style_params' in self._local_assets:
|
||||
try:
|
||||
with open(self._local_assets['style_params'], 'r') as f:
|
||||
self._style_params = json.load(f)
|
||||
logger.info(f"Loaded style params from {self._local_assets['style_params']}")
|
||||
logger.info(f"Style params sections: {list(self._style_params.keys()) if self._style_params else 'None'}")
|
||||
|
||||
# Update file paths in style params to use local downloaded files
|
||||
self._update_style_paths()
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to parse style_params.json: {e}")
|
||||
self._style_params = None
|
||||
else:
|
||||
logger.warning("No style_params asset found in downloaded assets")
|
||||
|
||||
self._loaded = True
|
||||
|
||||
async def _download_asset(self, asset_key: str, gcs_path: str) -> Optional[str]:
|
||||
"""Download a single asset from GCS."""
|
||||
try:
|
||||
# Determine local filename
|
||||
ext = Path(gcs_path).suffix
|
||||
local_path = os.path.join(self.style_dir, f"{asset_key}{ext}")
|
||||
|
||||
# Download from GCS
|
||||
self.storage.download_file(gcs_path, local_path)
|
||||
|
||||
if os.path.exists(local_path):
|
||||
return local_path
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"Error downloading asset {asset_key}: {e}")
|
||||
return None
|
||||
|
||||
def _update_style_paths(self) -> None:
|
||||
"""
|
||||
Update file paths in style params to use downloaded local files.
|
||||
|
||||
The uploaded style_params.json may have paths like "/tmp/font.ttf"
|
||||
which don't exist on the Cloud Run instance. We need to replace
|
||||
these with the actual downloaded file paths.
|
||||
|
||||
After updating, saves the modified JSON to a new file so that
|
||||
LyricsTranscriber can read the correct paths.
|
||||
"""
|
||||
if not self._style_params:
|
||||
return
|
||||
|
||||
# Map of style param keys to asset keys
|
||||
path_mappings = {
|
||||
('intro', 'background_image'): 'intro_background',
|
||||
('intro', 'font'): 'font',
|
||||
('karaoke', 'background_image'): 'karaoke_background',
|
||||
('karaoke', 'font_path'): 'font',
|
||||
('end', 'background_image'): 'end_background',
|
||||
('end', 'font'): 'font',
|
||||
('cdg', 'font_path'): 'font',
|
||||
('cdg', 'instrumental_background'): 'cdg_instrumental_background',
|
||||
('cdg', 'title_screen_background'): 'cdg_title_background',
|
||||
('cdg', 'outro_background'): 'cdg_outro_background',
|
||||
}
|
||||
|
||||
updates_made = False
|
||||
for (section, key), asset_key in path_mappings.items():
|
||||
if section in self._style_params and key in self._style_params[section]:
|
||||
if asset_key in self._local_assets:
|
||||
old_path = self._style_params[section][key]
|
||||
self._style_params[section][key] = self._local_assets[asset_key]
|
||||
logger.info(f"Updated {section}.{key}: {old_path} -> {self._local_assets[asset_key]}")
|
||||
updates_made = True
|
||||
|
||||
# Save the updated style params to a new file so LyricsTranscriber can read them
|
||||
if updates_made:
|
||||
updated_style_path = os.path.join(self.style_dir, "style_params_updated.json")
|
||||
try:
|
||||
with open(updated_style_path, 'w') as f:
|
||||
json.dump(self._style_params, f, indent=2)
|
||||
logger.info(f"Saved updated style params to: {updated_style_path}")
|
||||
# Update the local assets to point to the new file
|
||||
self._local_assets['style_params_updated'] = updated_style_path
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to save updated style params: {e}")
|
||||
logger.info(f"Successfully loaded style configuration")
|
||||
|
||||
def get_intro_format(self) -> Dict[str, Any]:
|
||||
"""Get title/intro screen format, with custom styles if available."""
|
||||
if self._style_params and 'intro' in self._style_params:
|
||||
# Merge custom params with defaults
|
||||
format_dict = DEFAULT_INTRO_FORMAT.copy()
|
||||
format_dict.update(self._style_params['intro'])
|
||||
return format_dict
|
||||
return DEFAULT_INTRO_FORMAT.copy()
|
||||
if self._style_params:
|
||||
return get_intro_format(self._style_params)
|
||||
return DEFAULT_INTRO_STYLE.copy()
|
||||
|
||||
def get_end_format(self) -> Dict[str, Any]:
|
||||
"""Get end screen format, with custom styles if available."""
|
||||
if self._style_params and 'end' in self._style_params:
|
||||
format_dict = DEFAULT_END_FORMAT.copy()
|
||||
format_dict.update(self._style_params['end'])
|
||||
return format_dict
|
||||
return DEFAULT_END_FORMAT.copy()
|
||||
if self._style_params:
|
||||
return get_end_format(self._style_params)
|
||||
return DEFAULT_END_STYLE.copy()
|
||||
|
||||
def get_karaoke_format(self) -> Dict[str, Any]:
|
||||
"""Get karaoke video format, with custom styles if available."""
|
||||
if self._style_params and 'karaoke' in self._style_params:
|
||||
format_dict = DEFAULT_KARAOKE_FORMAT.copy()
|
||||
format_dict.update(self._style_params['karaoke'])
|
||||
return format_dict
|
||||
return DEFAULT_KARAOKE_FORMAT.copy()
|
||||
if self._style_params:
|
||||
return get_karaoke_format(self._style_params)
|
||||
return DEFAULT_KARAOKE_STYLE.copy()
|
||||
|
||||
def get_cdg_styles(self) -> Optional[Dict[str, Any]]:
|
||||
"""Get CDG generation styles if available."""
|
||||
if self._style_params and 'cdg' in self._style_params:
|
||||
return self._style_params['cdg']
|
||||
if self._style_params:
|
||||
return get_cdg_format(self._style_params)
|
||||
return None
|
||||
|
||||
def get_style_params_path(self) -> Optional[str]:
|
||||
"""
|
||||
Get local path to style_params.json with updated asset paths.
|
||||
|
||||
Returns the updated JSON file (with local paths) if available,
|
||||
otherwise falls back to the original downloaded file.
|
||||
Returns the styles JSON file path, or None if not loaded.
|
||||
"""
|
||||
# Prefer the updated file with corrected local paths
|
||||
if 'style_params_updated' in self._local_assets:
|
||||
return self._local_assets['style_params_updated']
|
||||
return self._local_assets.get('style_params')
|
||||
return self._styles_path
|
||||
|
||||
def get_local_asset_path(self, asset_key: str) -> Optional[str]:
|
||||
"""Get local path for a specific asset."""
|
||||
return self._local_assets.get(asset_key)
|
||||
|
||||
def has_custom_styles(self) -> bool:
|
||||
"""Check if custom styles were provided."""
|
||||
return self._style_params is not None
|
||||
"""Check if custom styles were provided (not using defaults)."""
|
||||
return self._has_custom_styles
|
||||
|
||||
@property
|
||||
def intro_video_duration(self) -> int:
|
||||
|
||||
+58
-142
@@ -1,163 +1,79 @@
|
||||
"""
|
||||
Configuration utilities for karaoke generation.
|
||||
|
||||
This module provides configuration loading and setup functions.
|
||||
Style loading is delegated to the unified style_loader module.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import logging
|
||||
|
||||
# Default style parameters if no JSON file is provided or if it's invalid
|
||||
DEFAULT_STYLE_PARAMS = {
|
||||
"intro": {
|
||||
"video_duration": 5,
|
||||
"existing_image": None,
|
||||
"background_color": "#000000",
|
||||
"background_image": None,
|
||||
"font": "Montserrat-Bold.ttf",
|
||||
"artist_color": "#ffdf6b",
|
||||
"artist_gradient": None,
|
||||
"title_color": "#ffffff",
|
||||
"title_gradient": None,
|
||||
"title_region": "370, 200, 3100, 480",
|
||||
"artist_region": "370, 700, 3100, 480",
|
||||
"extra_text": None,
|
||||
"extra_text_color": "#ffffff",
|
||||
"extra_text_gradient": None,
|
||||
"extra_text_region": "370, 1200, 3100, 480",
|
||||
"title_text_transform": None, # none, uppercase, lowercase, propercase
|
||||
"artist_text_transform": None, # none, uppercase, lowercase, propercase
|
||||
},
|
||||
"end": {
|
||||
"video_duration": 5,
|
||||
"existing_image": None,
|
||||
"background_color": "#000000",
|
||||
"background_image": None,
|
||||
"font": "Montserrat-Bold.ttf",
|
||||
"artist_color": "#ffdf6b",
|
||||
"artist_gradient": None,
|
||||
"title_color": "#ffffff",
|
||||
"title_gradient": None,
|
||||
"title_region": None,
|
||||
"artist_region": None,
|
||||
"extra_text": "THANK YOU FOR SINGING!",
|
||||
"extra_text_color": "#ff7acc",
|
||||
"extra_text_gradient": None,
|
||||
"extra_text_region": None,
|
||||
"title_text_transform": None, # none, uppercase, lowercase, propercase
|
||||
"artist_text_transform": None, # none, uppercase, lowercase, propercase
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def apply_style_overrides(style_params, overrides, logger):
|
||||
"""Recursively applies overrides to the style parameters."""
|
||||
for key, value in overrides.items():
|
||||
keys = key.split('.')
|
||||
current_level = style_params
|
||||
for i, k in enumerate(keys):
|
||||
if i == len(keys) - 1:
|
||||
if k in current_level:
|
||||
# Attempt to cast the value to the type of the existing value
|
||||
try:
|
||||
original_type = type(current_level[k])
|
||||
if original_type == bool:
|
||||
# Handle boolean conversion
|
||||
value = value.lower() in ('true', '1', 't', 'y', 'yes')
|
||||
else:
|
||||
value = original_type(value)
|
||||
except (ValueError, TypeError) as e:
|
||||
logger.warning(f"Could not cast override value '{value}' for key '{key}' to original type. Using as string. Error: {e}")
|
||||
current_level[k] = value
|
||||
logger.info(f"Overrode style: {key} = {value}")
|
||||
else:
|
||||
logger.warning(f"Override key '{key}' not found in style parameters.")
|
||||
elif k in current_level and isinstance(current_level[k], dict):
|
||||
current_level = current_level[k]
|
||||
else:
|
||||
logger.warning(f"Override key part '{k}' not found or not a dictionary for key '{key}'.")
|
||||
break
|
||||
# Import from the unified style loader module
|
||||
from .style_loader import (
|
||||
# Re-export defaults for backwards compatibility
|
||||
DEFAULT_STYLE_PARAMS,
|
||||
DEFAULT_INTRO_STYLE as _DEFAULT_INTRO,
|
||||
DEFAULT_END_STYLE as _DEFAULT_END,
|
||||
# Functions
|
||||
load_style_params_from_file,
|
||||
apply_style_overrides,
|
||||
get_intro_format as _get_intro_format,
|
||||
get_end_format as _get_end_format,
|
||||
get_video_durations,
|
||||
get_existing_images,
|
||||
)
|
||||
|
||||
|
||||
def load_style_params(style_params_json, style_overrides, logger):
|
||||
"""Loads style parameters from a JSON file or uses defaults."""
|
||||
style_params = {}
|
||||
if style_params_json:
|
||||
try:
|
||||
with open(style_params_json, "r") as f:
|
||||
style_params = json.loads(f.read())
|
||||
logger.info(f"Loaded style parameters from {style_params_json}")
|
||||
except FileNotFoundError:
|
||||
logger.error(f"Style parameters configuration file not found: {style_params_json}")
|
||||
sys.exit(1)
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Invalid JSON in style parameters configuration file: {e}")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
logger.error(f"Error loading style parameters file {style_params_json}: {e}")
|
||||
sys.exit(1)
|
||||
else:
|
||||
logger.info("No style parameters JSON file provided. Using default styles.")
|
||||
style_params = DEFAULT_STYLE_PARAMS
|
||||
|
||||
"""
|
||||
Loads style parameters from a JSON file or uses defaults.
|
||||
|
||||
This is the main entry point for the local CLI to load styles.
|
||||
|
||||
Args:
|
||||
style_params_json: Path to style JSON file, or None for defaults.
|
||||
style_overrides: Dict of "section.key" -> value overrides.
|
||||
logger: Logger for messages.
|
||||
|
||||
Returns:
|
||||
Dictionary of style parameters.
|
||||
"""
|
||||
style_params = load_style_params_from_file(
|
||||
style_params_json,
|
||||
logger=logger,
|
||||
exit_on_error=True,
|
||||
)
|
||||
|
||||
if style_overrides:
|
||||
apply_style_overrides(style_params, style_overrides, logger)
|
||||
|
||||
|
||||
return style_params
|
||||
|
||||
|
||||
def setup_title_format(style_params):
|
||||
"""Sets up the title format dictionary from style parameters."""
|
||||
intro_params = style_params.get("intro", DEFAULT_STYLE_PARAMS["intro"])
|
||||
return {
|
||||
"background_color": intro_params.get("background_color", DEFAULT_STYLE_PARAMS["intro"]["background_color"]),
|
||||
"background_image": intro_params.get("background_image"),
|
||||
"font": intro_params.get("font", DEFAULT_STYLE_PARAMS["intro"]["font"]),
|
||||
"artist_color": intro_params.get("artist_color", DEFAULT_STYLE_PARAMS["intro"]["artist_color"]),
|
||||
"artist_gradient": intro_params.get("artist_gradient"),
|
||||
"title_color": intro_params.get("title_color", DEFAULT_STYLE_PARAMS["intro"]["title_color"]),
|
||||
"title_gradient": intro_params.get("title_gradient"),
|
||||
"extra_text": intro_params.get("extra_text"),
|
||||
"extra_text_color": intro_params.get("extra_text_color", DEFAULT_STYLE_PARAMS["intro"]["extra_text_color"]),
|
||||
"extra_text_gradient": intro_params.get("extra_text_gradient"),
|
||||
"extra_text_region": intro_params.get("extra_text_region", DEFAULT_STYLE_PARAMS["intro"]["extra_text_region"]),
|
||||
"title_region": intro_params.get("title_region", DEFAULT_STYLE_PARAMS["intro"]["title_region"]),
|
||||
"artist_region": intro_params.get("artist_region", DEFAULT_STYLE_PARAMS["intro"]["artist_region"]),
|
||||
"title_text_transform": intro_params.get("title_text_transform"),
|
||||
"artist_text_transform": intro_params.get("artist_text_transform"),
|
||||
}
|
||||
"""
|
||||
Sets up the title format dictionary from style parameters.
|
||||
|
||||
This is a thin wrapper around style_loader.get_intro_format()
|
||||
for backwards compatibility.
|
||||
"""
|
||||
return _get_intro_format(style_params)
|
||||
|
||||
|
||||
def setup_end_format(style_params):
|
||||
"""Sets up the end format dictionary from style parameters."""
|
||||
end_params = style_params.get("end", DEFAULT_STYLE_PARAMS["end"])
|
||||
return {
|
||||
"background_color": end_params.get("background_color", DEFAULT_STYLE_PARAMS["end"]["background_color"]),
|
||||
"background_image": end_params.get("background_image"),
|
||||
"font": end_params.get("font", DEFAULT_STYLE_PARAMS["end"]["font"]),
|
||||
"artist_color": end_params.get("artist_color", DEFAULT_STYLE_PARAMS["end"]["artist_color"]),
|
||||
"artist_gradient": end_params.get("artist_gradient"),
|
||||
"title_color": end_params.get("title_color", DEFAULT_STYLE_PARAMS["end"]["title_color"]),
|
||||
"title_gradient": end_params.get("title_gradient"),
|
||||
"extra_text": end_params.get("extra_text", DEFAULT_STYLE_PARAMS["end"]["extra_text"]),
|
||||
"extra_text_color": end_params.get("extra_text_color", DEFAULT_STYLE_PARAMS["end"]["extra_text_color"]),
|
||||
"extra_text_gradient": end_params.get("extra_text_gradient"),
|
||||
"extra_text_region": end_params.get("extra_text_region"),
|
||||
"title_region": end_params.get("title_region"),
|
||||
"artist_region": end_params.get("artist_region"),
|
||||
"title_text_transform": end_params.get("title_text_transform"),
|
||||
"artist_text_transform": end_params.get("artist_text_transform"),
|
||||
}
|
||||
"""
|
||||
Sets up the end format dictionary from style parameters.
|
||||
|
||||
This is a thin wrapper around style_loader.get_end_format()
|
||||
for backwards compatibility.
|
||||
"""
|
||||
return _get_end_format(style_params)
|
||||
|
||||
def get_video_durations(style_params):
|
||||
"""Gets intro and end video durations from style parameters."""
|
||||
intro_duration = style_params.get("intro", {}).get("video_duration", DEFAULT_STYLE_PARAMS["intro"]["video_duration"])
|
||||
end_duration = style_params.get("end", {}).get("video_duration", DEFAULT_STYLE_PARAMS["end"]["video_duration"])
|
||||
return intro_duration, end_duration
|
||||
|
||||
def get_existing_images(style_params):
|
||||
"""Gets existing title and end images from style parameters."""
|
||||
existing_title_image = style_params.get("intro", {}).get("existing_image")
|
||||
existing_end_image = style_params.get("end", {}).get("existing_image")
|
||||
return existing_title_image, existing_end_image
|
||||
|
||||
def setup_ffmpeg_command(log_level):
|
||||
"""Sets up the base ffmpeg command string based on log level."""
|
||||
# Path to the Windows PyInstaller frozen bundled ffmpeg.exe, or the system-installed FFmpeg binary on Mac/Linux
|
||||
# Path to the Windows PyInstaller frozen bundled ffmpeg.exe,
|
||||
# or the system-installed FFmpeg binary on Mac/Linux
|
||||
ffmpeg_path = os.path.join(sys._MEIPASS, "ffmpeg.exe") if getattr(sys, "frozen", False) else "ffmpeg"
|
||||
ffmpeg_base_command = f"{ffmpeg_path} -hide_banner -nostats"
|
||||
if log_level == logging.DEBUG:
|
||||
|
||||
@@ -0,0 +1,530 @@
|
||||
"""
|
||||
Unified style loading and configuration module.
|
||||
|
||||
This module provides a single source of truth for:
|
||||
- Default style configurations (intro, karaoke, end, cdg)
|
||||
- Asset key mappings (GCS asset keys -> style JSON paths)
|
||||
- Style loading from local files
|
||||
- Style loading from GCS with asset path updates
|
||||
|
||||
Used by both the local CLI (karaoke-gen) and the cloud backend workers.
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# DEFAULT STYLE CONFIGURATIONS
|
||||
# =============================================================================
|
||||
|
||||
DEFAULT_INTRO_STYLE = {
|
||||
"video_duration": 5,
|
||||
"existing_image": None,
|
||||
"background_color": "#000000",
|
||||
"background_image": None,
|
||||
"font": "Montserrat-Bold.ttf",
|
||||
"artist_color": "#ffdf6b",
|
||||
"artist_gradient": None,
|
||||
"artist_text_transform": None,
|
||||
"title_color": "#ffffff",
|
||||
"title_gradient": None,
|
||||
"title_text_transform": None,
|
||||
"title_region": "370, 200, 3100, 480",
|
||||
"artist_region": "370, 700, 3100, 480",
|
||||
"extra_text": None,
|
||||
"extra_text_color": "#ffffff",
|
||||
"extra_text_gradient": None,
|
||||
"extra_text_region": "370, 1200, 3100, 480",
|
||||
"extra_text_text_transform": None,
|
||||
}
|
||||
|
||||
DEFAULT_END_STYLE = {
|
||||
"video_duration": 5,
|
||||
"existing_image": None,
|
||||
"background_color": "#000000",
|
||||
"background_image": None,
|
||||
"font": "Montserrat-Bold.ttf",
|
||||
"artist_color": "#ffdf6b",
|
||||
"artist_gradient": None,
|
||||
"artist_text_transform": None,
|
||||
"title_color": "#ffffff",
|
||||
"title_gradient": None,
|
||||
"title_text_transform": None,
|
||||
"title_region": None,
|
||||
"artist_region": None,
|
||||
"extra_text": "THANK YOU FOR SINGING!",
|
||||
"extra_text_color": "#ff7acc",
|
||||
"extra_text_gradient": None,
|
||||
"extra_text_region": None,
|
||||
"extra_text_text_transform": None,
|
||||
}
|
||||
|
||||
DEFAULT_KARAOKE_STYLE = {
|
||||
# Video background
|
||||
"background_color": "#000000",
|
||||
"background_image": None,
|
||||
# Font settings
|
||||
"font": "Arial",
|
||||
"font_path": "", # Must be string, not None (for ASS generator)
|
||||
"ass_name": "Default",
|
||||
# Colors in "R, G, B, A" format (required by ASS)
|
||||
"primary_color": "112, 112, 247, 255",
|
||||
"secondary_color": "255, 255, 255, 255",
|
||||
"outline_color": "26, 58, 235, 255",
|
||||
"back_color": "0, 0, 0, 0",
|
||||
# Boolean style options
|
||||
"bold": False,
|
||||
"italic": False,
|
||||
"underline": False,
|
||||
"strike_out": False,
|
||||
# Numeric style options (all required for ASS)
|
||||
"scale_x": 100,
|
||||
"scale_y": 100,
|
||||
"spacing": 0,
|
||||
"angle": 0.0,
|
||||
"border_style": 1,
|
||||
"outline": 1,
|
||||
"shadow": 0,
|
||||
"margin_l": 0,
|
||||
"margin_r": 0,
|
||||
"margin_v": 0,
|
||||
"encoding": 0,
|
||||
# Layout settings
|
||||
"max_line_length": 40,
|
||||
"top_padding": 200,
|
||||
"font_size": 100,
|
||||
}
|
||||
|
||||
DEFAULT_CDG_STYLE = {
|
||||
"font_path": None,
|
||||
"instrumental_background": None,
|
||||
"title_screen_background": None,
|
||||
"outro_background": None,
|
||||
}
|
||||
|
||||
# Combined defaults for convenience
|
||||
DEFAULT_STYLE_PARAMS = {
|
||||
"intro": DEFAULT_INTRO_STYLE.copy(),
|
||||
"end": DEFAULT_END_STYLE.copy(),
|
||||
"karaoke": DEFAULT_KARAOKE_STYLE.copy(),
|
||||
"cdg": DEFAULT_CDG_STYLE.copy(),
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# ASSET KEY MAPPINGS
|
||||
# =============================================================================
|
||||
|
||||
# Maps GCS/upload asset keys to (section, field) paths in the style JSON.
|
||||
# Some assets map to multiple fields (e.g., font applies to intro, karaoke, end).
|
||||
ASSET_KEY_MAPPINGS: Dict[str, Union[Tuple[str, str], List[Tuple[str, str]]]] = {
|
||||
# Background images - each maps to one section
|
||||
"intro_background": ("intro", "background_image"),
|
||||
"style_intro_background": ("intro", "background_image"), # CLI upload key
|
||||
"karaoke_background": ("karaoke", "background_image"),
|
||||
"style_karaoke_background": ("karaoke", "background_image"), # CLI upload key
|
||||
"end_background": ("end", "background_image"),
|
||||
"style_end_background": ("end", "background_image"), # CLI upload key
|
||||
|
||||
# Font - maps to multiple sections
|
||||
"font": [
|
||||
("intro", "font"),
|
||||
("karaoke", "font_path"),
|
||||
("end", "font"),
|
||||
("cdg", "font_path"),
|
||||
],
|
||||
"style_font": [ # CLI upload key
|
||||
("intro", "font"),
|
||||
("karaoke", "font_path"),
|
||||
("end", "font"),
|
||||
("cdg", "font_path"),
|
||||
],
|
||||
|
||||
# CDG-specific backgrounds
|
||||
"cdg_instrumental_background": ("cdg", "instrumental_background"),
|
||||
"style_cdg_instrumental_background": ("cdg", "instrumental_background"),
|
||||
"cdg_title_background": ("cdg", "title_screen_background"),
|
||||
"style_cdg_title_background": ("cdg", "title_screen_background"),
|
||||
"cdg_outro_background": ("cdg", "outro_background"),
|
||||
"style_cdg_outro_background": ("cdg", "outro_background"),
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# STYLE LOADING FUNCTIONS
|
||||
# =============================================================================
|
||||
|
||||
def load_style_params_from_file(
|
||||
style_json_path: Optional[str],
|
||||
logger: Optional[logging.Logger] = None,
|
||||
exit_on_error: bool = True,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Load style parameters from a local JSON file.
|
||||
|
||||
Args:
|
||||
style_json_path: Path to the style JSON file, or None for defaults.
|
||||
logger: Optional logger for messages.
|
||||
exit_on_error: If True, calls sys.exit(1) on file errors.
|
||||
If False, raises exceptions instead.
|
||||
|
||||
Returns:
|
||||
Dictionary of style parameters (loaded or defaults).
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If file not found and exit_on_error=False.
|
||||
json.JSONDecodeError: If invalid JSON and exit_on_error=False.
|
||||
"""
|
||||
import sys
|
||||
|
||||
log = logger or logging.getLogger(__name__)
|
||||
|
||||
if not style_json_path:
|
||||
log.info("No style parameters JSON file provided. Using default styles.")
|
||||
return get_default_style_params()
|
||||
|
||||
try:
|
||||
with open(style_json_path, "r") as f:
|
||||
style_params = json.load(f)
|
||||
log.info(f"Loaded style parameters from {style_json_path}")
|
||||
return style_params
|
||||
except FileNotFoundError:
|
||||
log.error(f"Style parameters configuration file not found: {style_json_path}")
|
||||
if exit_on_error:
|
||||
sys.exit(1)
|
||||
raise
|
||||
except json.JSONDecodeError as e:
|
||||
log.error(f"Invalid JSON in style parameters configuration file: {e}")
|
||||
if exit_on_error:
|
||||
sys.exit(1)
|
||||
raise
|
||||
except Exception as e:
|
||||
log.error(f"Error loading style parameters file {style_json_path}: {e}")
|
||||
if exit_on_error:
|
||||
sys.exit(1)
|
||||
raise
|
||||
|
||||
|
||||
def apply_style_overrides(
|
||||
style_params: Dict[str, Any],
|
||||
overrides: Dict[str, str],
|
||||
logger: Optional[logging.Logger] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Recursively apply overrides to style parameters (in place).
|
||||
|
||||
Args:
|
||||
style_params: Style parameters dict to modify.
|
||||
overrides: Dict of "section.key" -> value overrides.
|
||||
logger: Optional logger for messages.
|
||||
"""
|
||||
log = logger or logging.getLogger(__name__)
|
||||
|
||||
for key, value in overrides.items():
|
||||
keys = key.split('.')
|
||||
current_level = style_params
|
||||
|
||||
for i, k in enumerate(keys):
|
||||
if i == len(keys) - 1:
|
||||
if k in current_level:
|
||||
# Cast to original type if possible
|
||||
try:
|
||||
original_type = type(current_level[k])
|
||||
if original_type == bool:
|
||||
value = str(value).lower() in ('true', '1', 't', 'y', 'yes')
|
||||
elif current_level[k] is not None:
|
||||
value = original_type(value)
|
||||
except (ValueError, TypeError) as e:
|
||||
log.warning(
|
||||
f"Could not cast override value '{value}' for key '{key}' "
|
||||
f"to original type. Using as string. Error: {e}"
|
||||
)
|
||||
current_level[k] = value
|
||||
log.info(f"Overrode style: {key} = {value}")
|
||||
else:
|
||||
log.warning(f"Override key '{key}' not found in style parameters.")
|
||||
elif k in current_level and isinstance(current_level[k], dict):
|
||||
current_level = current_level[k]
|
||||
else:
|
||||
log.warning(
|
||||
f"Override key part '{k}' not found or not a dictionary for key '{key}'."
|
||||
)
|
||||
break
|
||||
|
||||
|
||||
def update_asset_paths(
|
||||
style_data: Dict[str, Any],
|
||||
local_assets: Dict[str, str],
|
||||
logger: Optional[logging.Logger] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Update file paths in style data to point to local asset files.
|
||||
|
||||
This is used when assets are downloaded from GCS and need their
|
||||
paths updated in the style JSON to point to the local copies.
|
||||
|
||||
Args:
|
||||
style_data: Style parameters dict to modify (in place).
|
||||
local_assets: Dict mapping asset keys to local file paths.
|
||||
logger: Optional logger for messages.
|
||||
|
||||
Returns:
|
||||
True if any paths were updated, False otherwise.
|
||||
"""
|
||||
log = logger or logging.getLogger(__name__)
|
||||
updates_made = False
|
||||
|
||||
for asset_key, local_path in local_assets.items():
|
||||
if asset_key not in ASSET_KEY_MAPPINGS:
|
||||
continue
|
||||
|
||||
mappings = ASSET_KEY_MAPPINGS[asset_key]
|
||||
|
||||
# Normalize to list of tuples
|
||||
if isinstance(mappings, tuple):
|
||||
mappings = [mappings]
|
||||
|
||||
for section, field in mappings:
|
||||
if section in style_data and isinstance(style_data[section], dict):
|
||||
old_value = style_data[section].get(field, 'NOT SET')
|
||||
style_data[section][field] = local_path
|
||||
log.info(f"Updated {section}.{field}: {old_value} -> {local_path}")
|
||||
updates_made = True
|
||||
|
||||
return updates_made
|
||||
|
||||
|
||||
def save_style_params(
|
||||
style_data: Dict[str, Any],
|
||||
output_path: str,
|
||||
logger: Optional[logging.Logger] = None,
|
||||
) -> str:
|
||||
"""
|
||||
Save style parameters to a JSON file.
|
||||
|
||||
Args:
|
||||
style_data: Style parameters dict to save.
|
||||
output_path: Path to save the JSON file.
|
||||
logger: Optional logger for messages.
|
||||
|
||||
Returns:
|
||||
The output path.
|
||||
"""
|
||||
log = logger or logging.getLogger(__name__)
|
||||
|
||||
with open(output_path, 'w') as f:
|
||||
json.dump(style_data, f, indent=2)
|
||||
|
||||
log.info(f"Saved style parameters to: {output_path}")
|
||||
return output_path
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# GCS STYLE LOADING (for backend workers)
|
||||
# =============================================================================
|
||||
|
||||
def load_styles_from_gcs(
|
||||
style_params_gcs_path: Optional[str],
|
||||
style_assets: Optional[Dict[str, str]],
|
||||
temp_dir: str,
|
||||
download_func: Callable[[str, str], None],
|
||||
logger: Optional[logging.Logger] = None,
|
||||
) -> Tuple[str, Dict[str, Any]]:
|
||||
"""
|
||||
Download and process style configuration from GCS.
|
||||
|
||||
This is the main entry point for backend workers to load styles.
|
||||
It handles:
|
||||
1. Downloading style_params.json from GCS
|
||||
2. Downloading all style assets (backgrounds, fonts)
|
||||
3. Updating paths in the style JSON to point to local files
|
||||
4. Saving the updated JSON
|
||||
|
||||
Args:
|
||||
style_params_gcs_path: GCS path to style_params.json, or None.
|
||||
style_assets: Dict of asset_key -> GCS path for style assets.
|
||||
temp_dir: Temporary directory for downloaded files.
|
||||
download_func: Function(gcs_path, local_path) to download files.
|
||||
logger: Optional logger for messages.
|
||||
|
||||
Returns:
|
||||
Tuple of (local_styles_path, style_data_dict).
|
||||
If no custom styles, returns (minimal_styles_path, minimal_styles_dict).
|
||||
"""
|
||||
log = logger or logging.getLogger(__name__)
|
||||
|
||||
style_dir = os.path.join(temp_dir, "style")
|
||||
os.makedirs(style_dir, exist_ok=True)
|
||||
styles_path = os.path.join(style_dir, "styles.json")
|
||||
|
||||
if not style_params_gcs_path:
|
||||
log.info("No custom style_params_gcs_path found, using minimal/default styles")
|
||||
minimal_styles = get_minimal_karaoke_styles()
|
||||
save_style_params(minimal_styles, styles_path, log)
|
||||
return styles_path, minimal_styles
|
||||
|
||||
try:
|
||||
log.info(f"Downloading custom styles from {style_params_gcs_path}")
|
||||
download_func(style_params_gcs_path, styles_path)
|
||||
|
||||
with open(styles_path, 'r') as f:
|
||||
style_data = json.load(f)
|
||||
|
||||
log.info(f"Loaded style sections: {list(style_data.keys())}")
|
||||
|
||||
# Download style assets
|
||||
local_assets = {}
|
||||
if style_assets:
|
||||
log.info(f"Downloading {len(style_assets)} style assets...")
|
||||
for asset_key, gcs_path in style_assets.items():
|
||||
if asset_key == 'style_params':
|
||||
continue # Already downloaded
|
||||
try:
|
||||
ext = os.path.splitext(gcs_path)[1] or '.png'
|
||||
local_path = os.path.join(style_dir, f"{asset_key}{ext}")
|
||||
download_func(gcs_path, local_path)
|
||||
local_assets[asset_key] = local_path
|
||||
log.info(f" Downloaded {asset_key}: {local_path}")
|
||||
except Exception as e:
|
||||
log.warning(f" Failed to download {asset_key}: {e}")
|
||||
|
||||
# Update paths in style_data
|
||||
if local_assets:
|
||||
updates_made = update_asset_paths(style_data, local_assets, log)
|
||||
if updates_made:
|
||||
save_style_params(style_data, styles_path, log)
|
||||
|
||||
# Log karaoke style for debugging
|
||||
if 'karaoke' in style_data:
|
||||
k = style_data['karaoke']
|
||||
log.info(
|
||||
f"Final karaoke style: background_image={k.get('background_image', 'NOT SET')}, "
|
||||
f"font_path={k.get('font_path', 'NOT SET')}"
|
||||
)
|
||||
|
||||
return styles_path, style_data
|
||||
|
||||
except Exception as e:
|
||||
log.warning(f"Failed to download custom styles: {e}, using defaults")
|
||||
minimal_styles = get_minimal_karaoke_styles()
|
||||
save_style_params(minimal_styles, styles_path, log)
|
||||
return styles_path, minimal_styles
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# HELPER FUNCTIONS
|
||||
# =============================================================================
|
||||
|
||||
def get_default_style_params() -> Dict[str, Any]:
|
||||
"""Get a fresh copy of the default style parameters."""
|
||||
return {
|
||||
"intro": DEFAULT_INTRO_STYLE.copy(),
|
||||
"end": DEFAULT_END_STYLE.copy(),
|
||||
"karaoke": DEFAULT_KARAOKE_STYLE.copy(),
|
||||
"cdg": DEFAULT_CDG_STYLE.copy(),
|
||||
}
|
||||
|
||||
|
||||
def get_minimal_karaoke_styles() -> Dict[str, Any]:
|
||||
"""
|
||||
Get minimal styles for karaoke video generation.
|
||||
|
||||
This is used when no custom styles are provided, providing
|
||||
just enough configuration for the ASS subtitle generator.
|
||||
"""
|
||||
return {
|
||||
"karaoke": DEFAULT_KARAOKE_STYLE.copy(),
|
||||
}
|
||||
|
||||
|
||||
def get_intro_format(style_params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract intro/title screen format from style parameters.
|
||||
|
||||
Merges custom intro params with defaults.
|
||||
"""
|
||||
defaults = DEFAULT_INTRO_STYLE
|
||||
intro_params = style_params.get("intro", {})
|
||||
|
||||
result = defaults.copy()
|
||||
result.update(intro_params)
|
||||
return result
|
||||
|
||||
|
||||
def get_end_format(style_params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract end screen format from style parameters.
|
||||
|
||||
Merges custom end params with defaults.
|
||||
"""
|
||||
defaults = DEFAULT_END_STYLE
|
||||
end_params = style_params.get("end", {})
|
||||
|
||||
result = defaults.copy()
|
||||
result.update(end_params)
|
||||
return result
|
||||
|
||||
|
||||
def get_karaoke_format(style_params: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract karaoke video format from style parameters.
|
||||
|
||||
Merges custom karaoke params with defaults.
|
||||
"""
|
||||
defaults = DEFAULT_KARAOKE_STYLE
|
||||
karaoke_params = style_params.get("karaoke", {})
|
||||
|
||||
result = defaults.copy()
|
||||
result.update(karaoke_params)
|
||||
return result
|
||||
|
||||
|
||||
def get_cdg_format(style_params: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Extract CDG generation format from style parameters.
|
||||
|
||||
Returns None if no CDG section is defined.
|
||||
"""
|
||||
if "cdg" not in style_params:
|
||||
return None
|
||||
|
||||
defaults = DEFAULT_CDG_STYLE
|
||||
cdg_params = style_params.get("cdg", {})
|
||||
|
||||
result = defaults.copy()
|
||||
result.update(cdg_params)
|
||||
return result
|
||||
|
||||
|
||||
def get_video_durations(style_params: Dict[str, Any]) -> Tuple[int, int]:
|
||||
"""
|
||||
Get intro and end video durations from style parameters.
|
||||
|
||||
Returns:
|
||||
Tuple of (intro_duration, end_duration) in seconds.
|
||||
"""
|
||||
intro_duration = style_params.get("intro", {}).get(
|
||||
"video_duration", DEFAULT_INTRO_STYLE["video_duration"]
|
||||
)
|
||||
end_duration = style_params.get("end", {}).get(
|
||||
"video_duration", DEFAULT_END_STYLE["video_duration"]
|
||||
)
|
||||
return intro_duration, end_duration
|
||||
|
||||
|
||||
def get_existing_images(style_params: Dict[str, Any]) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""
|
||||
Get existing title and end images from style parameters.
|
||||
|
||||
Returns:
|
||||
Tuple of (existing_title_image, existing_end_image) paths or None.
|
||||
"""
|
||||
existing_title_image = style_params.get("intro", {}).get("existing_image")
|
||||
existing_end_image = style_params.get("end", {}).get("existing_image")
|
||||
return existing_title_image, existing_end_image
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "karaoke-gen"
|
||||
version = "0.70.5"
|
||||
version = "0.70.6"
|
||||
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
|
||||
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
|
||||
license = "MIT"
|
||||
|
||||
Reference in new issue
Block a user