Upload video bg via converter extension

d05373cdd2e8fbce1b3a71895ea3fa437e6047ac

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +54 -4Ignore whitespace
public/global.d.ts+11 -0
@@ -55,4 +55,15 @@ declare global {
5555 * @param provider Translation provider
5656 */
5757 async function translate(text: string, lang: string, provider: string = null): Promise<string>;
58+
59+ interface ConvertVideoArgs {
60+ buffer: Uint8Array;
61+ name: string;
62+ }
63+
64+ /**
65+ * Converts a video file to an animated WebP format using FFmpeg.
66+ * @param args - The arguments for the conversion function.
67+ */
68+ function convertVideoToAnimatedWebp(args: ConvertVideoArgs): Promise<Uint8Array>;
5869}
public/index.html+1 -1
@@ -4920,7 +4920,7 @@
49204920 <div id="bg_menu_content" class="bg_list">
49214921 <form id="form_bg_download" class="bg_example no-border no-shadow" action="javascript:void(null);" method="post" enctype="multipart/form-data">
49224922 <label class="input-file">
49234923 <input type="file" id="add_bg_button" name="avatar" accept="image/png, image/jpeg, image/jpg, image/gif*, imagevideo/bmp*">
49244924 <div class="bg_example no-border no-shadow add_bg_but" style="background-image: url('/img/addbg3.png');"></div>
49254925 </label>
49264926 </form>
public/scripts/backgrounds.js+42 -3
@@ -1,7 +1,7 @@
11import { Fuse } from '../lib.js';
22
33import { callPopup, chat_metadata, eventSource, event_types, generateQuietPrompt, getCurrentChatId, getRequestHeaders, getThumbnailUrl, saveSettingsDebounced } from '../script.js';
44import { openThirdPartyExtensionMenu, saveMetadataDebounced } from './extensions.js';
55import { SlashCommand } from './slash-commands/SlashCommand.js';
66import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
77import { flashHighlight, stringFormat } from './utils.js';
@@ -77,7 +77,7 @@ function getChatBackgroundsList() {
7777}
7878
7979function getBackgroundPath(fileUrl) {
8080 return `backgrounds/${encodeURIComponent(fileUrl)}`;
8181}
8282
8383function highlightLockedBackground() {
@@ -438,7 +438,7 @@ async function delBackground(bg) {
438438 });
439439}
440440
441441async function onBackgroundUploadSelected() {
442442 const form = $('#form_bg_download').get(0);
443443
444444 if (!(form instanceof HTMLFormElement)) {
@@ -447,11 +447,50 @@ function onBackgroundUploadSelected() {
447447 }
448448
449449 const formData = new FormData(form);
450+ await convertFileIfVideo(formData);
450451 uploadBackground(formData);
451452 form.reset();
452453}
453454
454455/**
456+ * Converts a video file to an animated webp format if the file is a video.
457+ * @param {FormData} formData
458+ * @returns {Promise<void>}
459+ */
460+async function convertFileIfVideo(formData) {
461+ const file = formData.get('avatar');
462+ if (!(file instanceof File)) {
463+ return;
464+ }
465+ if (!file.type.startsWith('video/')) {
466+ return;
467+ }
468+ if (typeof globalThis.convertVideoToAnimatedWebp !== 'function') {
469+ toastr.warning('Click here to install the Video Background Loader extension', 'Video background uploads require a downloadable add-on', {
470+ timeOut: 0,
471+ extendedTimeOut: 0,
472+ onclick: () => openThirdPartyExtensionMenu('https://github.com/SillyTavern/Extension-VideoBackgroundLoader'),
473+ });
474+ return;
475+ }
476+
477+ let toastMessage = jQuery();
478+ try {
479+ toastMessage = toastr.info('Preparing video for upload...', 'Please wait', { timeOut: 0, extendedTimeOut: 0 });
480+ const sourceBuffer = await file.arrayBuffer();
481+ const convertedBuffer = await globalThis.convertVideoToAnimatedWebp({ buffer: new Uint8Array(sourceBuffer), name: file.name });
482+ const convertedFileName = file.name.replace(/\.[^/.]+$/, '.webp');
483+ const convertedFile = new File([convertedBuffer], convertedFileName, { type: 'image/webp' });
484+ formData.set('avatar', convertedFile);
485+ toastMessage.remove();
486+ } catch (error) {
487+ toastMessage.remove();
488+ console.error('Error converting video to animated webp:', error);
489+ toastr.error('Error converting video to animated webp');
490+ }
491+}
492+
493+/**
455494 * Uploads a background to the server
456495 * @param {FormData} formData
457496 */