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:
2026-06-17 23:09:53 +03:00
parent 4ca802ef71
commit f732cdda4d
7 changed files with 647 additions and 155 deletions
+54 -18
View File
@@ -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()
+20 -6
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import { onMount } from "svelte"
import { api, type Config } from "./api"
import { api, type Config, type CodecAvailability } from "./api"
import HotkeyInput from "./HotkeyInput.svelte"
import { open } from "@tauri-apps/plugin-dialog"
@@ -12,6 +12,7 @@
let showAdvanced: boolean = $state(false)
let activeTab: string = $state("capture")
let initialCfg: Config | null = $state(null)
let codecs: CodecAvailability | null = $state(null)
let dirty = $derived.by(() => {
if (!cfg || !initialCfg) return false
@@ -38,7 +39,11 @@
{ id: "system", label: "System" },
]
onMount(async () => { cfg = await api.getConfig(); initialCfg = structuredClone(cfg) })
onMount(async () => {
cfg = await api.getConfig()
initialCfg = structuredClone(cfg)
try { codecs = await api.codecAvailability() } catch { codecs = null }
})
$effect(() => {
if (cfg) document.documentElement.setAttribute("data-theme", cfg.theme)
@@ -165,17 +170,20 @@
<label class="field">
<span class="field-label">WebM codec</span>
<select bind:value={cfg.export.webm_codec}>
<option value="vp9">VP9 (default)</option>
<option value="av1">AV1 (smaller, slower)</option>
<option value="vp9" disabled={codecs ? !codecs.vp9 : false}>VP9 (default){codecs && !codecs.vp9 ? " - unavailable" : ""}</option>
<option value="av1" disabled={codecs ? !codecs.av1 : false}>AV1 (smaller, slower){codecs && !codecs.av1 ? " - unavailable" : ""}</option>
</select>
</label>
<label class="field">
<span class="field-label">MP4 codec</span>
<select bind:value={cfg.export.mp4_codec}>
<option value="h264">H.264 (default)</option>
<option value="h265">H.265 (smaller, less universal)</option>
<option value="h264" disabled={codecs ? !codecs.h264 : false}>H.264 (default){codecs && !codecs.h264 ? " - unavailable" : ""}</option>
<option value="h265" disabled={codecs ? !codecs.h265 : false}>H.265 (smaller, less universal){codecs && !codecs.h265 ? " - unavailable" : ""}</option>
</select>
</label>
{#if codecs && (!codecs.av1 || !codecs.h265)}
<p class="codec-note">Some codecs are unavailable in your ffmpeg build and disabled above.</p>
{/if}
{/if}
</div>
</div>
@@ -442,6 +450,12 @@
.advanced-toggle svg { transition: transform 150ms; }
.advanced-toggle svg.rotated { transform: rotate(180deg); }
.codec-note {
margin: 0;
font-size: 11px;
color: var(--warn);
}
.path-row { display: flex; gap: 6px; }
.path-input { flex: 1; font-family: var(--mono); font-size: 12px; }
+31
View File
@@ -87,6 +87,7 @@ export type ClickEvent = {
time_secs: number
x: number
y: number
button: "Left" | "Right" | "Middle"
}
export type RecordedClip = {
@@ -148,6 +149,24 @@ export type GifEstimate = {
dims_label: string
}
export type GifEstimatePoint = {
target_bytes: number
width: number
fps: number
height: number
}
export type GifEstimateTable = {
points: GifEstimatePoint[]
}
export type CodecAvailability = {
h264: boolean
h265: boolean
vp9: boolean
av1: boolean
}
export type AudioFailed = string
export const api = {
@@ -191,6 +210,17 @@ export const api = {
duration_secs: number
}) =>
invoke<GifEstimate>("estimate_gif_params", { request: input }),
estimateGifParamsTable: (input: {
format: Format
fps: number
gif_budget_mode?: GifBudgetMode
gif_dither?: GifDither
gif_colors?: number
clip_width: number
clip_height: number
duration_secs: number
}) =>
invoke<GifEstimateTable>("estimate_gif_params_table", { request: input }),
renderRawPreview: (timestamp: number) =>
invoke<string>("render_raw_preview", { timestamp }),
defaultOutputPath: (format: Format) =>
@@ -214,6 +244,7 @@ export const api = {
onAudioFailed: (handler: (msg: string) => void): Promise<UnlistenFn> =>
listen<string>("audio-failed", (e) => handler(e.payload)),
cancelExport: () => invoke<void>("cancel_export"),
codecAvailability: () => invoke<CodecAvailability>("codec_availability"),
}
export function formatBytes(n: number): string {