🎥 Make videos programmatically with React
get silent parts
Jonny Burger75ee7860c3fb
diff --git a/packages/docs/docs/renderer/get-silent-parts.md b/packages/docs/docs/renderer/get-silent-parts.mdindex 6b25f25d0c..a6b38e20af 100644--- a/packages/docs/docs/renderer/get-silent-parts.md+++ b/packages/docs/docs/renderer/get-silent-parts.md@@ -68,6 +68,14 @@ An array of objects with the following properties:- `startInSeconds`: The start time of the silent part in seconds.- `endInSeconds`: The end time of the silent part in seconds.+### `audibleParts`++The inverse of the `silentParts` array.+An array of objects with the following properties:++- `startInSeconds`: The start time of the audible part in seconds.+- `endInSeconds`: The end time of the audible part in seconds.+### `durationInSeconds`The time length of the media in seconds.diff --git a/packages/renderer/src/compositor/payloads.ts b/packages/renderer/src/compositor/payloads.tsindex 3a46d61d0d..07a5920072 100644--- a/packages/renderer/src/compositor/payloads.ts+++ b/packages/renderer/src/compositor/payloads.ts@@ -50,11 +50,15 @@ type SilentPart = {export type SilentParts = SilentPart[];-export type GetSilentPartsResponse = {+export type GetSilentPartsResponseRust = {silentParts: SilentParts;durationInSeconds: number;};+export type GetSilentPartsResponse = GetSilentPartsResponseRust & {+ audibleParts: SilentParts;+};+export type CompositorCommand = {Compose: {output: string;diff --git a/packages/renderer/src/get-silent-parts.ts b/packages/renderer/src/get-silent-parts.tsindex 4bd8e29026..0dd7c70dd4 100644--- a/packages/renderer/src/get-silent-parts.ts+++ b/packages/renderer/src/get-silent-parts.ts@@ -2,12 +2,16 @@ import {getIdealMaximumFrameCacheItems,startLongRunningCompositor,} from './compositor/compositor';-import type {GetSilentPartsResponse} from './compositor/payloads';+import type {+ GetSilentPartsResponse,+ GetSilentPartsResponseRust,+ SilentParts,+} from './compositor/payloads';import type {LogLevel} from './log-level';export const getSilentParts = async ({src,- noiseThresholdInDecibels: passednoiseThresholdInDecibels,+ noiseThresholdInDecibels: passedNoiseThresholdInDecibels,minDuration: passedMinDuration,logLevel,}: {@@ -34,7 +38,7 @@ export const getSilentParts = async ({);}- const noiseThresholdInDecibels = passednoiseThresholdInDecibels ?? -20;+ const noiseThresholdInDecibels = passedNoiseThresholdInDecibels ?? -20;if (typeof noiseThresholdInDecibels !== 'number') {throw new Error(@@ -54,9 +58,48 @@ export const getSilentParts = async ({noiseThresholdInDecibels,});- const response = JSON.parse(res.toString('utf-8')) as GetSilentPartsResponse;+ const response = JSON.parse(+ res.toString('utf-8')+ ) as GetSilentPartsResponseRust;compositor.finishCommands();await compositor.waitForDone();- return response;++ const {silentParts, durationInSeconds} = response;++ return {+ silentParts,+ audibleParts: getAudibleParts({silentParts, durationInSeconds}),+ durationInSeconds,+ };+};++const getAudibleParts = ({+ silentParts,+ durationInSeconds,+}: {+ silentParts: SilentParts;+ durationInSeconds: number;+}) => {+ const audibleParts: SilentParts = [];+ let lastEnd = 0;+ for (const silentPart of silentParts) {+ if (silentPart.startInSeconds - lastEnd > 0) {+ audibleParts.push({+ startInSeconds: lastEnd,+ endInSeconds: silentPart.startInSeconds,+ });+ }++ lastEnd = silentPart.endInSeconds;+ }++ if (durationInSeconds - lastEnd > 0) {+ audibleParts.push({+ startInSeconds: lastEnd,+ endInSeconds: durationInSeconds,+ });+ }++ return audibleParts;};diff --git a/packages/renderer/src/test/get-silent-parts.test.ts b/packages/renderer/src/test/get-silent-parts.test.tsindex 25238873c3..786b40bf43 100644--- a/packages/renderer/src/test/get-silent-parts.test.ts+++ b/packages/renderer/src/test/get-silent-parts.test.ts@@ -4,17 +4,21 @@ import {getSilentParts} from '../get-silent-parts';import {exampleVideos} from './example-videos';test('Should be able to get the silences from a video', async () => {- const {silentParts} = await getSilentParts({+ const {silentParts, audibleParts} = await getSilentParts({src: exampleVideos.webcam,logLevel: 'info',});expect(silentParts.length).toEqual(1);expect(silentParts[0].startInSeconds).toBe(0);expect(silentParts[0].endInSeconds).toBe(1.0149);++ expect(audibleParts.length).toEqual(1);+ expect(audibleParts[0].startInSeconds).toEqual(1.0149);+ expect(audibleParts[0].endInSeconds).toEqual(2.789);});test('Sensitive settings', async () => {- const {silentParts, durationInSeconds} = await getSilentParts({+ const {silentParts, audibleParts, durationInSeconds} = await getSilentParts({src: exampleVideos.webcam,noiseThresholdInDecibels: 0,minDuration: 0.1,@@ -24,14 +28,19 @@ test('Sensitive settings', async () => {expect(silentParts[0].startInSeconds).toBe(0);expect(silentParts[0].endInSeconds).toBe(2.789);expect(durationInSeconds).toBe(durationInSeconds);+ expect(audibleParts.length).toEqual(0);});test('Long duration', async () => {- const {silentParts} = await getSilentParts({+ const {silentParts, audibleParts} = await getSilentParts({src: exampleVideos.webcam,minDuration: 10,});expect(silentParts.length).toEqual(0);+ expect(audibleParts.length).toEqual(1);+ expect(audibleParts.length).toEqual(1);+ expect(audibleParts[0].startInSeconds).toEqual(0);+ expect(audibleParts[0].endInSeconds).toEqual(2.789);});test('Wrong file', async () => {