294 lines
8.4 KiB
TypeScript
294 lines
8.4 KiB
TypeScript
import { invoke, convertFileSrc } from "@tauri-apps/api/core"
|
|
import { listen, type UnlistenFn } from "@tauri-apps/api/event"
|
|
|
|
export { convertFileSrc }
|
|
|
|
export type FfmpegStatus = "ready" | "missing"
|
|
|
|
export type AppStatus = {
|
|
ffmpeg: FfmpegStatus
|
|
needs_first_run: boolean
|
|
}
|
|
|
|
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"
|
|
|
|
export type ExportConfig = {
|
|
default_format: Format
|
|
gif_dither: GifDither
|
|
webm_codec: WebmCodec
|
|
mp4_codec: Mp4Codec
|
|
}
|
|
|
|
export type CaptureQuality = "auto" | "full"
|
|
|
|
export type CaptureConfig = {
|
|
default_fps: number
|
|
capture_cursor: boolean
|
|
capture_audio: boolean
|
|
capture_quality: CaptureQuality
|
|
limit_capture_fps: boolean
|
|
}
|
|
|
|
export type HotkeyConfig = {
|
|
start: string | null
|
|
stop: string | null
|
|
pause: string | null
|
|
}
|
|
|
|
export type UpdatesConfig = {
|
|
check_on_launch: boolean
|
|
}
|
|
|
|
export type Theme = "dark" | "light"
|
|
|
|
export type Config = {
|
|
export: ExportConfig
|
|
capture: CaptureConfig
|
|
hotkeys: HotkeyConfig
|
|
updates: UpdatesConfig
|
|
output_dir: string | null
|
|
theme: Theme
|
|
}
|
|
|
|
export type DownloadProgress = {
|
|
downloaded: number
|
|
total: number | null
|
|
}
|
|
|
|
export type UpdateInfo = {
|
|
version: string
|
|
current_version: string
|
|
release_url: string
|
|
body: string
|
|
download_url: string | null
|
|
}
|
|
|
|
export type UpdateDownloadProgress = {
|
|
downloaded: number
|
|
total: number | null
|
|
}
|
|
|
|
export type CaptureRect = {
|
|
x: number
|
|
y: number
|
|
width: number
|
|
height: number
|
|
}
|
|
|
|
export type WindowInfo = {
|
|
title: string
|
|
x: number
|
|
y: number
|
|
width: number
|
|
height: number
|
|
}
|
|
|
|
export type ClickEvent = {
|
|
time_secs: number
|
|
x: number
|
|
y: number
|
|
button: "Left" | "Right" | "Middle"
|
|
}
|
|
|
|
export type RecordedClip = {
|
|
temp_path: string
|
|
duration_secs: number
|
|
width: number
|
|
height: number
|
|
monitor_width: number
|
|
monitor_height: number
|
|
crop: CaptureRect | null
|
|
clicks: ClickEvent[]
|
|
}
|
|
|
|
export type StartCaptureRequest = {
|
|
crop: CaptureRect | null
|
|
options?: CaptureConfig
|
|
}
|
|
|
|
export type ExportRequest = {
|
|
format: Format
|
|
target_bytes: number
|
|
fps: number
|
|
keep_audio: boolean
|
|
gif_removed_ranges?: SourceRange[]
|
|
trim_start_secs?: number | null
|
|
trim_end_secs?: number | null
|
|
output_path?: string | null
|
|
overwrite?: boolean
|
|
crop?: CaptureRect | null
|
|
gif_budget_mode?: GifBudgetMode
|
|
gif_dither?: GifDither
|
|
gif_colors?: number
|
|
}
|
|
|
|
export type SourceRange = { start_secs: number; end_secs: number }
|
|
|
|
export type ExportResult = {
|
|
output_path: string
|
|
size_bytes: number
|
|
target_bytes: number
|
|
met_target: boolean
|
|
}
|
|
|
|
export type OutputPreview = {
|
|
id: string
|
|
path: string
|
|
format: Format
|
|
size_bytes: number
|
|
target_bytes: number
|
|
width: number
|
|
height: number
|
|
fps: number
|
|
duration_secs: number
|
|
}
|
|
|
|
export type HotkeyRegistrationResult = {
|
|
start_ok: boolean | null
|
|
stop_ok: boolean | null
|
|
pause_ok: boolean | null
|
|
}
|
|
|
|
export type SaveConfigResult = {
|
|
hotkey_result: HotkeyRegistrationResult
|
|
}
|
|
|
|
export type EncodeProgress = {
|
|
pass: number
|
|
total_passes: number
|
|
percent: number
|
|
}
|
|
|
|
export type GifEstimate = {
|
|
width: number
|
|
fps: number
|
|
height: number
|
|
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 DiagnosticsInfo = {
|
|
app_version: string
|
|
ffmpeg_version: string
|
|
log_dir: string
|
|
log_tail: string
|
|
}
|
|
|
|
export type AudioFailed = string
|
|
|
|
export const api = {
|
|
getStatus: () => invoke<AppStatus>("get_status"),
|
|
getConfig: () => invoke<Config>("get_config"),
|
|
saveConfig: (config: Config) => invoke<SaveConfigResult>("save_config", { config }),
|
|
downloadFfmpeg: () => invoke<void>("download_ffmpeg"),
|
|
onFfmpegProgress: (handler: (p: DownloadProgress) => void): Promise<UnlistenFn> =>
|
|
listen<DownloadProgress>("ffmpeg-progress", (e) => handler(e.payload)),
|
|
startCapture: (request: StartCaptureRequest) =>
|
|
invoke<void>("start_capture", { request }),
|
|
pauseCapture: () => invoke<void>("pause_capture"),
|
|
resumeCapture: () => invoke<void>("resume_capture"),
|
|
restartAudioCapture: () => invoke<void>("restart_audio_capture"),
|
|
stopCapture: () => invoke<RecordedClip>("stop_capture"),
|
|
discardClip: () => invoke<void>("discard_clip"),
|
|
export: (request: ExportRequest) => invoke<ExportResult>("export", { request }),
|
|
exportDestination: (format: Format, path: string) =>
|
|
invoke<{ path: string; exists: boolean }>("export_destination", { format, path }),
|
|
revealInExplorer: (path: string) => invoke<void>("reveal_in_explorer", { path }),
|
|
showOverlay: () => invoke<void>("show_overlay"),
|
|
openWindowPicker: () => invoke<void>("open_window_picker"),
|
|
hideOverlay: () => invoke<void>("hide_overlay"),
|
|
setOverlaySize: (width: number, height: number) =>
|
|
invoke<void>("set_overlay_size", { width, height }),
|
|
setOverlayAspect: (ratio: number | null) =>
|
|
invoke<void>("set_overlay_aspect", { ratio }),
|
|
setOverlayToWindow: (x: number, y: number, width: number, height: number) =>
|
|
invoke<void>("set_overlay_to_window", { x, y, width, height }),
|
|
listWindows: () => invoke<WindowInfo[]>("list_windows"),
|
|
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
|
|
extractThumbnails: (fps: number, start: number) =>
|
|
invoke<{ paths: string[]; has_more: boolean }>("extract_thumbnails", { fps, start }),
|
|
timelineThumbnails: () => invoke<string[]>("timeline_thumbnails"),
|
|
previewDisplay: (x: number, y: number) => invoke<ArrayBuffer>("preview_display", { x, y }),
|
|
renderOutputPreview: (request: ExportRequest) =>
|
|
invoke<OutputPreview>("render_output_preview", { request }),
|
|
saveOutputPreview: (previewId: string, outputPath: string, overwrite: boolean) =>
|
|
invoke<ExportResult>("save_output_preview", { previewId, outputPath, overwrite }),
|
|
openOutputPreview: (previewId: string) => invoke<void>("open_output_preview", { previewId }),
|
|
estimateGifParams: (input: {
|
|
format: Format
|
|
fps: number
|
|
target_bytes: number
|
|
gif_budget_mode?: GifBudgetMode
|
|
gif_dither?: GifDither
|
|
gif_colors?: number
|
|
clip_width: number
|
|
clip_height: number
|
|
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 }),
|
|
defaultOutputPath: (format: Format) =>
|
|
invoke<string>("default_output_path", { format }),
|
|
openUrl: (url: string) => invoke<void>("open_url", { url }),
|
|
checkForUpdates: () => invoke<UpdateInfo | null>("check_for_updates"),
|
|
onUpdateAvailable: (handler: (info: UpdateInfo) => void): Promise<UnlistenFn> =>
|
|
listen<UpdateInfo>("update-available", (e) => handler(e.payload)),
|
|
onHotkey: (handler: (kind: "start" | "stop" | "pause") => void): Promise<UnlistenFn> =>
|
|
listen<"start" | "stop" | "pause">("hotkey", (e) => handler(e.payload)),
|
|
downloadUpdate: (url: string) => invoke<void>("download_update", { url }),
|
|
applyUpdate: () => invoke<void>("apply_update"),
|
|
pendingUpdate: () => invoke<boolean>("pending_update"),
|
|
onUpdateDownloadProgress: (
|
|
handler: (p: UpdateDownloadProgress) => void,
|
|
): Promise<UnlistenFn> =>
|
|
listen<UpdateDownloadProgress>("update-download-progress", (e) =>
|
|
handler(e.payload),
|
|
),
|
|
onEncodeProgress: (handler: (p: EncodeProgress) => void): Promise<UnlistenFn> =>
|
|
listen<EncodeProgress>("encode-progress", (e) => handler(e.payload)),
|
|
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"),
|
|
diagnosticsInfo: () => invoke<DiagnosticsInfo>("diagnostics_info"),
|
|
openLogDir: () => invoke<void>("open_log_dir"),
|
|
}
|
|
|
|
export function formatBytes(n: number): string {
|
|
if (n < 1024) return `${n} B`
|
|
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`
|
|
if (n < 1024 * 1024 * 1024) return `${(n / 1024 / 1024).toFixed(1)} MB`
|
|
return `${(n / 1024 / 1024 / 1024).toFixed(2)} GB`
|
|
}
|