mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-19 00:03:36 +00:00
Make caption startup responsive
This commit is contained in:
+102
-41
@@ -156,67 +156,128 @@ class CaptionJobManager:
|
|||||||
self._stop_requested.clear()
|
self._stop_requested.clear()
|
||||||
self._resume_event.set()
|
self._resume_event.set()
|
||||||
|
|
||||||
init_db(settings.db_path)
|
started_at = datetime.now(timezone.utc).isoformat()
|
||||||
args = SimpleNamespace(
|
|
||||||
force=force,
|
|
||||||
media_id=None,
|
|
||||||
title=None,
|
|
||||||
limit=None,
|
|
||||||
library_ids=library_ids,
|
|
||||||
)
|
|
||||||
rows = _select_caption_rows(args, only_needing_caption=True)
|
|
||||||
|
|
||||||
with connect(settings.db_path) as con:
|
|
||||||
placeholders = ",".join("?" for _ in library_ids)
|
|
||||||
totals = con.execute(
|
|
||||||
f"""
|
|
||||||
SELECT COUNT(*) AS total,
|
|
||||||
SUM(CASE WHEN f.caption IS NOT NULL THEN 1 ELSE 0 END) AS captioned
|
|
||||||
FROM frames f
|
|
||||||
JOIN media m ON m.id = f.media_id
|
|
||||||
WHERE m.source_kind = 'jellyfin'
|
|
||||||
AND m.jellyfin_library_id IN ({placeholders})
|
|
||||||
""",
|
|
||||||
library_ids,
|
|
||||||
).fetchone()
|
|
||||||
|
|
||||||
indexed_frames = int(totals["total"] or 0)
|
|
||||||
already_captioned = int(totals["captioned"] or 0)
|
|
||||||
|
|
||||||
with self._state_lock:
|
with self._state_lock:
|
||||||
self._state = {
|
self._state = {
|
||||||
"running": bool(rows),
|
"running": True,
|
||||||
"stage": "starting" if rows else "complete",
|
"stage": "starting",
|
||||||
"started_at": datetime.now(timezone.utc).isoformat(),
|
"started_at": started_at,
|
||||||
"finished_at": None if rows else datetime.now(timezone.utc).isoformat(),
|
"finished_at": None,
|
||||||
"error": None,
|
"error": None,
|
||||||
"library_ids": library_ids,
|
"library_ids": library_ids,
|
||||||
"force": force,
|
"force": force,
|
||||||
"indexed_frames": indexed_frames,
|
"indexed_frames": 0,
|
||||||
"already_captioned": already_captioned,
|
"already_captioned": 0,
|
||||||
"frames_selected": len(rows),
|
"frames_selected": 0,
|
||||||
"frames_total": len(rows),
|
"frames_total": 0,
|
||||||
"frames_completed": 0,
|
"frames_completed": 0,
|
||||||
"failed": 0,
|
"failed": 0,
|
||||||
"retained": 0,
|
"retained": 0,
|
||||||
"percent": 0.0 if rows else 100.0,
|
"percent": 0.0,
|
||||||
"paused": False,
|
"paused": False,
|
||||||
"pause_requested": False,
|
"pause_requested": False,
|
||||||
"stop_requested": False,
|
"stop_requested": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
if not rows:
|
|
||||||
return self.status()
|
|
||||||
|
|
||||||
self._thread = threading.Thread(
|
self._thread = threading.Thread(
|
||||||
target=self._run,
|
target=self._prepare_and_run,
|
||||||
args=(args, rows),
|
args=(library_ids, force),
|
||||||
name="scene-search-caption",
|
name="scene-search-caption",
|
||||||
daemon=True,
|
daemon=True,
|
||||||
)
|
)
|
||||||
self._thread.start()
|
self._thread.start()
|
||||||
return self.status()
|
return self.status()
|
||||||
|
|
||||||
|
def _prepare_and_run(self, library_ids: list[str], force: bool) -> None:
|
||||||
|
try:
|
||||||
|
init_db(settings.db_path)
|
||||||
|
args = SimpleNamespace(
|
||||||
|
force=force,
|
||||||
|
media_id=None,
|
||||||
|
title=None,
|
||||||
|
limit=None,
|
||||||
|
library_ids=library_ids,
|
||||||
|
)
|
||||||
|
|
||||||
|
self._progress(
|
||||||
|
{
|
||||||
|
"stage": "starting",
|
||||||
|
"preparing": "Finding frames that need captions",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
rows = _select_caption_rows(args, only_needing_caption=True)
|
||||||
|
|
||||||
|
if self._stop_requested.is_set():
|
||||||
|
raise CaptionJobStopped()
|
||||||
|
|
||||||
|
with connect(settings.db_path) as con:
|
||||||
|
placeholders = ",".join("?" for _ in library_ids)
|
||||||
|
totals = con.execute(
|
||||||
|
f"""
|
||||||
|
SELECT COUNT(*) AS total,
|
||||||
|
SUM(CASE WHEN f.caption IS NOT NULL THEN 1 ELSE 0 END) AS captioned
|
||||||
|
FROM frames f
|
||||||
|
JOIN media m ON m.id = f.media_id
|
||||||
|
WHERE m.source_kind = 'jellyfin'
|
||||||
|
AND m.jellyfin_library_id IN ({placeholders})
|
||||||
|
""",
|
||||||
|
library_ids,
|
||||||
|
).fetchone()
|
||||||
|
|
||||||
|
indexed_frames = int(totals["total"] or 0)
|
||||||
|
already_captioned = int(totals["captioned"] or 0)
|
||||||
|
|
||||||
|
self._progress(
|
||||||
|
{
|
||||||
|
"indexed_frames": indexed_frames,
|
||||||
|
"already_captioned": already_captioned,
|
||||||
|
"frames_selected": len(rows),
|
||||||
|
"frames_total": len(rows),
|
||||||
|
"preparing": None,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if not rows:
|
||||||
|
self._progress(
|
||||||
|
{
|
||||||
|
"running": False,
|
||||||
|
"stage": "complete",
|
||||||
|
"finished_at": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"percent": 100.0,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
self._run(args, rows)
|
||||||
|
except CaptionJobStopped:
|
||||||
|
self._set_gpu_busy(False)
|
||||||
|
self._pause_requested.clear()
|
||||||
|
self._stop_requested.clear()
|
||||||
|
self._resume_event.set()
|
||||||
|
self._progress(
|
||||||
|
{
|
||||||
|
"running": False,
|
||||||
|
"stage": "stopped",
|
||||||
|
"finished_at": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"paused": False,
|
||||||
|
"pause_requested": False,
|
||||||
|
"stop_requested": False,
|
||||||
|
"preparing": None,
|
||||||
|
"error": None,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
self._set_gpu_busy(False)
|
||||||
|
self._progress(
|
||||||
|
{
|
||||||
|
"running": False,
|
||||||
|
"stage": "failed",
|
||||||
|
"finished_at": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"preparing": None,
|
||||||
|
"error": str(exc),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def _progress(self, update: dict[str, Any]) -> None:
|
def _progress(self, update: dict[str, Any]) -> None:
|
||||||
with self._state_lock:
|
with self._state_lock:
|
||||||
self._state.update(update)
|
self._state.update(update)
|
||||||
|
|||||||
Reference in New Issue
Block a user