bfs-video-studio / studio.js
Simzy's picture
Upload folder using huggingface_hub
1246043 verified
Raw History Blame Contribute Delete
8.09 kB
"use strict";
const byId = (id) => document.getElementById(id);
const faceInput = byId("face-file"), videoInput = byId("video-file");
const face = byId("face-preview"), video = byId("guide-player"), canvas = byId("guide-canvas");
const context = canvas.getContext("2d");
let faceUrl, videoUrl, ready = false, frameRequest = 0, sourceFace, sourceVideo;
const MODEL = "Alissonerdx/BFS-Best-Face-Swap-Video";
function message(text, error = false) {
byId("message").textContent = text;
byId("message").classList.toggle("error", error);
}
function stop() {
video.pause();
cancelAnimationFrame(frameRequest);
byId("play-guide").textContent = "Play preview";
}
function invalidate() {
ready = false; stop();
canvas.hidden = true;
byId("preview-placeholder").hidden = false;
for (const id of ["play-guide", "download-frame", "download-setup"]) byId(id).disabled = true;
}
function fileError(text, input) { input.value = ""; message(text, true); }
faceInput.addEventListener("change", () => {
invalidate();
sourceFace = undefined;
face.hidden = true;
byId("face-placeholder").hidden = false;
byId("face-name").textContent = "The identity you want to use";
const file = faceInput.files[0];
if (!file) return;
if (!/^image\/(jpeg|png|webp)$/.test(file.type)) return fileError("Choose a JPG, PNG, or WebP portrait.", faceInput);
if (file.size > 20 * 1024 * 1024) return fileError("The reference image must be smaller than 20 MB.", faceInput);
if (faceUrl) URL.revokeObjectURL(faceUrl);
faceUrl = URL.createObjectURL(file);
face.onload = () => {
if (face.naturalWidth * face.naturalHeight > 40000000) return fileError("Please resize this portrait to under 40 megapixels.", faceInput);
sourceFace = file;
face.hidden = false; byId("face-placeholder").hidden = true;
byId("face-name").textContent = file.name;
message("Portrait ready. Add a guide video, then preview your setup.");
};
face.onerror = () => fileError("This image could not be opened. Try a different file.", faceInput);
face.src = faceUrl;
});
videoInput.addEventListener("change", () => {
invalidate();
sourceVideo = undefined;
video.hidden = true; byId("video-placeholder").hidden = false;
byId("video-name").textContent = "The motion and scene you want to keep";
const file = videoInput.files[0];
if (!file) return;
if (!file.type.startsWith("video/")) return fileError("Choose an MP4, WebM, or MOV video.", videoInput);
if (file.size > 150 * 1024 * 1024) return fileError("The guide video must be smaller than 150 MB.", videoInput);
if (videoUrl) URL.revokeObjectURL(videoUrl);
videoUrl = URL.createObjectURL(file);
video.onloadeddata = () => {
if (!Number.isFinite(video.duration) || video.duration < 0.4) return fileError("Use a video at least 0.4 seconds long.", videoInput);
sourceVideo = file;
video.hidden = false; byId("video-placeholder").hidden = true;
byId("video-name").textContent = `${file.name} · ${video.duration.toFixed(1)}s`;
message("Guide ready. Preview your V3 reference layout below.");
};
video.onerror = () => fileError("Your browser cannot decode this video. Try an H.264 MP4.", videoInput);
video.src = videoUrl; video.load();
});
function dimensions() {
const cap = Number(byId("resolution").value);
const scale = cap / Math.max(video.videoWidth, video.videoHeight);
return [Math.max(128, Math.round(video.videoWidth * scale / 32) * 32),
Math.max(128, Math.round(video.videoHeight * scale / 32) * 32)];
}
function draw() {
if (!ready) return;
const [w, h] = [canvas.width, canvas.height], strip = Math.max(32, Math.round(w * .25));
context.fillStyle = "#000"; context.fillRect(0, 0, w, h);
context.fillStyle = "#00ff00"; context.fillRect(0, 0, strip, h);
const scale = Math.min((w - strip) / video.videoWidth, h / video.videoHeight);
const vw = video.videoWidth * scale, vh = video.videoHeight * scale;
context.drawImage(video, strip + (w - strip - vw) / 2, (h - vh) / 2, vw, vh);
const pw = face.naturalWidth + 32, ph = face.naturalHeight + 32;
const ps = Math.min((strip - 24) / pw, (h - 24) / ph);
const px = (strip - pw * ps) / 2, py = (h - ph * ps) / 2;
context.fillStyle = "white"; context.fillRect(px, py, pw * ps, ph * ps);
context.drawImage(face, px + 16 * ps, py + 16 * ps, face.naturalWidth * ps, face.naturalHeight * ps);
}
function clipDuration() {
const count = Math.floor(Math.min(Number(byId("duration").value), video.duration) * 24);
return Math.max(9, Math.floor((count - 1) / 8) * 8 + 1) / 24;
}
function animate() {
if (!ready || video.paused) return;
if (video.currentTime >= clipDuration()) video.currentTime = 0;
draw(); frameRequest = requestAnimationFrame(animate);
}
video.addEventListener("seeked", draw);
video.addEventListener("ended", stop);
byId("prepare").addEventListener("click", () => {
if (!sourceFace) return message("Add a reference portrait to begin.", true);
if (!sourceVideo) return message("Add a guide video to preview the reference layout.", true);
stop();
const [w, h] = dimensions();
canvas.width = w; canvas.height = h;
ready = true; canvas.hidden = false; byId("preview-placeholder").hidden = true;
video.currentTime = 0; draw();
for (const id of ["play-guide", "download-frame", "download-setup"]) byId(id).disabled = false;
message(`Guide ready: ${w} × ${h}, ${clipDuration().toFixed(2)} seconds at 24 fps. This is an input preview, not a generated face swap.`);
});
for (const id of ["duration", "resolution"]) byId(id).addEventListener("change", () => {
if (ready) { invalidate(); message("Settings changed. Preview again to update the guide."); }
});
byId("play-guide").addEventListener("click", async () => {
if (!ready) return;
if (!video.paused) return stop();
try {
if (video.currentTime >= clipDuration()) video.currentTime = 0;
await video.play(); byId("play-guide").textContent = "Pause preview"; animate();
} catch { message("Playback could not start. Try an H.264 MP4.", true); }
});
function save(blob, name) {
const url = URL.createObjectURL(blob), anchor = document.createElement("a");
anchor.href = url; anchor.download = name; anchor.click();
setTimeout(() => URL.revokeObjectURL(url), 5000);
}
byId("download-frame").addEventListener("click", () => {
if (!ready) return;
draw(); canvas.toBlob((blob) => { if (blob) save(blob, "bfs-v3-guide-frame.png"); }, "image/png");
});
byId("download-setup").addEventListener("click", () => {
if (!ready) return;
const seed = Number(byId("seed").value);
if (!Number.isInteger(seed) || seed < 0 || seed >= 2**31) return message("Choose a whole-number seed from 0 to 2147483647.", true);
const identity = byId("face-description").value.trim() || "Use the face, hair, and identity of the reference portrait. Preserve natural skin texture.";
const action = byId("action-description").value.trim() || "A person performs the motion in the guide video. Preserve clothing, body movement, camera framing, and environment.";
const data = {
format: "bfs-video-studio-setup-v1", model: MODEL,
adapter: "ltx-2.3/head_swap_v3_rank_64.safetensors", base_model: "Lightricks/LTX-2.3",
reference_filename: sourceFace.name, video_filename: sourceVideo.name,
duration_seconds: Number(byId("duration").value), actual_duration_seconds: clipDuration(),
resolution: Number(byId("resolution").value), width: canvas.width, height: canvas.height,
fps: 24, num_frames: Math.round(clipDuration() * 24), seed, steps: 8, lora_strength: 1,
prompt: `head_swap:\nFACE:\n${identity}\n\nACTION:\n${action}`,
note: "Preparation settings only; not a ComfyUI graph or a generated video. Input media are not included."
};
save(new Blob([JSON.stringify(data, null, 2) + "\n"], {type: "application/json"}), "bfs-v3-setup.json");
message("Setup downloaded. The JSON contains settings and filenames; your media stays on your device.");
});
window.addEventListener("pagehide", () => { stop(); if (faceUrl) URL.revokeObjectURL(faceUrl); if (videoUrl) URL.revokeObjectURL(videoUrl); });