1

我具有以下将.wav文件转换为.mp3. 如您所见,在使用ffmpeg模块转换音频文件之前,我已经检查了文件是否存在,然后在转换时,我只保留新文件并删除旧文件。但偶尔控制台会向我抛出错误Error: ENOENT: no such file or directory, unlink,这意味着我unlink(删除)了一个不存在的文件。我不明白为什么,因为我什至在转换之前就已经进行了存在检查,所以它应该已经存在以取消链接。

module.exports.convertAndMoveElastic = async (calllog) => {
    let { start, sip_uri, direction, source, destination } = calllog;
    const VNtimezoneOffset = 7 + new Date().getTimezoneOffset() / 60;
    const startTime = new Date(start + VNtimezoneOffset * 3600000 - 60000);
    const date = startTime.getDate() < 10 ? `0${startTime.getDate().toString()}` : startTime.getDate().toString();
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    const month = months[startTime.getMonth()];
    const year = startTime.getFullYear().toString();
    sip_uri = sip_uri || (direction === 'outgoing' ? source : destination);
    const [extension, domain_name] = sip_uri.split("@");
    return new Promise(async (resolve, reject) => {
        const links = await getLinkWithElastic(calllog);
        if (!links) { return reject(); }
        let file_id, filepath;
        for (let link of links) {
            const { callid, sipCallid, uuid, record_path } = link._source;
            if (record_path) {
                let recordPathArr = record_path.split('/');
                file_id = recordPathArr[recordPathArr.length - 1].split('.')[0];
                filepath = path.resolve(base_directory, domain_name, 'archive', year, month, date, `${file_id}.wav`);
            }
            if (!file_id || !fs.existsSync(filepath)) {
                file_id = callid;
                filepath = path.resolve(base_directory, domain_name, 'archive', year, month, date, `${file_id}.wav`);
            }
            if (!file_id || !fs.existsSync(filepath)) {
                file_id = uuid;
                filepath = path.resolve(base_directory, domain_name, 'archive', year, month, date, `${file_id}.wav`);
            }
            if (fs.existsSync(filepath)) { break; }
        }
        if (!fs.existsSync(filepath)) { return reject(); }
        ffmpeg(filepath)
            .audioCodec('libmp3lame')
            .on('error', function (error) {
                reject(error);
            })
            .on('end', function () {
                resolve({ recordUrl: `${host}/record/download/${file_id}.mp3` });
                fs.unlinkSync(filepath);
            })
            .toFormat('mp3')
            .saveToFile(path.resolve(dest_directory, file_id + ".mp3"));
    });
};
4

1 回答 1

0

util.promisify在你的unlink()方法上使用

https://stackoverflow.com/a/45463672/3793648

于 2020-08-10T13:28:44.467 回答