mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-18 07:51:27 +00:00
Preserve frame AI annotations and reconcile removed libraries
This commit is contained in:
@@ -392,6 +392,116 @@ def _embed_jellyfin_trickplay(
|
||||
return rows, dim
|
||||
|
||||
|
||||
FRAME_ANNOTATION_COLUMNS = (
|
||||
"caption",
|
||||
"caption_model",
|
||||
"captioned_at",
|
||||
"caption_route",
|
||||
"caption_route_reason",
|
||||
"route_score",
|
||||
"route_model",
|
||||
"route_policy",
|
||||
"routed_at",
|
||||
)
|
||||
|
||||
|
||||
def _load_frame_annotations(media_id: str) -> dict[str, tuple]:
|
||||
with connect(settings.db_path) as con:
|
||||
rows = con.execute(
|
||||
"""
|
||||
SELECT id, caption, caption_model, captioned_at,
|
||||
caption_route, caption_route_reason, route_score,
|
||||
route_model, route_policy, routed_at
|
||||
FROM frames
|
||||
WHERE media_id = ?
|
||||
""",
|
||||
(media_id,),
|
||||
).fetchall()
|
||||
|
||||
return {
|
||||
str(row["id"]): tuple(row[column] for column in FRAME_ANNOTATION_COLUMNS)
|
||||
for row in rows
|
||||
}
|
||||
|
||||
|
||||
def _restore_frame_annotations(
|
||||
con,
|
||||
annotations: dict[str, tuple],
|
||||
frame_ids: set[str],
|
||||
) -> int:
|
||||
matches = [
|
||||
(*values, frame_id)
|
||||
for frame_id, values in annotations.items()
|
||||
if frame_id in frame_ids
|
||||
]
|
||||
if not matches:
|
||||
return 0
|
||||
|
||||
con.executemany(
|
||||
"""
|
||||
UPDATE frames
|
||||
SET caption = ?, caption_model = ?, captioned_at = ?,
|
||||
caption_route = ?, caption_route_reason = ?, route_score = ?,
|
||||
route_model = ?, route_policy = ?, routed_at = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
matches,
|
||||
)
|
||||
return len(matches)
|
||||
|
||||
|
||||
def reconcile_removed_libraries(client: JellyfinClient) -> dict[str, object]:
|
||||
"""Prune canonical rows whose Jellyfin library no longer exists."""
|
||||
current_ids = {
|
||||
str(library.get("Id") or "")
|
||||
for library in client.media_folders()
|
||||
if str(library.get("Id") or "")
|
||||
}
|
||||
|
||||
removed_library_ids: list[str] = []
|
||||
removed_media_ids: list[str] = []
|
||||
with connect(settings.db_path) as con:
|
||||
rows = con.execute(
|
||||
"""
|
||||
SELECT id, jellyfin_library_id
|
||||
FROM media
|
||||
WHERE source_kind = 'jellyfin'
|
||||
AND jellyfin_library_id IS NOT NULL
|
||||
AND jellyfin_library_id != ''
|
||||
"""
|
||||
).fetchall()
|
||||
|
||||
stale = [
|
||||
row
|
||||
for row in rows
|
||||
if str(row["jellyfin_library_id"]) not in current_ids
|
||||
]
|
||||
removed_library_ids = sorted(
|
||||
{str(row["jellyfin_library_id"]) for row in stale}
|
||||
)
|
||||
removed_media_ids = [str(row["id"]) for row in stale]
|
||||
|
||||
if removed_library_ids:
|
||||
placeholders = ",".join("?" for _ in removed_library_ids)
|
||||
con.execute(
|
||||
f"""
|
||||
DELETE FROM media
|
||||
WHERE source_kind = 'jellyfin'
|
||||
AND jellyfin_library_id IN ({placeholders})
|
||||
""",
|
||||
removed_library_ids,
|
||||
)
|
||||
|
||||
for media_id in removed_media_ids:
|
||||
shutil.rmtree(settings.thumbs_dir / media_id, ignore_errors=True)
|
||||
|
||||
return {
|
||||
"removed_library_ids": removed_library_ids,
|
||||
"removed_libraries": len(removed_library_ids),
|
||||
"removed_media": len(removed_media_ids),
|
||||
}
|
||||
|
||||
|
||||
def _reconcile_library(library_id: str, current_item_ids: set[str]) -> int:
|
||||
stale_rows = []
|
||||
with connect(settings.db_path) as con:
|
||||
@@ -625,6 +735,20 @@ def _index_library(
|
||||
)
|
||||
|
||||
size_bytes = int(source.get("Size") or 0)
|
||||
|
||||
# Preserve AI work when the underlying media still appears to be the
|
||||
# same source. Frame ids are deterministic by media/timestamp/model,
|
||||
# so only exact surviving frames receive the old annotations.
|
||||
annotations: dict[str, tuple] = {}
|
||||
if (
|
||||
existing
|
||||
and str(existing["source_path"] or "") == source_path_text
|
||||
and int(existing["size_bytes"] or 0) == size_bytes
|
||||
and str(existing["model_id"] or "") == settings.model_id
|
||||
and str(existing["jellyfin_tags"] or "") == tags_json
|
||||
):
|
||||
annotations = _load_frame_annotations(str(existing["id"]))
|
||||
|
||||
with connect(settings.db_path) as con:
|
||||
con.execute("DELETE FROM media WHERE id = ?", (media_id,))
|
||||
con.execute(
|
||||
@@ -670,9 +794,20 @@ def _index_library(
|
||||
""",
|
||||
frame_rows,
|
||||
)
|
||||
preserved = _restore_frame_annotations(
|
||||
con,
|
||||
annotations,
|
||||
{str(row[0]) for row in frame_rows},
|
||||
)
|
||||
|
||||
preserved_note = (
|
||||
f"; preserved {preserved} caption/route annotation(s)"
|
||||
if preserved
|
||||
else ""
|
||||
)
|
||||
print(
|
||||
f" stored {len(frame_rows)} frames; tags={_display_tags(tags)}"
|
||||
f"{preserved_note}"
|
||||
)
|
||||
indexed += 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user