【发布时间】:2017-09-21 13:38:54
【问题描述】:
在执行任务B代码之前,我需要等待任务A完成。
任务A是转换音频文件和
任务 B 使用转换后的音频进行进一步处理。
因为任务 A 将新的音频文件存储到特定目录,而任务 B 正在尝试访问不存在的文件,我的代码中断了。
如何确保新音频文件保存到目录后任务 B 代码执行?
代码
var track = fileURL;//your path to source file
ffmpeg(track)
.toFormat('flac')
.on('error', function (err) {
console.log('An error occurred: ' + err.message);
})
.on('progress', function (progress) {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', function () {
console.log('Processing finished !');
})
.save(path.join(__dirname, '/public/downloads/Test.flac'));//path where you want to save your file
以上部分代码从uploads文件夹中取出文件,将其转换为新的文件格式,并保存到downloads目录中。
您可以在下面看到我正在尝试访问下载文件夹中的文件(Test.flac)。还有很多代码,但我需要在完成上述任务后执行这段代码。
const Speech = require('@google-cloud/speech');
const projectId = 'uliq-68823';
// Instantiates a client
const speechClient = Speech({
projectId: projectId
});
// The name of the audio file to transcribe
const fileName2 = path.join(__dirname, '/public/downloads/' + 'Test.flac');
// Reads a local audio file and converts it to base64
const file2 = fs.readFileSync(fileName2);
const audioBytes = file2.toString('base64');
【问题讨论】:
标签: javascript node.js audio google-speech-api