forked from calufornia/audio-split
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (138 loc) · 4.85 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require('fs'),
_ = require('lodash'),
ffmpeg = require('fluent-ffmpeg'),
Heap = require('heap')
const waveform = require('./streaming_waveform.js')
var threshold;
averageFrequency = function (frequencies) {
let totalFrequency = 0;
for (let i = 0; i < frequencies.length; i++) {
totalFrequency += frequencies[i];
}
return totalFrequency / frequencies.length;
};
calculateThreshold = function (frequencies) {
return averageFrequency(Heap.nsmallest(frequencies.filter(function (frequency) {
return frequency > 0;
}), frequencies.length / 2));
};
generateSubclips = async function (splits, filepath, clipLength, callback) {
let subclipPaths = [];
let ffmpeg_instance = ffmpeg(filepath);
if (!splits || !splits.length || splits.length === 0) {
let splitPath = filepath.split('.');
subclipPaths.push(splitPath[0] + `-0.` + splitPath[1]);
await new Promise(function (resolve, reject) {
ffmpeg_instance
.output(splitPath[0] + `-0.` + splitPath[1])
.on('error', function (err) {
callback(err);
})
.on('end', function () {
if (subclipPaths.length === splits.length + 1) {
callback(null, subclipPaths);
}
return resolve();
})
.run();
});
}
for (let i = -1; i < splits.length; i++) {
let startTime, duration;
if (i === -1) {
startTime = 0;
duration = splits[0];
} else if (i === splits.length - 1) {
startTime = splits[i];
duration = clipLength - startTime;
} else {
startTime = splits[i];
duration = splits[i + 1] - splits[i];
}
let splitPath = filepath.split('.');
ffmpeg_instance
.output(splitPath[0] + `-${i + 1}.` + splitPath[1])
.seek(startTime)
.setDuration(duration);
subclipPaths.push({startTime, endTime: startTime + duration, filepath: splitPath[0] + `-${i + 1}.` + splitPath[1]});
if (i % 10 === 0 || i === splits.length - 1) {
await new Promise(function (resolve, reject) {
ffmpeg_instance
.on('error', function (err) {
callback(err);
})
.on('end', function () {
if (subclipPaths.length === splits.length + 1) {
callback(null, subclipPaths);
}
return resolve();
})
.run();
});
ffmpeg_instance = ffmpeg(filepath);
}
}
};
module.exports = function (params, callback) {
let {filepath, minClipLength, maxClipLength} = params;
minClipLength = minClipLength ? minClipLength : 5;
maxClipLength = maxClipLength ? maxClipLength : (minClipLength + 5);
callback = callback || function () {
};
if (minClipLength > maxClipLength) {
callback('Minimum clip length cannot be greater than maximum clip length.');
}
ffmpeg(filepath).ffprobe(function (err, metadata) {
if (err) {
callback(err);
return;
}
let clipLength = metadata.format.duration;
let numOfSample = 5000;
let samplesPerSecond = numOfSample / clipLength;
let stepSize = samplesPerSecond / 10;
// streaming version of this
waveform(filepath, numOfSample).then((frequencies) => {
if (err) {
callback(err);
return;
}
threshold = calculateThreshold(frequencies);
let sampleSplits = [];
let lastSplit = minClipLength * samplesPerSecond;
let quietestSecond = minClipLength * samplesPerSecond;
let quietestFreq = averageFrequency(frequencies.slice(quietestSecond, quietestSecond + stepSize));
for (let i = minClipLength * samplesPerSecond; i + stepSize < frequencies.length - minClipLength * samplesPerSecond; i += stepSize) { // iterating through frequency space
let segment = frequencies.slice(i, i + stepSize);
let avgFreq = averageFrequency(segment);
if (avgFreq <= threshold) {
sampleSplits.push(i + stepSize / 2);
lastSplit = i;
i += minClipLength * samplesPerSecond;
quietestSecond = i;
quietestFreq = averageFrequency(frequencies.slice(i, i + stepSize));
} else if ((i - lastSplit) > maxClipLength * samplesPerSecond) {
sampleSplits.push(quietestSecond + stepSize / 2);
lastSplit = quietestSecond;
i = quietestSecond + minClipLength * samplesPerSecond;
quietestSecond = i;
quietestFreq = averageFrequency(frequencies.slice(i, i + stepSize));
}
if (avgFreq < quietestFreq) {
quietestFreq = avgFreq;
quietestSecond = i;
}
}
let secondSplits = _.map(sampleSplits, (frequency) => {
return (frequency / frequencies.length) * clipLength
});
generateSubclips(secondSplits, filepath, clipLength, function (err, subclipPaths) {
if (err) {
callback(err);
} else {
callback(null, subclipPaths);
}
})
})
});
};