因为ffmpeg.ffprobe是异步的,需要使用promise同步,然后Promise.all将所有的Promise执行结果处理后返回
/** * 获取文件列表时长(同步获取返回) * @param {Object} fileList */ async getFileDuration(fileList) { // 初始化 let promises = fileList.map(file => { return new Promise((resolve, reject) => { ffmpeg.ffprobe(file, (err, data) => { if (err) { reject(`无法获取视频信息: ${err}`); } else { resolve(data.format.duration); } }); }); }); return Promise.all(promises) .then(res => { let durationList = []; fileList.forEach((file, index) => { durationList.push({name: file, time: Math.ceil(res[index])}); }); return durationList; }); }
调用获取结果
let duration = await Services.get('file').getFileDuration(fileList); console.log(fileList, duration);
标签:err,批量,nodejs,fileList,获取,Promise,file,duration,时长 From: https://www.cnblogs.com/lanse1993/p/17794528.html