更新时间:2021-08-05 22:36:45
我会这样做:
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
并且它是系统路径的一部分,还请确保所有必需的代码均已存在.
just make sure ffmpeg
is installed and is part of system path, also make sure all the necessary codes are present.
更新:
对于没有音频的视频,只需执行.noAudio().videoCodec('copy')
:
for video without audio, simply do .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:
用于将视频和音频合并为单个:
for merging video and audio into single:
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();
}