diff --git a/backend/api/routes/review.py b/backend/api/routes/review.py index 8081c2c7..2a0d9e67 100644 --- a/backend/api/routes/review.py +++ b/backend/api/routes/review.py @@ -1955,6 +1955,17 @@ async def generate_preview_video( detail=t("en", "review.reviewNotReady", status=job.status) ) + # An in-browser edit can send words/lines without timings (JSON drops undefined), + # which crashed the preview with KeyError 'end_time'. Fill what we can, as review + # submit does, and name the line we can't instead of returning a 500. + from backend.services.timing_sanitizer import find_missing_timings, sanitize_corrections + _, timing_fixes = sanitize_corrections(updated_data) + if timing_fixes: + logger.info(f"Job {job_id}: preview payload had {timing_fixes} missing/out-of-range timings; filled") + untimed_line = find_missing_timings(updated_data) + if untimed_line is not None: + raise HTTPException(status_code=422, detail=t("en", "review.previewMissingTimings", line=untimed_line[:80])) + # Check if GCE preview encoding is enabled use_gce_preview = encoding_service.is_preview_enabled diff --git a/backend/services/timing_sanitizer.py b/backend/services/timing_sanitizer.py index 3e6f8a6f..3bcb928e 100644 --- a/backend/services/timing_sanitizer.py +++ b/backend/services/timing_sanitizer.py @@ -3,25 +3,62 @@ Mirrors the frontend sanitizeWordTimings.ts and the render-time SegmentResizer._sanitize_segment_timings. Operates on the raw corrections dict the frontend POSTs (Dict[str, Any]); clamps any word whose start/end falls outside its -segment window or is inverted. Segments whose own bounds are non-finite are left -untouched (the editor derives bounds from words elsewhere). Mutates and returns the -same dict for caller convenience, plus the number of fields clamped.""" +segment window or is inverted. A segment whose own bounds are missing/non-finite +(JSON.stringify drops `undefined`, so an in-browser edit can arrive without them) +gets them from its words' timings, else from its neighbours; one with no usable +timing anywhere is left untouched (see find_missing_timings). Mutates and returns +the same dict for caller convenience, plus the number of fields changed.""" from __future__ import annotations import math -from typing import Any, Dict, Tuple +from typing import Any, Dict, List, Optional, Tuple def _finite(v: Any) -> bool: return isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v) +def _fill_segment_bounds(segments: List[Any]) -> int: + """Give segments with missing/non-finite bounds ones derived from their words, + else from the neighbouring segments. Returns the number of fields filled.""" + changes = 0 + for i, seg in enumerate(segments): + if not isinstance(seg, dict): + continue + start, end = seg.get("start_time"), seg.get("end_time") + if _finite(start) and _finite(end): + continue + words = [w for w in seg.get("words", []) if isinstance(w, dict)] + starts = [w["start_time"] for w in words if _finite(w.get("start_time"))] + ends = [w["end_time"] for w in words if _finite(w.get("end_time"))] + prev_end = next((s.get("end_time") for s in reversed(segments[:i]) + if isinstance(s, dict) and _finite(s.get("end_time"))), None) + next_start = next((s.get("start_time") for s in segments[i + 1:] + if isinstance(s, dict) and _finite(s.get("start_time"))), None) + + new_start = start if _finite(start) else (min(starts + ends) if starts or ends else prev_end) + new_end = end if _finite(end) else (max(starts + ends) if starts or ends else next_start) + if new_start is None and new_end is None: + continue # no timing anywhere near it: leave it for find_missing_timings + if new_start is None: + new_start = new_end + if new_end is None or new_end < new_start: + new_end = new_start + if new_start != start: + seg["start_time"] = new_start + changes += 1 + if new_end != end: + seg["end_time"] = new_end + changes += 1 + return changes + + def sanitize_corrections(corrections: Dict[str, Any]) -> Tuple[Dict[str, Any], int]: segments = corrections.get("corrected_segments") if not isinstance(segments, list): return corrections, 0 - changes = 0 + changes = _fill_segment_bounds(segments) for seg in segments: if not isinstance(seg, dict): continue @@ -62,3 +99,16 @@ def sanitize_corrections(corrections: Dict[str, Any]) -> Tuple[Dict[str, Any], i prev_end = new_end return corrections, changes + + +def find_missing_timings(corrections: Dict[str, Any]) -> Optional[str]: + """After sanitize_corrections: the text of the first line that still has a word + or segment without a finite start/end time, or None if every timing is usable.""" + for seg in corrections.get("corrected_segments") or []: + if not isinstance(seg, dict): + continue + words = [w for w in seg.get("words", []) if isinstance(w, dict)] + fields = [seg] + words + if any(not _finite(f.get("start_time")) or not _finite(f.get("end_time")) for f in fields): + return str(seg.get("text") or " ".join(str(w.get("text", "")) for w in words)).strip() + return None diff --git a/backend/tests/services/test_timing_sanitizer.py b/backend/tests/services/test_timing_sanitizer.py index 60f22aa3..e68f1bb2 100644 --- a/backend/tests/services/test_timing_sanitizer.py +++ b/backend/tests/services/test_timing_sanitizer.py @@ -1,7 +1,7 @@ """Tests for backend.services.timing_sanitizer.""" import pytest -from backend.services.timing_sanitizer import sanitize_corrections +from backend.services.timing_sanitizer import find_missing_timings, sanitize_corrections def _sample_corrections(): @@ -57,14 +57,52 @@ def test_missing_segments_key_is_noop(): assert cleaned == {"foo": "bar"} -def test_non_finite_segment_bounds_left_untouched(): +def test_missing_segment_bounds_come_from_its_words(): payload = {"corrected_segments": [ - {"id": "s", "start_time": None, "end_time": None, "text": "x", - "words": [{"id": "w", "text": "x", "start_time": 3, "end_time": 5}]} + {"id": "s", "start_time": None, "end_time": None, "text": "x y", + "words": [{"id": "w", "text": "x", "start_time": 3, "end_time": 5}, + {"id": "v", "text": "y", "start_time": 5.5, "end_time": 6}]} ]} cleaned, count = sanitize_corrections(payload) - assert count == 0 - assert cleaned["corrected_segments"][0]["words"][0]["start_time"] == 3 + seg = cleaned["corrected_segments"][0] + assert (seg["start_time"], seg["end_time"]) == (3, 6) + assert count == 2 + assert seg["words"][0]["start_time"] == 3 + + +def test_segment_without_end_time_key_is_filled(): + """JSON.stringify drops undefined, so the key itself can be missing (job 16792df7).""" + payload = {"corrected_segments": [ + {"id": "s", "start_time": 1.0, "text": "x", + "words": [{"id": "w", "text": "x", "start_time": 1.0, "end_time": 1.4}]} + ]} + cleaned, _ = sanitize_corrections(payload) + assert cleaned["corrected_segments"][0]["end_time"] == 1.4 + assert find_missing_timings(cleaned) is None + + +def test_untimed_segment_takes_its_neighbours_gap_and_words_get_filled(): + payload = {"corrected_segments": [ + {"id": "a", "start_time": 0.0, "end_time": 2.0, "text": "a", + "words": [{"id": "a1", "text": "a", "start_time": 0.0, "end_time": 2.0}]}, + {"id": "b", "text": "new line", + "words": [{"id": "b1", "text": "new"}, {"id": "b2", "text": "line"}]}, + {"id": "c", "start_time": 5.0, "end_time": 6.0, "text": "c", + "words": [{"id": "c1", "text": "c", "start_time": 5.0, "end_time": 6.0}]}, + ]} + cleaned, _ = sanitize_corrections(payload) + seg = cleaned["corrected_segments"][1] + assert (seg["start_time"], seg["end_time"]) == (2.0, 5.0) + assert all(2.0 <= w["start_time"] <= w["end_time"] <= 5.0 for w in seg["words"]) + assert find_missing_timings(cleaned) is None + + +def test_line_with_no_timing_anywhere_is_reported(): + payload = {"corrected_segments": [ + {"id": "b", "text": "lonely line", "words": [{"id": "b1", "text": "lonely"}]}, + ]} + cleaned, _ = sanitize_corrections(payload) + assert find_missing_timings(cleaned) == "lonely line" def test_start_past_segment_end_uses_prev_end(): diff --git a/backend/tests/test_routes_review.py b/backend/tests/test_routes_review.py index 307c5336..2dffc842 100644 --- a/backend/tests/test_routes_review.py +++ b/backend/tests/test_routes_review.py @@ -2558,3 +2558,52 @@ class TestCorrectionDataReviewTransitionIdempotent: ) assert resp.status_code == 500 jm.record_review_started.assert_not_called() + + +class TestPreviewVideoMissingTimings: + """POST /preview-video with an untimed in-browser edit: fill it, or 422 naming the line (was a 500 KeyError).""" + + @pytest.fixture(autouse=True) + def auth_overrides(self): + from backend.main import app + from backend.api.dependencies import require_review_auth + + async def mock_require_review_auth(job_id: str = "test123"): + return (job_id, "full") + + app.dependency_overrides[require_review_auth] = mock_require_review_auth + yield + app.dependency_overrides.pop(require_review_auth, None) + + def _post(self, test_client, payload): + from backend.models.job import JobStatus + + mock_job = MagicMock() + mock_job.status = JobStatus.IN_REVIEW + encoding = MagicMock() + encoding.is_preview_enabled = False # local render path + render = AsyncMock(return_value="hash123") + with patch("backend.api.routes.review.JobManager") as mock_jm, \ + patch("backend.api.routes.review.StorageService"), \ + patch("backend.api.routes.review.get_encoding_service", return_value=encoding), \ + patch("backend.api.routes.review._render_preview_locally", render): + mock_jm.return_value.get_job.return_value = mock_job + resp = test_client.post("/api/review/test123/preview-video", json=payload) + return resp, render + + def test_unrecoverable_line_is_a_422_naming_it(self, test_client): + payload = {"corrections": [], "corrected_segments": [ + {"id": "s", "text": "lonely line", "words": [{"id": "w", "text": "lonely"}]}]} + resp, render = self._post(test_client, payload) + assert resp.status_code == 422, resp.text + assert "lonely line" in resp.json()["detail"] + render.assert_not_awaited() + + def test_fillable_gaps_reach_the_renderer_filled(self, test_client): + payload = {"corrections": [], "corrected_segments": [ + {"id": "s", "text": "x", "start_time": 1.0, + "words": [{"id": "w", "text": "x", "start_time": 1.0, "end_time": 1.5}]}]} + resp, render = self._post(test_client, payload) + assert resp.status_code == 200, resp.text + sent = render.await_args.args[1] + assert sent["corrected_segments"][0]["end_time"] == 1.5 diff --git a/backend/translations/de.json b/backend/translations/de.json index 3c63a4d3..cd759f98 100644 --- a/backend/translations/de.json +++ b/backend/translations/de.json @@ -81,7 +81,8 @@ "waveformGenerationError": "Fehler beim Generieren der Wellenform-Daten: {error}", "waveformNoAudio": "Kein Audio für Wellenform verfügbar", "sessionSaveError": "Korrekturdaten sind erforderlich", - "invalidIsDuetFlag": "is_duet muss ein Boolean sein (true oder false)" + "invalidIsDuetFlag": "is_duet muss ein Boolean sein (true oder false)", + "previewMissingTimings": "Die Zeile \"{line}\" enthält ein Wort ohne Timing. Synchronisiere diese Zeile und versuche die Vorschau dann erneut." }, "audioSearch": { "notEnabled": "Die Audiosuche ist für diesen Mandanten nicht aktiviert", diff --git a/backend/translations/en.json b/backend/translations/en.json index 740575cd..e9eec3ed 100644 --- a/backend/translations/en.json +++ b/backend/translations/en.json @@ -81,7 +81,8 @@ "waveformGenerationError": "Error generating waveform data: {error}", "waveformNoAudio": "No audio available for waveform", "sessionSaveError": "Correction data is required", - "invalidIsDuetFlag": "is_duet must be a boolean (true or false)" + "invalidIsDuetFlag": "is_duet must be a boolean (true or false)", + "previewMissingTimings": "The line \"{line}\" has a word with no timing. Sync that line, then try the preview again." }, "audioSearch": { "notEnabled": "Audio search is not enabled for this tenant", diff --git a/backend/translations/es.json b/backend/translations/es.json index 1d160e2f..16f86c2d 100644 --- a/backend/translations/es.json +++ b/backend/translations/es.json @@ -81,7 +81,8 @@ "waveformGenerationError": "Error al generar los datos de la forma de onda: {error}", "waveformNoAudio": "No hay audio disponible para la forma de onda", "sessionSaveError": "Se requieren datos de corrección", - "invalidIsDuetFlag": "is_duet debe ser un booleano (true o false)" + "invalidIsDuetFlag": "is_duet debe ser un booleano (true o false)", + "previewMissingTimings": "La línea \"{line}\" tiene una palabra sin sincronizar. Sincroniza esa línea y vuelve a intentar la vista previa." }, "audioSearch": { "notEnabled": "La búsqueda de audio no está habilitada para este inquilino", diff --git a/pyproject.toml b/pyproject.toml index 5919beff..176716e5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "karaoke-gen" -version = "0.278.14" +version = "0.278.15" 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 "] license = "MIT"