audiox-processor
Version:
AudioX: Transform Any Inspiration Into Professional Audio With AI - Audio processing utilities for the AudioX platform
139 lines (120 loc) • 3.56 kB
JavaScript
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const os = require('os');
// Create temporary directory for processing
const TEMP_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'audiox-'));
/**
* Extract audio from a video file
* @param {string} videoPath - Path to the input video file
* @param {string} outputPath - Path to save the extracted audio
* @returns {Promise<string>} - Path to the extracted audio file
*/
function extractAudio(videoPath, outputPath) {
return new Promise((resolve, reject) => {
const args = [
'-i', videoPath,
'-vn', '-acodec', 'pcm_s16le',
'-ar', '44100', '-ac', '2',
outputPath, '-y'
];
const ffmpeg = spawn('ffmpeg', args);
ffmpeg.on('close', (code) => {
if (code === 0) {
resolve(outputPath);
} else {
reject(new Error(`FFmpeg process exited with code ${code}`));
}
});
ffmpeg.stderr.on('data', (data) => {
console.log(`ffmpeg: ${data}`);
});
});
}
/**
* Remove audio from a video file
* @param {string} videoPath - Path to the input video file
* @param {string} outputPath - Path to save the video without audio
* @returns {Promise<string>} - Path to the video file without audio
*/
function removeAudio(videoPath, outputPath) {
return new Promise((resolve, reject) => {
const args = [
'-i', videoPath,
'-c', 'copy', '-an',
outputPath, '-y'
];
const ffmpeg = spawn('ffmpeg', args);
ffmpeg.on('close', (code) => {
if (code === 0) {
resolve(outputPath);
} else {
reject(new Error(`FFmpeg process exited with code ${code}`));
}
});
ffmpeg.stderr.on('data', (data) => {
console.log(`ffmpeg: ${data}`);
});
});
}
/**
* Add audio to a video file
* @param {string} videoPath - Path to the input video file
* @param {string} audioPath - Path to the audio file to add
* @param {string} outputPath - Path to save the combined video
* @returns {Promise<string>} - Path to the combined video file
*/
function addAudioToVideo(videoPath, audioPath, outputPath) {
return new Promise((resolve, reject) => {
const args = [
'-i', videoPath,
'-i', audioPath, '-map', '0:v',
'-map', '1:a', '-c:v', 'copy',
'-shortest', outputPath, '-y'
];
const ffmpeg = spawn('ffmpeg', args);
ffmpeg.on('close', (code) => {
if (code === 0) {
resolve(outputPath);
} else {
reject(new Error(`FFmpeg process exited with code ${code}`));
}
});
ffmpeg.stderr.on('data', (data) => {
console.log(`ffmpeg: ${data}`);
});
});
}
/**
* Reverse an audio file
* @param {string} audioPath - Path to the input audio file
* @param {string} outputPath - Path to save the reversed audio
* @returns {Promise<string>} - Path to the reversed audio file
*/
function reverseAudio(audioPath, outputPath) {
return new Promise((resolve, reject) => {
const args = [
'-i', audioPath,
'-af', 'areverse',
outputPath, '-y'
];
const ffmpeg = spawn('ffmpeg', args);
ffmpeg.on('close', (code) => {
if (code === 0) {
resolve(outputPath);
} else {
reject(new Error(`FFmpeg process exited with code ${code}`));
}
});
ffmpeg.stderr.on('data', (data) => {
console.log(`ffmpeg: ${data}`);
});
});
}
module.exports = {
extractAudio,
removeAudio,
addAudioToVideo,
reverseAudio,
TEMP_DIR
};