fps selector on idle screen, lower gif fps minimum, 16:9 preset, aspect ratio lock on frame resize, gif budget mode (resolution/framerate/balanced priority), fix asset protocol scope
This commit is contained in:
+96
-11
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte"
|
||||
import { api, convertFileSrc, formatBytes, type Config, type Format, type RecordedClip, type ExportResult, type CaptureRect } from "./api"
|
||||
import { api, convertFileSrc, formatBytes, type Config, type Format, type GifBudgetMode, type RecordedClip, type ExportResult, type CaptureRect } from "./api"
|
||||
import { save } from "@tauri-apps/plugin-dialog"
|
||||
import { emit } from "@tauri-apps/api/event"
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
let outputPath: string = $state("")
|
||||
let pathOverridden: boolean = $state(false)
|
||||
|
||||
let gifBudgetMode: GifBudgetMode = $state("balanced")
|
||||
|
||||
// Post-record crop
|
||||
let cropEnabled: boolean = $state(false)
|
||||
let cropX: number = $state(0)
|
||||
@@ -50,10 +52,12 @@
|
||||
let previewTimer: number | null = null
|
||||
let thumbCache: Map<string, string[]> = $state(new Map())
|
||||
let showDiscardConfirm: boolean = $state(false)
|
||||
let aspectLock: number | null = $state(null)
|
||||
|
||||
const PRESETS: [number, number, string][] = [
|
||||
[1280, 720, "720p"],
|
||||
[1920, 1080, "1080p"],
|
||||
[960, 540, "16:9"],
|
||||
[800, 800, "1:1"],
|
||||
[540, 960, "9:16"],
|
||||
]
|
||||
@@ -64,13 +68,38 @@
|
||||
if (!clip) return ""
|
||||
if (format === "gif") {
|
||||
const bpf = 0.5
|
||||
const frames = Math.max(1, fps * trimmedDuration)
|
||||
const budget = (targetMB * 1_048_576) / (frames * bpf)
|
||||
const requestedFps = fps
|
||||
const aspect = clip.height === 0 ? 16 / 9 : clip.width / clip.height
|
||||
const w = Math.sqrt(budget * aspect)
|
||||
const w2 = Math.min(clip.width, Math.max(120, Math.round(w))) & ~1
|
||||
const h2 = Math.round(w2 / aspect) & ~1
|
||||
return `${w2}x${h2}`
|
||||
|
||||
if (gifBudgetMode === "resolution") {
|
||||
const maxW = Math.min(clip.width, 1280)
|
||||
const w = Math.max(120, maxW)
|
||||
const h = Math.round(w / aspect)
|
||||
const pixelsPerFrame = w * h
|
||||
const bytesPerFrame = pixelsPerFrame * bpf
|
||||
const maxFrames = Math.max(1, Math.floor((targetMB * 1_048_576) / bytesPerFrame))
|
||||
const minFps = Math.max(1, Math.round(maxFrames / trimmedDuration))
|
||||
const effectiveFps = Math.max(1, Math.min(requestedFps, minFps))
|
||||
const w2 = w & ~1
|
||||
const h2 = Math.round(w2 / aspect) & ~1
|
||||
return `${w2}x${h2} @ ${effectiveFps}fps`
|
||||
} else if (gifBudgetMode === "framerate") {
|
||||
const frames = Math.max(1, requestedFps * trimmedDuration)
|
||||
const budget = (targetMB * 1_048_576) / (frames * bpf)
|
||||
const w = Math.sqrt(budget * aspect)
|
||||
const w2 = Math.min(clip.width, Math.max(120, Math.round(w))) & ~1
|
||||
const h2 = Math.round(w2 / aspect) & ~1
|
||||
return `${w2}x${h2}`
|
||||
} else {
|
||||
const framesAtFull = Math.max(1, requestedFps * trimmedDuration)
|
||||
const budgetAtFull = (targetMB * 1_048_576) / (framesAtFull * bpf)
|
||||
const wAtFull = Math.sqrt(budgetAtFull * aspect)
|
||||
const scale = wAtFull < clip.width ? Math.sqrt(wAtFull / clip.width) : 1
|
||||
const w2 = Math.max(60, Math.round(clip.width * scale)) & ~1
|
||||
const h2 = Math.round(w2 / aspect) & ~1
|
||||
const effectiveFps = Math.max(1, Math.min(requestedFps, Math.round(requestedFps * scale)))
|
||||
return `${w2}x${h2} @ ${effectiveFps}fps`
|
||||
}
|
||||
}
|
||||
return `${clip.width}x${clip.height}`
|
||||
})
|
||||
@@ -125,7 +154,7 @@
|
||||
try { crop = await api.getOverlayRect() } catch (e) { errMsg = String(e); return }
|
||||
}
|
||||
try {
|
||||
await api.startCapture({ crop })
|
||||
await api.startCapture({ crop, fps })
|
||||
phase = "recording"
|
||||
tick()
|
||||
void emit("recording-state", { phase: "recording", fps })
|
||||
@@ -176,7 +205,7 @@
|
||||
if (!clip) return
|
||||
previewLoading = true
|
||||
try {
|
||||
previewPath = await api.renderPreview(format, fps, Math.round(targetMB * 1_048_576))
|
||||
previewPath = await api.renderPreview(format, fps, Math.round(targetMB * 1_048_576), format === "gif" ? gifBudgetMode : undefined)
|
||||
previewCacheBust = Date.now()
|
||||
} catch { previewPath = null }
|
||||
finally { previewLoading = false }
|
||||
@@ -219,6 +248,7 @@
|
||||
trim_start_secs: tsStart, trim_end_secs: tsEnd,
|
||||
output_path: outputPath.trim() || null,
|
||||
crop,
|
||||
gif_budget_mode: format === "gif" ? gifBudgetMode : undefined,
|
||||
})
|
||||
exportResult = result; phase = "done"
|
||||
} catch (e) { errMsg = String(e); phase = "editing" }
|
||||
@@ -403,10 +433,27 @@
|
||||
{#if useFrame}
|
||||
<div class="preset-row">
|
||||
{#each PRESETS as [w, h, label]}
|
||||
<button class="chip" onclick={() => api.setOverlaySize(w, h)}>{label}</button>
|
||||
<button class="chip" onclick={() => {
|
||||
api.setOverlaySize(w, h)
|
||||
aspectLock = w / h
|
||||
emit("overlay-aspect-lock", { ratio: aspectLock })
|
||||
}}>{label}</button>
|
||||
{/each}
|
||||
{#if aspectLock !== null}
|
||||
<button class="chip unlock" onclick={() => {
|
||||
aspectLock = null
|
||||
emit("overlay-aspect-lock", { ratio: null })
|
||||
}}>Unlock</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="idle-fps">
|
||||
<span class="switch-label">Capture FPS</span>
|
||||
<select bind:value={fps}>
|
||||
{#each [1, 2, 5, 10, 15, 24, 30, 60] as f}<option value={f}>{f}</option>{/each}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -587,12 +634,32 @@
|
||||
<p class="estimate">{previewDims} · {fps}fps · {trimmedDuration.toFixed(1)}s</p>
|
||||
</div>
|
||||
|
||||
{#if format === "gif"}
|
||||
<div class="control-group">
|
||||
<span class="section-title">Budget priority</span>
|
||||
<div class="fmt-bar">
|
||||
{#each (["resolution", "framerate", "balanced"] as const) as m}
|
||||
<button
|
||||
class="fmt-chip"
|
||||
class:active={gifBudgetMode === m}
|
||||
onclick={() => { gifBudgetMode = m }}
|
||||
>{m === "resolution" ? "Resolution" : m === "framerate" ? "Framerate" : "Balanced"}</button>
|
||||
{/each}
|
||||
</div>
|
||||
<p class="estimate">
|
||||
{gifBudgetMode === "resolution" ? "Keeps resolution high, may reduce FPS" :
|
||||
gifBudgetMode === "framerate" ? "Keeps FPS high, may reduce resolution" :
|
||||
"Balances resolution and FPS"}
|
||||
</p>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<!-- FPS + Audio -->
|
||||
<div class="control-row">
|
||||
<label class="control">
|
||||
<span class="control-label">FPS</span>
|
||||
<select bind:value={fps} onchange={refreshPreview}>
|
||||
{#each [15,24,30,60] as f}<option value={f}>{f}</option>{/each}
|
||||
{#each [1, 2, 5, 10, 15, 24, 30, 60] as f}<option value={f}>{f}</option>{/each}
|
||||
</select>
|
||||
</label>
|
||||
{#if format !== "gif"}
|
||||
@@ -866,6 +933,16 @@
|
||||
}
|
||||
.switch.on .switch-knob { transform: translateX(16px); background: var(--accent-text); }
|
||||
|
||||
.idle-fps {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 10px 14px;
|
||||
background: var(--bg-2);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--r-sm);
|
||||
}
|
||||
|
||||
.preset-row {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
@@ -884,6 +961,14 @@
|
||||
transition: border-color 80ms, color 80ms;
|
||||
}
|
||||
.chip:hover { border-color: var(--accent); color: var(--accent); }
|
||||
.chip.unlock {
|
||||
border-color: var(--warn);
|
||||
color: var(--warn);
|
||||
}
|
||||
.chip.unlock:hover {
|
||||
background: var(--warn);
|
||||
color: var(--bg);
|
||||
}
|
||||
|
||||
/* ---- Recording ---- */
|
||||
.recording {
|
||||
|
||||
+28
-2
@@ -13,6 +13,8 @@
|
||||
let fps: number = $state(30)
|
||||
let elapsed: number = $state(0)
|
||||
let timer: number | null = null
|
||||
let lockedRatio: number | null = null
|
||||
let resizeDebounce: number | null = null
|
||||
|
||||
onMount(() => {
|
||||
const updateSize = async () => {
|
||||
@@ -24,7 +26,27 @@
|
||||
updateSize()
|
||||
let cleanup: (() => void)[] = []
|
||||
|
||||
win.onResized(updateSize).then(fn => cleanup.push(fn))
|
||||
win.onResized(async () => {
|
||||
await updateSize()
|
||||
if (lockedRatio === null) return
|
||||
if (resizeDebounce !== null) clearTimeout(resizeDebounce)
|
||||
resizeDebounce = window.setTimeout(async () => {
|
||||
const ratio = lockedRatio!
|
||||
const targetH = Math.round(width / ratio)
|
||||
const minH = 90
|
||||
const clampedH = Math.max(minH, targetH)
|
||||
if (Math.abs(clampedH - height) > 1) {
|
||||
try {
|
||||
await win.setSize({ type: "Logical", width, height: clampedH })
|
||||
await updateSize()
|
||||
} catch {}
|
||||
}
|
||||
}, 150)
|
||||
}).then(fn => cleanup.push(fn))
|
||||
|
||||
listen<{ ratio: number | null }>("overlay-aspect-lock", (e) => {
|
||||
lockedRatio = e.payload.ratio
|
||||
}).then(fn => cleanup.push(fn))
|
||||
|
||||
listen<{ phase: "idle"|"recording"|"paused"; fps: number }>(
|
||||
"recording-state", (e) => {
|
||||
@@ -49,7 +71,11 @@
|
||||
try { await invoke("close_window_picker") } catch {}
|
||||
}).then(fn => cleanup.push(fn))
|
||||
|
||||
return () => { stopTimer(); cleanup.forEach(fn => fn()) }
|
||||
return () => {
|
||||
stopTimer()
|
||||
if (resizeDebounce !== null) clearTimeout(resizeDebounce)
|
||||
cleanup.forEach(fn => fn())
|
||||
}
|
||||
})
|
||||
|
||||
function startTimer() {
|
||||
|
||||
+5
-2
@@ -12,6 +12,7 @@ export type AppStatus = {
|
||||
|
||||
export type Format = "gif" | "webm" | "mp4"
|
||||
export type GifDither = "floyd_steinberg" | "bayer" | "none"
|
||||
export type GifBudgetMode = "resolution" | "framerate" | "balanced"
|
||||
export type WebmCodec = "vp9" | "av1"
|
||||
export type Mp4Codec = "h264" | "h265"
|
||||
|
||||
@@ -101,6 +102,7 @@ export type RecordedClip = {
|
||||
|
||||
export type StartCaptureRequest = {
|
||||
crop: CaptureRect | null
|
||||
fps: number
|
||||
}
|
||||
|
||||
export type ExportRequest = {
|
||||
@@ -113,6 +115,7 @@ export type ExportRequest = {
|
||||
trim_end_secs?: number | null
|
||||
output_path?: string | null
|
||||
crop?: CaptureRect | null
|
||||
gif_budget_mode?: GifBudgetMode
|
||||
}
|
||||
|
||||
export type ExportResult = {
|
||||
@@ -162,8 +165,8 @@ export const api = {
|
||||
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
|
||||
extractThumbnails: (fps: number) =>
|
||||
invoke<string[]>("extract_thumbnails", { fps }),
|
||||
renderPreview: (format: Format, fps: number, target_bytes: number) =>
|
||||
invoke<string>("render_preview", { request: { format, fps, target_bytes } }),
|
||||
renderPreview: (format: Format, fps: number, target_bytes: number, gif_budget_mode?: GifBudgetMode) =>
|
||||
invoke<string>("render_preview", { request: { format, fps, target_bytes, gif_budget_mode } }),
|
||||
defaultOutputPath: (format: Format) =>
|
||||
invoke<string>("default_output_path", { format }),
|
||||
openUrl: (url: string) => invoke<void>("open_url", { url }),
|
||||
|
||||
Reference in New Issue
Block a user