[英]video to audio file convert & save through FFMPEG in node js
我正在节点js上进行FFMPEG处理。 我想使用node js从视频文件中检索音轨。 我也想保存这样的文件,但是我不知道怎么做。
尽管这行代码对我有帮助:
ffmpeg('/path/to/file.avi').noVideo();
我已经在npm包中得到了这个。 我不太了解如何使用它以及如何实际保存音频文件。
还有一些其他的代码行:
try {
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
// Callback mode
video.fnExtractSoundToMP3('/path/to/your_audio_file.mp3', function (error, file) {
if (!error)
console.log('Audio file: ' + file);
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
我的问题是:
如何从FFMPEG视频中检索音频? 我该如何保存?
我会这样做:
var ffmpeg = require('fluent-ffmpeg');
/**
* input - string, path of input file
* output - string, path of output file
* callback - function, node-style callback fn (error, result)
*/
function convert(input, output, callback) {
ffmpeg(input)
.output(output)
.on('end', function() {
console.log('conversion ended');
callback(null);
}).on('error', function(err){
console.log('error: ', e.code, e.msg);
callback(err);
}).run();
}
convert('./df.mp4', './output.mp3', function(err){
if(!err) {
console.log('conversion complete');
//...
}
});
只需确保已安装ffmpeg
并将其作为系统路径的一部分,并确保所有必需的代码都已存在。
更新:
对于没有音频的视频,只需执行.noAudio().videoCodec('copy')
:
function copyWitoutAudio(input, output, callback) {
ffmpeg(input)
.output(output)
.noAudio().videoCodec('copy')
.on('end', function() {
console.log('conversion ended');
callback(null);
}).on('error', function(err){
console.log('error: ', err);
callback(err);
}).run();
}
更新2:
用于将视频和音频合并为单个:
function mergeMedia(aud, vid, output, callback) {
ffmpeg()
.input(aud)
.input(vid)
.output(output)
.outputOptions(
'-strict', '-2',
'-map', '0:0',
'-map', '1:0'
).on('end', function() {
console.log('conversion ended');
callback(null);
}).on('error', function(err){
console.log('error: ', err);
callback(err);
}).run();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.