我正在尝试使用节点模块vtt2srt将VTT字符串转换为SRT文件并保存输出。它只能工作一次,并且我的字幕可以正确保存,但是如果我碰到了端点,第二次节点将崩溃,并显示以下错误:
Error: write after end
at writeAfterEnd
我尝试了.close .on('close')的所有组合
我从前端发送了唯一的vid和VTT数据
router.post('/downloadsubs', function(req,res,next) {
var vttObj = webvtt.compile(req.body.data);
fs.unlink(__dirname + '/../static/videos/'+req.body.vid+'/subtitles.srt', function(){
srtStream.write(vttObj);
var writestream = fs.createWriteStream(__dirname + '/../static/videos/'+req.body.vid+'/subtitles.srt')
srtStream.end()
srtStream.pipe(writestream)
res.send(req.body.vid);
})
})
答案 0 :(得分:0)
我弄清楚我的问题所在,希望有一天对其他人有用。
以前,我需要在路由器文件的开头添加模块:
const vtt2srt = require('node-vtt-to-srt');
const srtStream = vtt2srt();
router.post('/downloadsubs', function(req,res,next) {
var vttObj = webvtt.compile(req.body.data);
fs.unlink(__dirname + '/../static/videos/'+req.body.vid+'/subtitles.srt', function(){
srtStream.write(vttObj);
srtStream.end()
var writestream = fs.createWriteStream(__dirname + '/../static/videos/'+req.body.vid+'/subtitles.srt');
srtStream.pipe(writestream)
writestream.on('finish', function () { res.send(req.body.vid) });
})
})
现在,我在路由器方法中创建了一个新的srtStream:
const vtt2srt = require('node-vtt-to-srt');
router.post('/downloadsubs', function(req,res,next) {
var srtStream = vtt2srt();
var vttObj = webvtt.compile(req.body.data);
fs.unlink(__dirname + '/../static/videos/'+req.body.vid+'/subtitles.srt', function(){
srtStream.write(vttObj);
srtStream.end()
var writestream = fs.createWriteStream(__dirname + '/../static/videos/'+req.body.vid+'/subtitles.srt');
srtStream.pipe(writestream)
writestream.on('finish', function () { res.send(req.body.vid) });
})
})
它有效。