Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(export): report progress while the voice track is written

davbio89539d8c8506
diff --git a/src/core/export/voiceover/__tests__/audio.test.ts b/src/core/export/voiceover/__tests__/audio.test.ts
index 6235434..d17def5 100644
--- a/src/core/export/voiceover/__tests__/audio.test.ts
+++ b/src/core/export/voiceover/__tests__/audio.test.ts
@@ -1,5 +1,5 @@
-import { describe, expect, it } from 'vitest';
-import { type PlacedClip, voiceTrackPieces } from '@/core/export/voiceover/audio';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { type PlacedClip, voiceTrackPieces, writeVoiceTrack } from '@/core/export/voiceover/audio';
const RATE = 100;
@@ -42,3 +42,53 @@ describe('voiceTrackPieces', () => {
]);
});
});
+
+describe('writeVoiceTrack', () => {
+ beforeEach(() => {
+ vi.stubGlobal(
+ 'OfflineAudioContext',
+ class {
+ createBuffer(_channels: number, length: number) {
+ return { length };
+ }
+ },
+ );
+ });
+
+ it('reports a step per piece, so the bar can move while the track is written', async () => {
+ const seen: [number, number][] = [];
+ await writeVoiceTrack(
+ [clip(1, 50)],
+ 3,
+ async () => {},
+ RATE,
+ (done, total) => seen.push([done, total]),
+ );
+
+ expect(seen).toEqual([
+ [1, 3],
+ [2, 3],
+ [3, 3],
+ ]);
+ });
+
+ it('ends on a full count, so the bar lands on 100%', async () => {
+ const seen: [number, number][] = [];
+ await writeVoiceTrack(
+ [],
+ 2,
+ async () => {},
+ RATE,
+ (done, total) => seen.push([done, total]),
+ );
+
+ expect(seen.at(-1)?.[0]).toBe(seen.at(-1)?.[1]);
+ });
+
+ it('writes the track without a progress callback at all', async () => {
+ const written: number[] = [];
+ await writeVoiceTrack([clip(1, 50)], 3, async (b) => void written.push(b.length), RATE);
+
+ expect(written).toEqual([100, 50, 150]);
+ });
+});
diff --git a/src/core/export/voiceover/audio.ts b/src/core/export/voiceover/audio.ts
index aac6e08..20bf39c 100644
--- a/src/core/export/voiceover/audio.ts
+++ b/src/core/export/voiceover/audio.ts
@@ -68,9 +68,13 @@ export async function writeVoiceTrack(
totalSec: number,
add: (buffer: AudioBuffer) => Promise<void>,
sampleRate = VOICE_SAMPLE_RATE,
+ onProgress?: (done: number, total: number) => void,
): Promise<void> {
- for (const piece of voiceTrackPieces(clips, totalSec, sampleRate)) {
+ const pieces = voiceTrackPieces(clips, totalSec, sampleRate);
+ let written = 0;
+ for (const piece of pieces) {
await add(piece.kind === 'clip' ? piece.buffer : silence(piece.samples, sampleRate));
+ onProgress?.(++written, pieces.length);
}
}
@@ -80,11 +84,6 @@ export async function pickVoiceCodec(container: VideoContainer): Promise<VoiceCo
return (await canEncodeAudio(codec, { numberOfChannels: 1, sampleRate: VOICE_SAMPLE_RATE })) ? codec : null;
}
-/**
- * Picks the first container this browser can encode *audio* into. Video support alone is not
- * enough: Chromium on Linux encodes H.264 but not AAC, so preferring mp4 there lands the export in
- * a container it cannot write the voice track into, while WebM/Opus would have worked.
- */
export async function pickVoiceContainer(
containers: VideoContainer[],
): Promise<{ container: VideoContainer; codec: VoiceCodec } | null> {