audio-split
Advanced tools
Comparing version 1.0.3 to 1.0.4
140
index.js
@@ -1,6 +0,6 @@ | ||
const fs = require('fs'), | ||
_ = require('lodash'), | ||
waveform = require('waveform-node'), | ||
ffmpeg = require('fluent-ffmpeg'), | ||
Heap = require('heap') | ||
const fs = require('fs'), | ||
_ = require('lodash'), | ||
ffmpeg = require('fluent-ffmpeg'), | ||
Heap = require('heap') | ||
const waveform = require('./streaming_waveform.js') | ||
@@ -46,7 +46,31 @@ var threshold; | ||
generateSubclips = function (splits, filepath, clipLength, callback) { | ||
let subclipsGenerated = 0; | ||
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(); | ||
}); | ||
} | ||
console.log(splits); | ||
for (let i = -1; i < splits.length; i++) { | ||
let startTime, duration; | ||
if (i === -1) { | ||
@@ -62,30 +86,46 @@ startTime = 0; | ||
} | ||
let splitPath = filepath.split('.'); | ||
ffmpeg(filepath) | ||
.setStartTime(startTime) | ||
.setDuration(duration) | ||
ffmpeg_instance | ||
.output(splitPath[0] + `-${i + 1}.` + splitPath[1]) | ||
.on('error', function (err) { | ||
callback(err); | ||
}) | ||
.on('end', function () { | ||
subclipPaths.push(splitPath[0] + `-${i + 1}.` + splitPath[1]); | ||
if (++subclipsGenerated === splits.length + 1) { | ||
callback(null, subclipPaths); | ||
} | ||
}) | ||
.run(); | ||
.seek(startTime) | ||
.setDuration(duration); | ||
subclipPaths.push(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} = params; | ||
ffmpeg(filepath).audioCodec('pcm_s16le').on('end', function(data) { | ||
console.log(data); | ||
}).on('error', function() { | ||
console.log('asdlokignasdkl') | ||
}) | ||
let {filepath, minClipLength, maxClipLength} = params; | ||
minClipLength = minClipLength ? minClipLength : 5; | ||
maxClipLength = maxClipLength ? maxClipLength : (minClipLength + 5); | ||
callback = callback || function () { | ||
}; | ||
callback = callback || function () {}; | ||
ffmpeg(filepath).ffprobe( function (err, metadata) { | ||
if (minClipLength > maxClipLength) { | ||
callback('Minimum clip length cannot be greater than maximum clip length.'); | ||
} | ||
ffmpeg(filepath).ffprobe(function (err, metadata) { | ||
if (err) { | ||
@@ -96,12 +136,8 @@ callback(err); | ||
let clipLength = metadata.format.duration; | ||
if (clipLength < minClipLength) { // return original clip | ||
callback(null, [filepath]); | ||
return; | ||
} | ||
minClipLength = minClipLength ? minClipLength : 5; | ||
let numOfSample = 5000; | ||
let samplesPerSecond = numOfSample / clipLength; | ||
let stepSize = samplesPerSecond / 10; | ||
let options = { numOfSample }; | ||
waveform.getWaveForm(filepath, options, function (err, frequencies) { | ||
// streaming version of this | ||
waveform(filepath, numOfSample).then((frequencies) => { | ||
if (err) { | ||
@@ -114,15 +150,34 @@ callback(err); | ||
let {start, end} = trimClip(frequencies); | ||
// let {start, end} = trimClip(frequencies); | ||
let sampleSplits = []; | ||
for (let i = start + minClipLength * samplesPerSecond; i + stepSize < end - minClipLength * samplesPerSecond; i += stepSize) { | ||
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); | ||
if (averageFrequency(segment) <= threshold) { | ||
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 | ||
}); | ||
return (frequency / frequencies.length) * clipLength | ||
}); | ||
generateSubclips(secondSplits, filepath, clipLength, function (err, subclipPaths) { | ||
@@ -135,4 +190,5 @@ if (err) { | ||
}) | ||
}); | ||
}) | ||
}); | ||
}; | ||
}; |
{ | ||
"name": "audio-split", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Slice audio file into subclips", | ||
@@ -22,2 +22,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"bluebird": "^3.5.0", | ||
"fluent-ffmpeg": "^2.1.2", | ||
@@ -24,0 +25,0 @@ "heap": "^0.2.6", |
@@ -22,2 +22,3 @@ # audio-split | ||
minClipLength: 5 | ||
maxClipLength: 10 | ||
}); | ||
@@ -30,1 +31,2 @@ ``` | ||
- `minClipLength` (float, optional, default 5) | ||
- `maxClipLength` (float, optional, default minClipLength + 5) |
10
test.js
@@ -5,6 +5,12 @@ var Promise = require('bluebird'); | ||
const splitClip = Promise.coroutine(function*(filepath) { | ||
let paths = yield split({filepath}); | ||
let paths; | ||
try { | ||
paths = yield split({filepath}); | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
console.log(paths); | ||
}); | ||
// splitClip('path/to/file.mp4'); // uncomment | ||
// splitClip('path/to/file.mp3'); // uncomment |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
12064
7
305
31
5
2
2
+ Addedbluebird@^3.5.0
+ Addedbluebird@3.7.2(transitive)