无法使用 ffmpeg 找到文件
Can not find file with ffmpeg
我正在 node.js 中对服务器进行编程以处理音频文件。
我用这个代码保存文件:
app.use(upload());
app.get("/", function (req, res) {
res.sendFile("index.html")
});
//Uploaded files will be saved
app.post("/", function (req, res) {
if (req.files) {
var file = req.files.filename,
filename = file.name;
file.mv("./upload/" + filename, function (err) {
if (err) {
console.log("err");
}
else {
res.send("Done");
console.log(filename);
convert(filename);
}
})
}
})
我将文件保存在我的上传目录中。一切都很好,但现在问题来了。
我用 ffmpeg
转换文件
function convert(filename) {
var realName = "./upload/" + filename;
var newName = "./output/" + filename.substr(0, filename.length - 4) + ".flac";
ffmpegN = ffmpeg(realName);
ffmpegN.audioBitrate(16).audioFrequency(16000).withAudioCodec('flac').format("flac").save(outputFile);
ffmpeg(realName)
.toFormat('flac')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished !');
})
.save(newName);//path where you want to save your file
SpeechToText(newName);
}
然后我想把这个文件传给google演讲api。但是后来我收到找不到文件的错误
这是演讲的代码 Api:
function SpeechToText(path) {
// The name of the audio file to transcribe
const fileName = path;
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'FLAC',
languageCode: 'de-DE',
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
})
.catch(err => {
console.error('ERROR:', err);
});
}
问题是,如果我上传文件,一切正常。但是,如果我第二次尝试,就会出现错误:
fs.js:646
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open
'C:\Users\paulu\desktop\API\output\sample.flac'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at SpeechToText (C:\Users\paulu\desktop\API\server.js:68:21)
at convert (C:\Users\paulu\desktop\API\server.js:121:5)
at C:\Users\paulu\desktop\API\server.js:50:17
at doMove (C:\Users\paulu\desktop\API\node_modules\express-
fileupload\lib\index.js:152:17)
at WriteStream.<anonymous> (C:\Users\paulu\desktop\API\node_modules\express-
fileupload\lib\in dex.js:182:15)
at emitNone (events.js:106:13)
at WriteStream.emit (events.js:208:7)
at fs.close (fs.js:2094:12)
谢谢大家的回答!
我修好了。
问题是ffmpeg进程没有完成。解决方法在这里:
function convert(filename) {
var realName = "./upload/" + filename;
var newName = "./output/" + filename.substr(0, filename.length - 4) + ".flac";
ffmpeg(realName)
.toFormat('flac')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished !');
SpeechToText(newName); //Just move this statement to here !
})
.save(newName);//path where you want to save your file
}
我正在 node.js 中对服务器进行编程以处理音频文件。 我用这个代码保存文件:
app.use(upload());
app.get("/", function (req, res) {
res.sendFile("index.html")
});
//Uploaded files will be saved
app.post("/", function (req, res) {
if (req.files) {
var file = req.files.filename,
filename = file.name;
file.mv("./upload/" + filename, function (err) {
if (err) {
console.log("err");
}
else {
res.send("Done");
console.log(filename);
convert(filename);
}
})
}
})
我将文件保存在我的上传目录中。一切都很好,但现在问题来了。 我用 ffmpeg
转换文件function convert(filename) {
var realName = "./upload/" + filename;
var newName = "./output/" + filename.substr(0, filename.length - 4) + ".flac";
ffmpegN = ffmpeg(realName);
ffmpegN.audioBitrate(16).audioFrequency(16000).withAudioCodec('flac').format("flac").save(outputFile);
ffmpeg(realName)
.toFormat('flac')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished !');
})
.save(newName);//path where you want to save your file
SpeechToText(newName);
}
然后我想把这个文件传给google演讲api。但是后来我收到找不到文件的错误 这是演讲的代码 Api:
function SpeechToText(path) {
// The name of the audio file to transcribe
const fileName = path;
// Reads a local audio file and converts it to base64
const file = fs.readFileSync(fileName);
const audioBytes = file.toString('base64');
// The audio file's encoding, sample rate in hertz, and BCP-47 language code
const audio = {
content: audioBytes,
};
const config = {
encoding: 'FLAC',
languageCode: 'de-DE',
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
client
.recognize(request)
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
})
.catch(err => {
console.error('ERROR:', err);
});
}
问题是,如果我上传文件,一切正常。但是,如果我第二次尝试,就会出现错误:
fs.js:646
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open
'C:\Users\paulu\desktop\API\output\sample.flac'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at SpeechToText (C:\Users\paulu\desktop\API\server.js:68:21)
at convert (C:\Users\paulu\desktop\API\server.js:121:5)
at C:\Users\paulu\desktop\API\server.js:50:17
at doMove (C:\Users\paulu\desktop\API\node_modules\express-
fileupload\lib\index.js:152:17)
at WriteStream.<anonymous> (C:\Users\paulu\desktop\API\node_modules\express-
fileupload\lib\in dex.js:182:15)
at emitNone (events.js:106:13)
at WriteStream.emit (events.js:208:7)
at fs.close (fs.js:2094:12)
谢谢大家的回答!
我修好了。
问题是ffmpeg进程没有完成。解决方法在这里:
function convert(filename) {
var realName = "./upload/" + filename;
var newName = "./output/" + filename.substr(0, filename.length - 4) + ".flac";
ffmpeg(realName)
.toFormat('flac')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished !');
SpeechToText(newName); //Just move this statement to here !
})
.save(newName);//path where you want to save your file
}