-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnormalize-volume.js
More file actions
85 lines (71 loc) · 2.63 KB
/
Copy pathnormalize-volume.js
File metadata and controls
85 lines (71 loc) · 2.63 KB
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
77
78
79
80
81
82
83
84
85
/*!
* Normalize Volume, http://tpkn.me/
*/
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
function NormalizeVolume(input_file, output_file, options = {}){
let {
volume = 0.5,
normalize = true,
ffmpeg_bin = 'ffmpeg',
convert_bin = 'convert',
waveform,
silent = true
} = options;
let cmd = [];
let errors = [];
let result = { file: output_file };
// Normalize or just change the volume
if(normalize){
cmd.push(`"${ffmpeg_bin}" -i "${input_file}" -y -c:v libx264 -c:a aac -ar 44100 -vcodec copy -filter:a loudnorm=print_format=json "${output_file}"`);
}else{
cmd.push(`"${ffmpeg_bin}" -i "${input_file}" -y -filter:a "volume=${volume}" "${output_file}"`);
}
// Create two waveforms (before and after converting) merged together for comparison
if(waveform){
let {
image_before = output_file + '_before.png',
image_after = output_file + '_after.png',
image_comparison = output_file + '_comparison.png',
width = 400,
height = 225,
before_color = 'white',
after_color = '#ff00b3',
} = waveform;
// 'Before' waveform
cmd.unshift(`"${ffmpeg_bin}" -i "${input_file}" -y -filter_complex "showwavespic=s=${width}x${height}:colors=${before_color}:split_channels=1" -frames:v 1 "${image_before}"`);
// 'After' waveform
cmd.push(`"${ffmpeg_bin}" -i "${output_file}" -y -filter_complex "showwavespic=s=${width}x${height}:colors=${after_color}:split_channels=1" -frames:v 1 "${image_after}"`);
// Merging
cmd.push(`"${convert_bin}" "${image_before}" "${image_after}" -gravity NorthEast -composite "${image_comparison}"`);
// Cleanup a bit
cmd.push(`del /f ${image_before}`);
cmd.push(`del /f ${image_after}`);
result.waveform = image_comparison;
}
return new Promise((resolve, reject) => {
let child = spawn(cmd.join(' && '), { shell: true });
child.stdout.on('data', (data) => {
if(!silent){
console.log(`${data}`);
}
});
child.stderr.on('data', (data) => {
errors.push(data.toString());
if(!silent){
console.log(`${data}`);
}
});
child.on('exit', (exitCode) => {
child.stdin.pause();
child.kill();
if(exitCode == 0){
resolve(result);
}else{
reject({ code: exitCode, errors });
}
});
})
}
module.exports = NormalizeVolume;