/
convert_to_audio.js
56 lines (48 loc) · 1.91 KB
/
convert_to_audio.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
/**
* @module convert_to_audio
* @description Convers video file into wav (or any ffmpeg supported input)
* takes in input file, output destination file, and callback,
* IBM sudgested audio compression {@link http://www.ibm.com/watson/developercloud/doc/speech-to-text/input.shtml#limits}
* callback returns audio file name/path
* @example <caption>usage</caption>
var convertToWav = require('./convert_to_audio.js');
convertToWav(file, audioFile, ffmpegPath, function(newFile) {
//do something with the converted file, returned as a string file path
}
* @requires fluent-ffmpeg
*/
//TODO: refactor with fluent ffmpeg so that can add ffmpeg bin to config json and input
// "use strict";
var ffmpeg = require('fluent-ffmpeg');
/**
* @function convertToWav
* @param {string} file - path to audio or viceo file to convert to wav
* @param {string} audioFile - path to output ogg audio file
* @param {string} ffmpegPath - path to ffmpeg binary. If not present it will try to use system one.
* @returns {callback} callback - callback to return audio file path as string.
*/
function convertToWav(file,audioFile,ffmpegPath, callback) {
//setting ffmpeg binary path
if(ffmpegPath) {
ffmpeg.setFfmpegPath(ffmpegPath);
} else {
console.warn("ffmpeg binary path not defined, so using system one. if available");
}
//running ffmpeg comand
var comand = ffmpeg(file)
.noVideo()
.audioCodec('libopus')
.audioChannels(1)
.audioFrequency(16000)
.output(audioFile)
.on('end',
//listener must be a function, so to return the callback wrapping it inside a function
function(){
callback(audioFile)
}
|| function() {
console.log('Finished processing');
}
).run();
}
module.exports = convertToWav;