Skip to content

Commit ada8951

Browse files
committed
make ffprobe format detection optional
1 parent 3660910 commit ada8951

File tree

2 files changed

+157
-106
lines changed

2 files changed

+157
-106
lines changed

library/audioDataAnalyzer.js

Lines changed: 146 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -20,144 +20,179 @@ var analyzer = function analyzerConstructor() {
2020

2121
this.trackData = {};
2222

23+
this.detectFormat = false;
24+
2325
};
2426

2527
/**
2628
*
27-
* get track data using ffprobe (channels, samplerate, ...)
29+
* set detect format option
30+
*
31+
* @param {type} detectFormat
32+
* @returns {undefined}
33+
*/
34+
analyzer.prototype.setDetectFormat = function setDetectFormatFunction(detectFormat) {
35+
36+
this.detectFormat = detectFormat;
37+
38+
};
39+
40+
/**
41+
*
42+
* get detect format option
43+
*
44+
* @returns {undefined}
45+
*/
46+
analyzer.prototype.getDetectFormat = function getDetectFormatFunction() {
47+
48+
return this.detectFormat;
49+
50+
};
51+
52+
/**
53+
*
54+
* get track format using ffprobe (channels, samplerate, ...)
2855
*
2956
* @param {type} trackPath
3057
* @param {type} callback
3158
* @returns {undefined}
3259
*/
33-
analyzer.prototype.getData = function getDataFunction(trackPath, callback) {
60+
analyzer.prototype.getFormat = function getFormatFunction(trackPath, callback) {
3461

3562
//console.log(trackPath);
3663

37-
var that = this;
38-
39-
// ffprobe file data
40-
var ffprobeSpawn = childProcessSpawn(
41-
'ffprobe',
42-
[
43-
trackPath,
44-
'-v',
45-
'quiet',
46-
'-show_streams',
47-
'-show_format',
48-
'-print_format',
49-
'json'
50-
]
51-
);
52-
53-
//ffprobeSpawn.stdout.setEncoding('utf8');
54-
ffprobeSpawn.stderr.setEncoding('utf8');
64+
if (this.detectFormat) {
5565

56-
// ffprobe recieves data on stdout
57-
ffprobeSpawn.stdout.on('data', function(data) {
66+
var that = this;
5867

59-
that.stdoutFfprobeOuputString += data;
60-
61-
});
68+
// ffprobe file data
69+
var ffprobeSpawn = childProcessSpawn(
70+
'ffprobe',
71+
[
72+
trackPath,
73+
'-v',
74+
'quiet',
75+
'-show_streams',
76+
'-show_format',
77+
'-print_format',
78+
'json'
79+
]
80+
);
6281

63-
ffprobeSpawn.stdout.on('end', function(data) {
82+
//ffprobeSpawn.stdout.setEncoding('utf8');
83+
ffprobeSpawn.stderr.setEncoding('utf8');
6484

65-
//console.log('ffprobeSpawn stdout end');
66-
//console.log(that.stdoutFfprobeOuputString);
85+
// ffprobe recieves data on stdout
86+
ffprobeSpawn.stdout.on('data', function(data) {
6787

68-
if (that.stdoutFfprobeOuputString !== '') {
88+
that.stdoutFfprobeOuputString += data;
6989

70-
// parse the ffprobe json string response
71-
var stdoutOuput = JSON.parse(that.stdoutFfprobeOuputString);
72-
73-
//console.log(stdoutOuput);
74-
//console.log(Object.keys(stdoutOuput).length);
75-
76-
if (Object.keys(stdoutOuput).length > 0) {
77-
78-
// create a trackdata object with the informations we need
79-
that.trackData.duration = stdoutOuput['format']['duration'];
80-
that.trackData.size = stdoutOuput['format']['size'];
81-
that.trackData.bitRate = stdoutOuput['format']['bit_rate'];
82-
that.trackData.sampleRate = stdoutOuput['streams'][0]['sample_rate'];
83-
that.trackData.channels = stdoutOuput['streams'][0]['channels'];
84-
85-
}
86-
87-
//console.log(that.trackData);
90+
});
8891

89-
}
92+
ffprobeSpawn.stdout.on('end', function(data) {
9093

91-
});
94+
//console.log('ffprobeSpawn stdout end');
95+
//console.log(that.stdoutFfprobeOuputString);
9296

93-
ffprobeSpawn.stderr.on('data', function(data) {
97+
if (that.stdoutFfprobeOuputString !== '') {
9498

95-
that.stderrFfprobeOuputString += data;
99+
// parse the ffprobe json string response
100+
var stdoutOuput = JSON.parse(that.stdoutFfprobeOuputString);
96101

97-
});
102+
//console.log(stdoutOuput);
103+
//console.log(Object.keys(stdoutOuput).length);
98104

99-
ffprobeSpawn.stderr.on('end', function() {
105+
if (Object.keys(stdoutOuput).length > 0) {
100106

101-
//console.log('ffprobeSpawn stderr end');
107+
// create a trackdata object with the informations we need
108+
that.trackData.duration = stdoutOuput['format']['duration'];
109+
that.trackData.size = stdoutOuput['format']['size'];
110+
that.trackData.bitRate = stdoutOuput['format']['bit_rate'];
111+
that.trackData.sampleRate = stdoutOuput['streams'][0]['sample_rate'];
112+
that.trackData.channels = stdoutOuput['streams'][0]['channels'];
102113

103-
});
114+
}
104115

105-
ffprobeSpawn.on('exit', function(code) {
116+
//console.log(that.trackData);
106117

107-
//console.log('ffprobeSpawn exit, code: ' + code);
108-
109-
// if the code is an error code
110-
if (code > 0) {
111-
112-
if (that.stderrFfprobeOuputString === '') {
113-
114-
that.stderrFfprobeOuputString = 'unknown ffprobe error';
115-
116118
}
117-
118-
callback(that.stderrFfprobeOuputString);
119-
120-
} else {
121-
122-
//console.log(that.trackData);
123-
//console.log(Object.keys(that.trackData).length);
124-
125-
// if the trackdata object isnt empty
126-
if (Object.keys(that.trackData).length > 0) {
127-
128-
callback(false, that.trackData);
129-
119+
120+
});
121+
122+
ffprobeSpawn.stderr.on('data', function(data) {
123+
124+
that.stderrFfprobeOuputString += data;
125+
126+
});
127+
128+
ffprobeSpawn.stderr.on('end', function() {
129+
130+
//console.log('ffprobeSpawn stderr end');
131+
132+
});
133+
134+
ffprobeSpawn.on('exit', function(code) {
135+
136+
//console.log('ffprobeSpawn exit, code: ' + code);
137+
138+
// if the code is an error code
139+
if (code > 0) {
140+
141+
if (that.stderrFfprobeOuputString === '') {
142+
143+
that.stderrFfprobeOuputString = 'unknown ffprobe error';
144+
145+
}
146+
147+
callback(that.stderrFfprobeOuputString);
148+
130149
} else {
131-
132-
//console.log('ffprobe did not output any data');
133-
134-
callback('ffprobe did not output any data');
135-
150+
151+
//console.log(that.trackData);
152+
//console.log(Object.keys(that.trackData).length);
153+
154+
// if the trackdata object isnt empty
155+
if (Object.keys(that.trackData).length > 0) {
156+
157+
callback(false, that.trackData);
158+
159+
} else {
160+
161+
//console.log('ffprobe did not output any data');
162+
163+
callback('ffprobe did not output any data');
164+
165+
}
166+
136167
}
137-
138-
}
139168

140-
});
169+
});
141170

142-
ffprobeSpawn.on('close', function() {
171+
ffprobeSpawn.on('close', function() {
143172

144-
//console.log('ffprobeSpawn close');
173+
//console.log('ffprobeSpawn close');
145174

146-
});
147-
148-
ffprobeSpawn.on('error', function(error) {
175+
});
149176

150-
if (error.code === 'ENOENT') {
177+
ffprobeSpawn.on('error', function(error) {
151178

152-
callback('Unable to locate ffprobe, check it is installed and in the path');
153-
154-
} else {
155-
156-
callback(error.syscall + ' ' + error.errno);
157-
158-
}
179+
if (error.code === 'ENOENT') {
159180

160-
});
181+
callback('Unable to locate ffprobe, check it is installed and in the path');
182+
183+
} else {
184+
185+
callback(error.syscall + ' ' + error.errno);
186+
187+
}
188+
189+
});
190+
191+
} else {
192+
193+
callback(false, null);
194+
195+
}
161196

162197
};
163198

@@ -174,7 +209,7 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
174209

175210
var that = this;
176211

177-
this.getData(trackPath, function(error, trackData) {
212+
this.getFormat(trackPath, function(error, trackData) {
178213

179214
if (!error) {
180215

@@ -188,6 +223,15 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
188223

189224
}
190225

226+
if (trackData === null) {
227+
228+
trackData = {};
229+
230+
trackData.sampleRate = 44100;
231+
trackData.channels = 1;
232+
233+
}
234+
191235
// get audio pcm as 16bit little endians
192236
var ffmpegSpawn = childProcessSpawn(
193237
'ffmpeg',
@@ -197,9 +241,7 @@ analyzer.prototype.getPeaks = function getValuesFunction(trackPath, peaksAmountR
197241
'-f',
198242
's16le',
199243
'-ac',
200-
//trackData.channels,
201-
// put everything into one channel
202-
1,
244+
trackData.channels,
203245
'-acodec',
204246
'pcm_s16le',
205247
'-ar',

library/waveformData.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var queryObjectToOptions = function queryObjectToOptionsFunction(queryObject) {
1515
method: 'GET',
1616
serverDirectory: queryObject.serverDirectory,
1717
fileName: queryObject.trackId + '.' + queryObject.trackFormat,
18-
service: queryObject.service
18+
service: queryObject.service,
19+
detectFormat: queryObject.detectFormat
1920
};
2021

2122
if (options.serverDirectory === undefined) {
@@ -30,6 +31,12 @@ var queryObjectToOptions = function queryObjectToOptionsFunction(queryObject) {
3031

3132
}
3233

34+
if (options.detectFormat === undefined) {
35+
36+
options.detectFormat = false;
37+
38+
}
39+
3340
return options;
3441

3542
};
@@ -39,8 +46,10 @@ var analyzeAudio = function analyzeAudioFunction(filePath, options, callback) {
3946
// initialize the audioAnalyzer
4047
var audioDataAnalyzer = new AudioDataAnalyzer();
4148

49+
audioDataAnalyzer.setDetectFormat(options.detectFormat);
50+
4251
//console.log(filePath);
43-
52+
4453
// analyze the track using ffmpeg
4554
audioDataAnalyzer.getPeaks(filePath, options.peaksAmount, function getValuesCallback(error, peaks) {
4655

0 commit comments

Comments
 (0)