-
-
Notifications
You must be signed in to change notification settings - Fork 4
Add ffmpeg wasm #1822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add ffmpeg wasm #1822
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
adfaf20
Add ffmpeg - only works via vite port/server
myieye bb2d6d9
Add ffmpeg wasm
myieye 374b634
Merge branch 'develop' into ffmpeg-wasm
hahn-kev bd54b0a
setup audio pipeline for the audio editor to process and modify audio
hahn-kev e7c1275
abort file operations when the audio editor is destroyed
hahn-kev 265dcfb
minor cleanup
hahn-kev f02152d
cleanup and teardown ffmpeg with the audio editor dialog
hahn-kev d6195a2
use resource abort signal
hahn-kev 001b41c
resolve type issues
hahn-kev c5f280f
remove unused imports
hahn-kev 4b45a17
Update frontend/viewer/src/lib/components/audio/audio-editor.svelte
hahn-kev cb524dc
show audio input to all
hahn-kev 4a431e8
use both abort signals for our ffmpeg resource pipeline
hahn-kev 179b13a
add a download button to avoid losing audio when it's too big
hahn-kev 47e4dd9
remove unused inport
hahn-kev 5c4c2ce
update i18n
hahn-kev 8523105
Merge branch 'develop' into ffmpeg-wasm
hahn-kev 43b1219
update i18n again
hahn-kev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 89 additions & 24 deletions
113
frontend/viewer/src/lib/components/audio/audio-editor.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,119 @@ | ||
<script lang="ts"> | ||
import { t } from 'svelte-i18n-lingui'; | ||
import {t} from 'svelte-i18n-lingui'; | ||
import Button from '../ui/button/button.svelte'; | ||
import Waveform from './wavesurfer/waveform.svelte'; | ||
import type WaveSurfer from 'wavesurfer.js'; | ||
import {formatDigitalDuration} from '../ui/format/format-duration'; | ||
import DevContent from '$lib/layout/DevContent.svelte'; | ||
import {Label} from '../ui/label'; | ||
import {FFmpegApi} from './ffmpeg'; | ||
import Loading from '$lib/components/Loading.svelte'; | ||
import {resource, watch} from 'runed'; | ||
import {onDestroy} from 'svelte'; | ||
|
||
type Props = { | ||
audio: Blob; | ||
name: string | ||
audio: File; | ||
finalAudio: File | undefined; | ||
onDiscard: () => void; | ||
}; | ||
|
||
let { audio, name, onDiscard }: Props = $props(); | ||
let { | ||
audio, | ||
finalAudio = $bindable(undefined), | ||
onDiscard | ||
}: Props = $props(); | ||
|
||
let audioApi = $state<WaveSurfer>(); | ||
let playing = $state(false); | ||
let duration = $state<number | null>(null); | ||
const mb = $derived((audio.size / 1024 / 1024).toFixed(2)); | ||
const formatedDuration = $derived(duration ? formatDigitalDuration({ seconds: duration }) : 'unknown'); | ||
const mb = $derived(!finalAudio ? '0' : (finalAudio.size / 1024 / 1024).toFixed(2)); | ||
const formattedDuration = $derived(duration ? formatDigitalDuration({seconds: duration}) : 'unknown'); | ||
let ffmpegApi: FFmpegApi | undefined; | ||
|
||
let ffmpegFile = resource(() => audio, async (audio, _, {signal}) => { | ||
ffmpegApi ??= await FFmpegApi.create(); | ||
return await ffmpegApi.toFFmpegFile(audio, AbortSignal.any([signal, abortController.signal])); | ||
}); | ||
|
||
let flacFile = resource(() => [ffmpegFile.current], async ([file], _, {signal}) => { | ||
if (!file) return; | ||
ffmpegApi ??= await FFmpegApi.create(); | ||
return await ffmpegApi.convertToFlac(file, AbortSignal.any([signal, abortController.signal])); | ||
}); | ||
|
||
let readFile = resource(() => [flacFile.current], async ([file], _, {signal}) => { | ||
if (!file) return; | ||
ffmpegApi ??= await FFmpegApi.create(); | ||
return await ffmpegApi.readFile(file, AbortSignal.any([signal, abortController.signal])); | ||
}); | ||
|
||
watch(() => [readFile.current, readFile.loading] as const, ([file, loading]) => { | ||
if (loading || !file) { | ||
finalAudio = undefined; | ||
} else { | ||
finalAudio = file; | ||
} | ||
}); | ||
|
||
const loading = $derived(ffmpegFile.loading || flacFile.loading || readFile.loading); | ||
const error = $derived((ffmpegFile.error || flacFile.error || readFile.error)?.toString()); | ||
|
||
const abortController = new AbortController(); | ||
onDestroy(() => { | ||
abortController.abort(); | ||
ffmpegApi?.terminate(); | ||
}); | ||
|
||
function downloadAudio() { | ||
if (!finalAudio) throw new Error('No audio to download'); | ||
const url = URL.createObjectURL(finalAudio); | ||
try { | ||
const a = document.createElement('a'); | ||
a.href = url; | ||
a.download = `${finalAudio.name}`; | ||
a.click(); | ||
} finally { | ||
URL.revokeObjectURL(url); | ||
} | ||
} | ||
</script> | ||
|
||
<div class="flex flex-col gap-4 items-center justify-center"> | ||
{#if loading || !finalAudio} | ||
<Loading class="self-center justify-self-center size-16"/> | ||
{:else} | ||
<span class="inline-grid grid-cols-[auto_auto_1rem_auto_auto] gap-2 items-baseline"> | ||
<Label class="justify-self-end">{$t`Length:`}</Label> <span>{$t`${formatedDuration}`}</span> | ||
<Label class="justify-self-end">{$t`Length:`}</Label> <span>{$t`${formattedDuration}`}</span> | ||
<span></span> | ||
<Label class="justify-self-end">{$t`Size:`}</Label> <span>{$t`${mb} MB`}</span> | ||
{#if name} | ||
{#if finalAudio.name} | ||
<Label class="justify-self-end">{$t`File name:`}</Label> | ||
<span class="col-span-4">{$t`${name}`}</span> | ||
<span class="col-span-4">{$t`${finalAudio.name}`}</span> | ||
{/if} | ||
<DevContent> | ||
<Label class="justify-self-end">{$t`Type:`}</Label> | ||
<span class="col-span-4">{$t`${audio.type}`}</span> | ||
<span class="col-span-4">{$t`${finalAudio.type}`}</span> | ||
</DevContent> | ||
</span> | ||
<!-- contain-inline-size prevents wavesurfer from freaking out inside a grid --> | ||
<!-- pb-8 ensures the timeline is in the bounds of the container --> | ||
<div class="w-full grow max-h-32 pb-3 contain-inline-size border-y"> | ||
<Waveform {audio} bind:playing bind:audioApi bind:duration showTimeline autoplay class="size-full" /> | ||
</div> | ||
<div class="flex gap-2"> | ||
<Button variant="secondary" icon="i-mdi-close" onclick={onDiscard} disabled={!audio}>{$t`Discard`}</Button> | ||
<Button | ||
icon={playing ? 'i-mdi-pause' : 'i-mdi-play'} | ||
onclick={() => (playing ? audioApi?.pause() : audioApi?.play())} | ||
disabled={!audioApi} | ||
size="icon" | ||
/> | ||
</div> | ||
<!-- contain-size prevents wavesurfer from freaking out inside a grid | ||
contain-inline-size would improve the height reactivity of the waveform, but | ||
results in the waveform sometimes change its height unexpectedly --> | ||
<!-- pb-8 ensures the timeline is in the bounds of the container --> | ||
<div class="w-full grow max-h-32 pb-3 contain-size border-y"> | ||
<Waveform audio={finalAudio} bind:playing bind:audioApi bind:duration showTimeline autoplay class="size-full"/> | ||
</div> | ||
<div class="flex gap-2"> | ||
<Button onclick={() => downloadAudio()} disabled={!finalAudio} variant="secondary" icon="i-mdi-download">{$t`Download`}</Button> | ||
<Button variant="secondary" icon="i-mdi-close" onclick={onDiscard} disabled={!audio}>{$t`Discard`}</Button> | ||
<Button | ||
icon={playing ? 'i-mdi-pause' : 'i-mdi-play'} | ||
onclick={() => (playing ? audioApi?.pause() : audioApi?.play())} | ||
disabled={!audioApi} | ||
size="icon" | ||
/> | ||
</div> | ||
{/if} | ||
{#if error} | ||
<p class="text-destructive">{error}</p> | ||
{/if} | ||
</div> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.