diff --git a/scripts/show_captions.sh b/scripts/show_captions.sh index 77a8745..47b460a 100644 --- a/scripts/show_captions.sh +++ b/scripts/show_captions.sh @@ -1,26 +1,55 @@ -docker compose exec -T scene-search python - <<'PY' > captions.txt +#!/usr/bin/env bash +set -Eeuo pipefail +source "$(cd -- "$(dirname -- "$0")" && pwd)/common.sh" + +need docker + +output_file="$REPO_ROOT/captions.txt" +temp_file="$(mktemp "$REPO_ROOT/.captions.txt.XXXXXX")" +trap 'rm -f -- "$temp_file"' EXIT + +# Export from the canonical SQLite database. The newer contact-sheet captions +# live in caption_windows, while older per-frame captions live in frames. +# Write to a temporary file so a Docker/SQL failure cannot truncate a +# previously successful export. +compose exec -T scene-search python - <<'PY' > "$temp_file" import sqlite3 -con = sqlite3.connect('/app/data/scenes.sqlite3') +con = sqlite3.connect("/app/data/scenes.sqlite3") con.row_factory = sqlite3.Row +try: + rows = con.execute(""" + SELECT m.title, m.jellyfin_item_id, + cw.start_ms AS timestamp_ms, cw.caption + FROM caption_windows cw + JOIN media m ON m.id = cw.media_id + WHERE cw.caption IS NOT NULL AND TRIM(cw.caption) != '' -for row in con.execute(""" - SELECT - m.title, - m.jellyfin_item_id, - f.timestamp_ms, - f.caption - FROM media m - JOIN frames f ON f.media_id = m.id - WHERE m.source_kind = 'jellyfin' - AND f.caption IS NOT NULL - AND TRIM(f.caption) != '' - ORDER BY m.title, f.timestamp_ms -"""): - seconds = row['timestamp_ms'] // 1000 - mins, secs = divmod(seconds, 60) - hours, mins = divmod(mins, 60) + UNION ALL - print(f"\n{row['title']} ({row['jellyfin_item_id']}) [{hours:02d}:{mins:02d}:{secs:02d}]") - print(row['caption']) + SELECT m.title, m.jellyfin_item_id, + f.timestamp_ms, f.caption + FROM frames f + JOIN media m ON m.id = f.media_id + WHERE f.caption IS NOT NULL AND TRIM(f.caption) != '' + + ORDER BY title, timestamp_ms + """) + + for row in rows: + seconds = int(row["timestamp_ms"]) // 1000 + hours, remainder = divmod(seconds, 3600) + minutes, seconds = divmod(remainder, 60) + timestamp = f"{hours:02d}:{minutes:02d}:{seconds:02d}" + caption = " ".join(row["caption"].split()) + + print(f"{row['title']} [{timestamp}]") + print(f"Jellyfin ID: {row['jellyfin_item_id']}") + print(f"Caption: {caption}") + print() +finally: + con.close() PY + +mv -f -- "$temp_file" "$output_file" +printf 'Saved all captions to %s\n' "$output_file"