Skip to content

🎥 Make videos programmatically with React

Revert "get rid of getaudiochannels"

Jonny Burger54a140af0024
diff --git a/packages/renderer/src/assets/get-audio-channels.ts b/packages/renderer/src/assets/get-audio-channels.ts
new file mode 100644
index 0000000000..3d1e2e8d02
--- /dev/null
+++ b/packages/renderer/src/assets/get-audio-channels.ts
@@ -0,0 +1,24 @@
+import execa from 'execa';
+
+export async function getAudioChannels(path: string) {
+ const args = [
+ ['-v', 'error'],
+ ['-show_entries', 'stream=channels'],
+ ['-of', 'default=nw=1'],
+ [path],
+ ]
+ .reduce<(string | null)[]>((acc, val) => acc.concat(val), [])
+ .filter(Boolean) as string[];
+
+ const task = await execa('ffprobe', args);
+ if (!task.stdout.includes('channels=')) {
+ return 0;
+ }
+
+ const channels = parseInt(task.stdout.replace('channels=', ''), 10);
+ if (isNaN(channels)) {
+ throw new TypeError('Unexpected result from ffprobe for channel probing: ');
+ }
+
+ return channels;
+}
diff --git a/packages/renderer/src/calculate-ffmpeg-filters.ts b/packages/renderer/src/calculate-ffmpeg-filters.ts
index 81d0e605f4..c972f8d76f 100644
--- a/packages/renderer/src/calculate-ffmpeg-filters.ts
+++ b/packages/renderer/src/calculate-ffmpeg-filters.ts
@@ -6,16 +6,23 @@ export const calculateFfmpegFilter = ({
asset,
fps,
durationInFrames,
+ channels,
}: {
asset: MediaAsset;
fps: number;
durationInFrames: number;
+ channels: number;
}): string | null => {
+ if (channels === 0) {
+ return null;
+ }
+
const assetTrimLeft = asset.trimLeft / fps;
const assetTrimRight =
(asset.trimLeft + asset.duration * asset.playbackRate) / fps;
const filter = stringifyFfmpegFilter({
+ channels,
startInVideo: asset.startInVideo,
trimLeft: assetTrimLeft,
trimRight: assetTrimRight,
diff --git a/packages/renderer/src/preprocess-audio-track.ts b/packages/renderer/src/preprocess-audio-track.ts
index f31b6793f3..fba88c0b50 100644
--- a/packages/renderer/src/preprocess-audio-track.ts
+++ b/packages/renderer/src/preprocess-audio-track.ts
@@ -1,5 +1,6 @@
import execa from 'execa';
import {FfmpegExecutable} from 'remotion';
+import {getAudioChannels} from './assets/get-audio-channels';
import {MediaAsset} from './assets/types';
import {calculateFfmpegFilter} from './calculate-ffmpeg-filters';
import {makeFfmpegFilterFile} from './ffmpeg-filter-file';
@@ -21,10 +22,13 @@ const preprocessAudioTrackUnlimited = async ({
expectedFrames,
fps,
}: Options): Promise<string | null> => {
+ const channels = await getAudioChannels(resolveAssetSrc(asset.src));
+
const filter = calculateFfmpegFilter({
asset,
durationInFrames: expectedFrames,
fps,
+ channels,
});
if (filter === null) {
diff --git a/packages/renderer/src/stringify-ffmpeg-filter.ts b/packages/renderer/src/stringify-ffmpeg-filter.ts
index 284753c8c7..07b3e60e54 100644
--- a/packages/renderer/src/stringify-ffmpeg-filter.ts
+++ b/packages/renderer/src/stringify-ffmpeg-filter.ts
@@ -6,6 +6,7 @@ import {AssetVolume} from './assets/types';
export const stringifyFfmpegFilter = ({
trimLeft,
trimRight,
+ channels,
startInVideo,
volume,
fps,
@@ -14,6 +15,7 @@ export const stringifyFfmpegFilter = ({
}: {
trimLeft: number;
trimRight: number;
+ channels: number;
startInVideo: number;
volume: AssetVolume;
fps: number;
@@ -42,13 +44,15 @@ export const stringifyFfmpegFilter = ({
`atrim=${trimLeft.toFixed(6)}:${trimRight.toFixed(6)}`,
// then set the tempo
calculateATempo(playbackRate),
- // We delay up to 10 channels.
- // Audio usually does not have more than than that and FFMEPG docs state:
+ // For n channels, we delay n + 1 channels.
+ // This is because `ffprobe` for some audio files reports the wrong amount
+ // of channels.
+ // This should be fine because FFMPEG documentation states:
// "Unused delays will be silently ignored."
// https://ffmpeg.org/ffmpeg-filters.html#adelay
startInVideoSeconds === 0
? null
- : `adelay=${new Array(10)
+ : `adelay=${new Array(channels + 1)
.fill((startInVideoSeconds * 1000).toFixed(0))
.join('|')}`,
// set the volume if needed
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..d4d640e5bc
--- /dev/null
+++ b/packages/renderer/src/test/get-audio-channels.test.ts
@@ -0,0 +1,60 @@
+import {existsSync} from 'fs';
+import path from 'path';
+import {getAudioChannels} from '../assets/get-audio-channels';
+jest.setTimeout(10000);
+
+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', () => {
+ const tsFile = path.join(__dirname, '..', 'ffmpeg-flags.ts');
+ expect(existsSync(tsFile)).toBe(true);
+ expect(() => getAudioChannels(tsFile)).rejects.toThrow(
+ /Invalid data found when processing input/
+ );
+});