-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.js
76 lines (69 loc) · 2.97 KB
/
converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const path = require('path');
const { exec } = require('child_process');
function extractBackgroundMusic(videoPath, targetDirPath) {
return new Promise((resolve, reject) => {
// Extract audio from the video
const audiofileName = `originalAudio-${Date.now()}`;
const audioPath = path.join(targetDirPath, `${audiofileName}.mp3`);
const command = `ffmpeg -i ${videoPath} -map 0:a:0 ${audioPath}`;
exec(command, (err) => {
if (err) {
console.log('error extracting audio', err);
return reject(err);
}
// Extract the background music from the video's audio
const spleeterCommand = `bash separate.sh ${audioPath} ${audiofileName} ${targetDirPath}/output`
console.log('command', spleeterCommand)
// const spleeterCommand = `spleeter separate -i ${audioPath} -p spleeter:2stems -o ${targetDirPath}/output`;
exec(spleeterCommand, (err, stdout, stderr) => {
if (err) {
console.log('error splitting with spleeter', err, stderr, stdout);
return reject(err);
}
return resolve(path.join(targetDirPath, 'output', audiofileName, 'accompaniment.wav'));
})
})
})
}
function extractVoice(videoPath, targetDirPath) {
return new Promise((resolve, reject) => {
// Extract audio from the video
const audiofileName = `originalAudio-${Date.now()}`;
const audioPath = path.join(targetDirPath, `${audiofileName}.mp3`);
const command = `ffmpeg -i ${videoPath} -map 0:a:0 ${audioPath}`;
exec(command, (err) => {
if (err) {
console.log('error extracting audio', err);
return reject(err);
}
// Extract the background music from the video's audio
const spleeterCommand = `bash separate.sh ${audioPath} ${audiofileName} ${targetDirPath}/output`
console.log('command', spleeterCommand)
// const spleeterCommand = `spleeter separate -i ${audioPath} -p spleeter:2stems -o ${targetDirPath}/output`;
exec(spleeterCommand, (err, stdout, stderr) => {
if (err) {
console.log('error splitting with spleeter', err, stderr, stdout);
return reject(err);
}
return resolve(path.join(targetDirPath, 'output', audiofileName, 'vocals.wav'));
})
})
})
}
function compressAudioFile(filePath, targetPath) {
return new Promise((resolve, reject) => {
const command = `ffmpeg -i ${filePath} -af "volume=1.5" ${targetPath}`;
exec(command, (err) => {
if (err) {
console.log('error compressing file', err);
return reject(err);
}
return resolve(targetPath);
})
})
}
module.exports = {
extractBackgroundMusic,
compressAudioFile,
extractVoice
}