Skip to content

🎥 Make videos programmatically with React

add tests for getting audio channels

Jonny Burgerc0e33ab38dd5
diff --git a/packages/renderer/src/assets/assets.ts b/packages/renderer/src/assets/assets.ts
index d9037f6569..bbbb6e5645 100644
--- a/packages/renderer/src/assets/assets.ts
+++ b/packages/renderer/src/assets/assets.ts
@@ -78,13 +78,14 @@ export async function getAudioChannels(path: string) {
.filter(Boolean) as string[];
const task = await execa('ffprobe', args);
- const parsed = parseInt(task.stdout.replace('channels=', ''), 10);
- // TODO: How to determine the amount of channels for video?
- // Is it even necessary?
- if (isNaN(parsed)) {
+ if (!task.stdout.includes('channels=')) {
return 0;
}
- return parsed;
+ const channels = parseInt(task.stdout.replace('channels=', ''), 10);
+ if (isNaN(channels)) {
+ throw new TypeError('Unexpected result from ffprobe for channel probing: ');
+ }
+ return channels;
}
export async function getAssetAudioDetails(options: {
diff --git a/packages/renderer/src/test/expect-to-throw.ts b/packages/renderer/src/test/expect-to-throw.ts
new file mode 100644
index 0000000000..23fa4d202e
--- /dev/null
+++ b/packages/renderer/src/test/expect-to-throw.ts
@@ -0,0 +1,12 @@
+export const expectToThrow = (func: Function, err: RegExp) => {
+ // Even though the error is caught, it still gets printed to the console
+ // so we mock that out to avoid the wall of red text.
+ jest.spyOn(console, 'error');
+ // @ts-expect-error
+ console.error.mockImplementation(() => void 0);
+
+ expect(func).toThrow(err);
+
+ // @ts-expect-error
+ console.error.mockRestore();
+};
diff --git a/packages/renderer/src/test/get-audio-channels.test.ts b/packages/renderer/src/test/get-audio-channels.test.ts
new file mode 100644
index 0000000000..1d285203ea
--- /dev/null
+++ b/packages/renderer/src/test/get-audio-channels.test.ts
@@ -0,0 +1,59 @@
+import {existsSync} from 'fs';
+import path from 'path';
+import {getAudioChannels} from '../assets/assets';
+
+test('Get audio channels for video', async () => {
+ const videoWithoutAudio = path.join(
+ __dirname,
+ '..',
+ '..',
+ '..',
+ 'example',
+ 'src',
+ 'resources',
+ 'framer-music.mp4'
+ );
+ expect(existsSync(videoWithoutAudio)).toBe(true);
+ const channels = await getAudioChannels(videoWithoutAudio);
+ expect(channels).toBe(2);
+});
+
+test('Get audio channels for video without music', async () => {
+ const videoWithAudio = path.join(
+ __dirname,
+ '..',
+ '..',
+ '..',
+ 'example',
+ 'src',
+ 'resources',
+ 'framer.mp4'
+ );
+ expect(existsSync(videoWithAudio)).toBe(true);
+ const channels = await getAudioChannels(videoWithAudio);
+ expect(channels).toBe(0);
+});
+
+test('Get audio channels for video without music', async () => {
+ const audio = path.join(
+ __dirname,
+ '..',
+ '..',
+ '..',
+ 'example',
+ 'src',
+ 'resources',
+ 'sound1.mp3'
+ );
+ expect(existsSync(audio)).toBe(true);
+ const channels = await getAudioChannels(audio);
+ expect(channels).toBe(2);
+});
+
+test('Throw error if parsing a non video file', async () => {
+ const tsFile = path.join(__dirname, '..', 'ffmpeg-flags.ts');
+ expect(existsSync(tsFile)).toBe(true);
+ expect(() => getAudioChannels(tsFile)).rejects.toThrow(
+ /Invalid data found when processing input/
+ );
+});