bil*_*ash 5 javascript audio video ffmpeg node.js
I am working on an FFMPEG on node js. I'd like to retrieve the audio track from a video file using node js. I would also like to save such file but I can't figure out how.
I though this line of code would help me :
ffmpeg('/path/to/file.avi').noVideo();
Run Code Online (Sandbox Code Playgroud)
I have got this in npm package. I don't quite understand how to work with this and how to actually save the audio file.
Some other line of code that come in play :
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);
}
Run Code Online (Sandbox Code Playgroud)
My question is:
如何从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');
//...
}
});
Run Code Online (Sandbox Code Playgroud)
只需确保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();
}
Run Code Online (Sandbox Code Playgroud)
更新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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5837 次 |
| 最近记录: |