Spaces:
Paused
Paused
File size: 8,091 Bytes
1246043 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | "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); });
|