67 lines
2.9 KiB
JavaScript
67 lines
2.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getSilentParts = void 0;
|
|
const compositor_1 = require("./compositor/compositor");
|
|
/*
|
|
* @description Gets the silent parts of a video or audio in Node.js. Useful for cutting out silence from a video.
|
|
* @see [Documentation](https://www.remotion.dev/docs/renderer/get-silent-parts)
|
|
*/
|
|
const getSilentParts = async ({ src, noiseThresholdInDecibels: passedNoiseThresholdInDecibels, minDurationInSeconds: passedMinDuration, logLevel, binariesDirectory, }) => {
|
|
const compositor = (0, compositor_1.startLongRunningCompositor)({
|
|
maximumFrameCacheItemsInBytes: null,
|
|
logLevel: logLevel !== null && logLevel !== void 0 ? logLevel : 'info',
|
|
indent: false,
|
|
binariesDirectory: binariesDirectory !== null && binariesDirectory !== void 0 ? binariesDirectory : null,
|
|
extraThreads: 0,
|
|
});
|
|
const minDurationInSeconds = passedMinDuration !== null && passedMinDuration !== void 0 ? passedMinDuration : 1;
|
|
if (typeof minDurationInSeconds !== 'number') {
|
|
throw new Error(`minDurationInSeconds must be a number, but was ${minDurationInSeconds}`);
|
|
}
|
|
if (minDurationInSeconds <= 0) {
|
|
throw new Error(`minDurationInSeconds must be greater than 0, but was ${minDurationInSeconds}`);
|
|
}
|
|
const noiseThresholdInDecibels = passedNoiseThresholdInDecibels !== null && passedNoiseThresholdInDecibels !== void 0 ? passedNoiseThresholdInDecibels : -20;
|
|
if (typeof noiseThresholdInDecibels !== 'number') {
|
|
throw new Error(`noiseThresholdInDecibels must be a number, but was ${noiseThresholdInDecibels}`);
|
|
}
|
|
if (noiseThresholdInDecibels >= 30) {
|
|
throw new Error(`noiseThresholdInDecibels must be less than 30, but was ${noiseThresholdInDecibels}`);
|
|
}
|
|
const res = await compositor.executeCommand('GetSilences', {
|
|
src,
|
|
minDurationInSeconds,
|
|
noiseThresholdInDecibels,
|
|
});
|
|
const response = JSON.parse(new TextDecoder('utf-8').decode(res));
|
|
await compositor.finishCommands();
|
|
await compositor.waitForDone();
|
|
const { silentParts, durationInSeconds } = response;
|
|
return {
|
|
silentParts,
|
|
audibleParts: getAudibleParts({ silentParts, durationInSeconds }),
|
|
durationInSeconds,
|
|
};
|
|
};
|
|
exports.getSilentParts = getSilentParts;
|
|
const getAudibleParts = ({ silentParts, durationInSeconds, }) => {
|
|
const audibleParts = [];
|
|
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;
|
|
};
|