calibrate gif size, probe codecs, align audio with video
- sample-encode gif calibration before the retry loop - probe ffmpeg encoders and grey out missing codecs in settings - pad audio head with silence so it lines up with video start - cache gif preview palette keyed on colors and sample window - read encode progress from -progress pipe:1 instead of stderr scraping - thread bt709 color metadata through capture, fix, and gif scale - sweep temp on startup - gif estimate table for the ui, interpolated locally on slider drag
This commit is contained in:
+54
-18
@@ -74,17 +74,21 @@
|
||||
// Derived
|
||||
let trimmedDuration = $derived(Math.max(0.1, trimEnd - trimStart))
|
||||
// GIF size/dimensions estimate - computed by the Rust backend so the
|
||||
// math stays in lockstep with encode.rs::gif_params.
|
||||
// math stays in lockstep with encode.rs::gif_params. We fetch a table of
|
||||
// estimates across a target-bytes grid once per parameter set (fps, dims,
|
||||
// duration, mode, colors) and interpolate on slider drag locally to avoid
|
||||
// one IPC per debounce tick.
|
||||
let gifEstimate: { width: number; fps: number; height: number; dims_label: string } | null = $state(null)
|
||||
let gifEstimateTimer: number | null = null
|
||||
let gifTable: { key: string; points: { target_bytes: number; width: number; fps: number; height: number }[] } | null = $state(null)
|
||||
|
||||
async function refreshGifEstimate() {
|
||||
if (!clip || format !== "gif") { gifEstimate = null; return }
|
||||
async function refreshGifTable() {
|
||||
if (!clip || format !== "gif") { gifTable = null; return }
|
||||
const key = `${fps}|${gifBudgetMode}|${gifColors}|${clip.width}x${clip.height}|${trimmedDuration}`
|
||||
try {
|
||||
gifEstimate = await api.estimateGifParams({
|
||||
const table = await api.estimateGifParamsTable({
|
||||
format: "gif",
|
||||
fps,
|
||||
target_bytes: Math.round(targetMB * 1_048_576),
|
||||
gif_budget_mode: gifBudgetMode,
|
||||
gif_dither: gifDither,
|
||||
gif_colors: gifColors,
|
||||
@@ -92,26 +96,50 @@
|
||||
clip_height: clip.height,
|
||||
duration_secs: trimmedDuration,
|
||||
})
|
||||
gifTable = { key, points: table.points }
|
||||
} catch {
|
||||
gifEstimate = null
|
||||
gifTable = null
|
||||
}
|
||||
}
|
||||
|
||||
// Debounced reactive estimate: any change to the inputs re-estimates
|
||||
// ~150 ms after the last change. Cancels in-flight when inputs change again.
|
||||
// Interpolate the cached table for the current target_bytes. GIF width/fps
|
||||
// are step functions of budget, so we find the bracketing grid points and
|
||||
// pick the lower one (slightly conservative on dims).
|
||||
function interpolateTable(): { width: number; fps: number; height: number; dims_label: string } | null {
|
||||
if (!gifTable || gifTable.points.length === 0) return null
|
||||
const target = Math.round(targetMB * 1_048_576)
|
||||
const pts = gifTable.points
|
||||
let chosen = pts[0]
|
||||
for (const p of pts) {
|
||||
if (p.target_bytes <= target) chosen = p
|
||||
else break
|
||||
}
|
||||
const { width, fps, height } = chosen
|
||||
return { width, fps, height, dims_label: `${width}x${height} @ ${fps}fps` }
|
||||
}
|
||||
|
||||
// Debounced: re-fetch the table when any non-target parameter changes;
|
||||
// re-interpolate when targetMB changes (local, no IPC).
|
||||
$effect(() => {
|
||||
// Touch every input so the effect re-runs on any of them changing.
|
||||
void phase; void clip; void format; void fps; void targetMB
|
||||
void gifBudgetMode; void gifDither; void gifColors
|
||||
void trimStart; void trimEnd
|
||||
if (phase !== "editing" || !clip) return
|
||||
if (gifEstimateTimer !== null) clearTimeout(gifEstimateTimer)
|
||||
gifEstimateTimer = window.setTimeout(() => { void refreshGifEstimate() }, 150)
|
||||
void phase; void clip; void format
|
||||
if (phase !== "editing" || !clip || format !== "gif") { gifTable = null; gifEstimate = null; return }
|
||||
const key = `${fps}|${gifBudgetMode}|${gifColors}|${clip.width}x${clip.height}|${trimmedDuration}`
|
||||
if (!gifTable || gifTable.key !== key) {
|
||||
if (gifEstimateTimer !== null) clearTimeout(gifEstimateTimer)
|
||||
gifEstimateTimer = window.setTimeout(() => { void refreshGifTable() }, 150)
|
||||
}
|
||||
return () => {
|
||||
if (gifEstimateTimer !== null) { clearTimeout(gifEstimateTimer); gifEstimateTimer = null }
|
||||
}
|
||||
})
|
||||
|
||||
// Local interpolation on targetMB changes - no IPC.
|
||||
$effect(() => {
|
||||
void targetMB
|
||||
if (phase !== "editing" || !clip || format !== "gif") { gifEstimate = null; return }
|
||||
gifEstimate = interpolateTable()
|
||||
})
|
||||
|
||||
let previewDims = $derived.by(() => {
|
||||
if (!clip) return ""
|
||||
if (format === "gif") {
|
||||
@@ -267,14 +295,22 @@
|
||||
if (format !== "gif") { thumbnails = []; return }
|
||||
const cacheKey = `${format}-${fps}`
|
||||
const cached = thumbCache.get(cacheKey)
|
||||
if (cached) { thumbnails = cached; deletedFrames = new Set(); return }
|
||||
if (cached) {
|
||||
// Promote to most-recently-used so eviction is LRU, not FIFO.
|
||||
// Map iteration is insertion-order; delete + re-set moves the key to the end.
|
||||
thumbCache.delete(cacheKey)
|
||||
thumbCache.set(cacheKey, cached)
|
||||
thumbnails = cached
|
||||
deletedFrames = new Set()
|
||||
return
|
||||
}
|
||||
loadingThumbs = true
|
||||
try {
|
||||
const thumbs = await api.extractThumbnails(fps)
|
||||
thumbCache.set(cacheKey, thumbs)
|
||||
if (thumbCache.size > 5) {
|
||||
const firstKey = thumbCache.keys().next().value
|
||||
if (firstKey) thumbCache.delete(firstKey)
|
||||
const oldestKey = thumbCache.keys().next().value
|
||||
if (oldestKey) thumbCache.delete(oldestKey)
|
||||
}
|
||||
thumbnails = thumbs
|
||||
deletedFrames = new Set()
|
||||
|
||||
Reference in New Issue
Block a user