我正在尝试借助Promise逐一转换视频。我正在使用ffmpeg进行转换,并使用multer来上传多个文件。
multer一次上传多个文件,之后我必须一一链接转换。到目前为止,它只是转换第一个文件。
我认为像数组中的promise链应该可以工作,但是如果ffmpeg还返回promise,我可以在数组中定义新的promise,我感到困惑
我的路由器:
const router = require('express').Router();
const multer = require('multer');
const ffmpeg = require('ffmpeg');
let str;
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './uploads');
},
filename: (req, file, cb) => {
str = file.originalname.replace(/\.[^/.]+$/, "");
str = str.replace(/[^a-z0-9+]+/gi, '_') + '.' + file.originalname.replace(/^.*\./, '');
cb(null, str);
}
});
const upload = multer({ storage: storage }).array('files', 12);
router.post('/upload', (req, res, next) => {
// req.files is an array of files
// req.body will contain the text fields, if there were any
function uploadFile() {
return new Promise((resolve, reject) => {
upload(req, res, (err) => {
if (err) {
res.send(err) // Pass errors to Express.
reject(`Error: Something went wrong!`);
} else if (req.files == undefined) {
res.send(`No File selected.`);
resolve();
} else if (!err && req.files.length > 0) {
res.status(201).send(`${req.files.length} File(s): ${req.files} uploaded successfully.`);
console.log('uploaded');
resolve();
}
});
});
}
uploadFile().then(() => {
try {
var process = new ffmpeg('./uploads/' + str);
process.then(function (video) {
console.log('The video is ready to be processed');
video.addCommand('-hide_banner', '');
video.addCommand('-y', '');
video.addCommand('-c:a', 'aac');
video.addCommand('-ar', '48000');
video.addCommand('-c:v', 'h264');
video.addCommand('-profile:v', 'main');
video.addCommand('-crf', '20');
video.addCommand('-sc_threshold', '0');
video.addCommand('-g', '50');
video.addCommand('-keyint_min', '50');
video.addCommand('-hls_time', '4');
video.addCommand('-hls_playlist_type', 'vod');
video.addCommand('-vf', 'scale=-2:720');
video.addCommand('-b:v', '1400k');
video.addCommand('-maxrate', '1498k');
video.addCommand('-bufsize', '2100k');
video.addCommand('-b:a', '128k');
video.save('./converted/' + str, function (error, file) {
if (!error)
console.log('Video file: ' + file);
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
}).catch((err) => {
console.log(Error, err);
});
});
module.exports = router;
答案 0 :(得分:0)
我将使用Promise.all
异步处理上传,上传本身会返回带有结果数组的单个promise。
示例:
const express = require ( 'express' );
const router = express.Router ( {} );
const multer = require ( 'multer' );
const upload = multer ( { dest: 'uploads/' } );
// Process a single upload - returns a promise
const processUpload = file => {
return new Promise ( ( resolve, reject ) => {
// Processing code here
// ...
// Return the promise when done
return resolve ( path );
} );
};
// Handle upload route
router.post ( '/upload', [
// Process the multiple file upload
upload.array ( 'files', 12 ),
// Controller - process the uploaded files
( req, res, next ) => {
// Create an array of promises for each uploaded file in req.files
let promises = req.files.map ( file => processUpload ( file ) );
Promise.all ( promises )
.then ( results => {
// Handle the finished results here
return res
.status ( 201 )
.json ( {
message: `${results.length} files successfully uploaded`
} )
;
} )
.catch ( e => next ( e ) )
;
}
] );
更新以按顺序处理上传:
使用异步库进行更好的控制:http://caolan.github.io/async/docs.html#eachSeries
const express = require ( 'express' );
const router = express.Router ( {} );
const async = require ( 'async' );
const multer = require ( 'multer' );
const upload = multer ( { dest: 'uploads/' } );
// Process a single upload - returns a promise
const processUpload = file => {
return new Promise ( ( resolve, reject ) => {
// Processing code here
const process = new ffmpeg ( /../ );
process
.then ( video => {
// Return the promise when done
return resolve ( something );
} )
.catch ( e => reject ( e ) )
;
} );
};
// Handle upload route
router.post ( '/upload', [
// Process the multiple file upload
upload.array ( 'files', 12 ),
// Controller - process the uploaded files
( req, res, next ) => {
// Process multiple files sequentially
let results = [];
async.eachSeries (
// Array to iterate through
req.files,
// Callback per file
( file, callback ) => {
processUpload(file)
.then(r => {
// Compile results and return this callback
results.push(r);
callback()
})
.catch ( e => callback ( e ) )
;
},
// Completion handler
e => {
if ( e ) {
return next ( e );
}
// Handle the finished results here
return res
.status ( 201 )
.json ( {
message: `${results.length} files successfully uploaded`
} )
;
}
);
}
] );
希望这会有所帮助。