【发布时间】:2017-10-26 08:16:20
【问题描述】:
我有一个 firebase 函数,它将从前端接收带有文件名的请求,该文件将是存储在 firebase 存储中的视频,然后我将应用 ffmpeg 并将视频提取到多个帧中。最后,我会将所有帧上传到 Firebase 存储中。 一切正常,我能够获得所有帧。但是,上传帧存在问题。有时我可以成功上传所有帧,但函数会一直运行直到超时,有时我只能上传第一帧。我是 node.js 的新手。我想 return 或 promise 有问题(我不明白要返回什么以及如何处理 promise)。 另外,我想将每一帧的数据写入数据库。我应该把这部分代码放在哪里?
exports.extractFrame = functions.https.onRequest(function (req, res) {
const name = req.query.fileName;
const username = name.substr(0, name.length - 4);
const sessionId = 'video-org';
const framePath = 'frame-org';
const sourceBucketName = 'this is my bucket name';
const sourceBucket = gcs.bucket(sourceBucketName);
const temDir = os.tmpdir();
return sourceBucket.file(sessionId + '/' + name).download({
destination: temDir + '/' + name
}
).then(() => {
console.log('extract frames');
return spawn(ffmpegPath, ['-i', temDir + '/' + name, temDir + '/' +
username + '%d.png']);
}).then(() => {
const frames = fs.readdirSync(temDir);
console.log(frames);
for (let index in frames) {
if (index != 0) {
console.log('uploading');
sourceBucket.upload(temDir + '/' + frames[index], {destination:
framePath + '/' + frames[index]});
}
}
}).then(() => {
res.send('I am done');
});
});
非常感谢您的帮助!
【问题讨论】:
标签: node.js firebase google-cloud-storage firebase-storage google-cloud-functions