Add caption controls to Jellyfin plugin page

This commit is contained in:
Sleeps
2026-10-07 11:43:45 +01:00
parent e896d3f94c
commit 6290dd0779
@@ -69,6 +69,29 @@
</div> </div>
<div id="indexStatus" class="fieldDescription" style="margin-bottom:1.5em;"></div> <div id="indexStatus" class="fieldDescription" style="margin-bottom:1.5em;"></div>
<h2>Captioning</h2>
<div class="fieldDescription" style="margin-bottom:1em;">
Send indexed frames from the selected libraries to the configured AI caption models. Start / resume skips frames that already have a current caption.
</div>
<div style="margin-bottom:1em;">
<button is="emby-button" type="button" id="captionNow" class="raised">
<span>Start / resume captioning</span>
</button>
<label class="checkboxContainer" style="margin-top:1em;">
<input is="emby-checkbox" type="checkbox" id="forceCaption" />
<span>Force recaption existing frames</span>
</label>
</div>
<div style="margin-bottom:.5em;">
<div style="display:flex;justify-content:space-between;gap:1em;">
<strong id="captionProgressText">0%</strong>
<span id="captionProgressFrames" class="fieldDescription">0 / 0 frames</span>
</div>
<progress id="captionProgress" max="100" value="0" style="width:100%;height:1.1em;"></progress>
</div>
<div id="captionStatus" class="fieldDescription" style="margin-bottom:.5em;">Captioning idle.</div>
<div id="captionDetail" class="fieldDescription" style="margin-bottom:1.5em;"></div>
<button is="emby-button" type="submit" class="raised button-submit block"> <button is="emby-button" type="submit" class="raised button-submit block">
<span>Save</span> <span>Save</span>
</button> </button>
@@ -82,6 +105,7 @@
var page = document.querySelector('#sceneSearchConfigPage'); var page = document.querySelector('#sceneSearchConfigPage');
var currentConfig = null; var currentConfig = null;
var statusTimer = null; var statusTimer = null;
var captionTimer = null;
function selectedLibraryIds() { function selectedLibraryIds() {
return Array.prototype.slice.call( return Array.prototype.slice.call(
@@ -186,6 +210,96 @@
}); });
} }
function formatTs(ms) {
var total = Math.floor((ms || 0) / 1000);
var hours = Math.floor(total / 3600);
var minutes = Math.floor((total % 3600) / 60);
var seconds = total % 60;
return [hours, minutes, seconds].map(function (value) {
return String(value).padStart(2, '0');
}).join(':');
}
function renderCaptionStatus(status) {
var progress = page.querySelector('#captionProgress');
var progressText = page.querySelector('#captionProgressText');
var progressFrames = page.querySelector('#captionProgressFrames');
var container = page.querySelector('#captionStatus');
var detail = page.querySelector('#captionDetail');
var button = page.querySelector('#captionNow');
if (!status) {
progress.value = 0;
progressText.textContent = '0%';
progressFrames.textContent = '0 / 0 frames';
container.textContent = 'Caption status unavailable.';
detail.textContent = '';
button.disabled = false;
return;
}
var percent = Number(status.percent || (status.stage === 'complete' ? 100 : 0));
progress.value = percent;
progressText.textContent = percent.toFixed(1).replace('.0', '') + '%';
progressFrames.textContent =
(status.frames_completed || 0) + ' / ' +
(status.frames_total || 0) + ' frames';
if (status.running) {
button.disabled = true;
if (status.stage === 'routing') {
container.textContent = 'Routing frames to caption models.';
detail.textContent = (status.frames_selected || status.frames_total || 0) + ' frames selected.';
} else if (status.stage === 'syncing' || status.stage === 'sync') {
container.textContent = 'Rebuilding search index: ' + (status.documents_processed || 0);
detail.textContent =
(status.frames_completed || 0) + ' captions saved, ' +
(status.failed || 0) + ' failed.';
} else {
container.textContent = 'Captioning: ' +
(status.title || 'current frame') +
(status.timestamp_ms != null ? ' at ' + formatTs(status.timestamp_ms) : '');
detail.textContent =
(status.model || '') +
(status.route ? ', ' + status.route : '') +
', ' + (status.failed || 0) + ' failed.';
}
} else if (status.stage === 'failed') {
button.disabled = false;
container.textContent = 'Captioning failed: ' + (status.error || 'unknown error');
detail.textContent = '';
} else if (status.stage === 'complete') {
button.disabled = false;
container.textContent =
'Captioning complete: ' + (status.frames_completed || 0) +
' saved, ' + (status.failed || 0) + ' failed.';
detail.textContent =
(status.retained || 0) + ' existing captions retained.';
} else {
button.disabled = false;
container.textContent = 'Captioning idle.';
detail.textContent = '';
}
}
function refreshCaptionStatus() {
return ApiClient.getJSON(ApiClient.getUrl('Plugins/SceneSearch/CaptionStatus'))
.then(function (status) {
renderCaptionStatus(status);
if (status && status.running) {
if (!captionTimer) {
captionTimer = setInterval(refreshCaptionStatus, 2000);
}
} else if (captionTimer) {
clearInterval(captionTimer);
captionTimer = null;
}
})
.catch(function () {
renderCaptionStatus(null);
});
}
function loadLibraries() { function loadLibraries() {
return ApiClient.getJSON(ApiClient.getUrl('Plugins/SceneSearch/Libraries')) return ApiClient.getJSON(ApiClient.getUrl('Plugins/SceneSearch/Libraries'))
.then(renderLibraries) .then(renderLibraries)
@@ -207,7 +321,7 @@
page.querySelector('#maxResults').value = config.MaxResults || 20; page.querySelector('#maxResults').value = config.MaxResults || 20;
page.querySelector('#timeoutSeconds').value = config.TimeoutSeconds || 60; page.querySelector('#timeoutSeconds').value = config.TimeoutSeconds || 60;
return Promise.all([loadLibraries(), refreshStatus()]); return Promise.all([loadLibraries(), refreshStatus(), refreshCaptionStatus()]);
}).finally(function () { }).finally(function () {
Dashboard.hideLoadingMsg(); Dashboard.hideLoadingMsg();
}); });
@@ -218,6 +332,10 @@
clearInterval(statusTimer); clearInterval(statusTimer);
statusTimer = null; statusTimer = null;
} }
if (captionTimer) {
clearInterval(captionTimer);
captionTimer = null;
}
}); });
page.querySelector('#indexNow').addEventListener('click', function () { page.querySelector('#indexNow').addEventListener('click', function () {
@@ -258,6 +376,37 @@
}); });
}); });
page.querySelector('#captionNow').addEventListener('click', function () {
var ids = selectedLibraryIds();
if (!ids.length) {
page.querySelector('#captionStatus').textContent = 'Select at least one library first.';
return;
}
page.querySelector('#captionStatus').textContent = 'Saving settings and starting captioning...';
ApiClient.getPluginConfiguration(pluginId).then(function (config) {
config.SelectedLibraryIds = ids;
currentConfig = config;
return ApiClient.updatePluginConfiguration(pluginId, config);
}).then(function () {
return ApiClient.ajax({
type: 'POST',
url: ApiClient.getUrl('Plugins/SceneSearch/Caption'),
contentType: 'application/json',
data: JSON.stringify({
library_ids: ids,
force: page.querySelector('#forceCaption').checked
})
});
}).then(function () {
refreshCaptionStatus();
}).catch(function () {
page.querySelector('#captionStatus').textContent =
'Could not start captioning. Check Jellyfin logs and Scene Search API status.';
});
});
page.querySelector('.sceneSearchConfigForm').addEventListener('submit', function (event) { page.querySelector('.sceneSearchConfigForm').addEventListener('submit', function (event) {
event.preventDefault(); event.preventDefault();
Dashboard.showLoadingMsg(); Dashboard.showLoadingMsg();