【发布时间】:2020-03-27 08:06:15
【问题描述】:
我尝试通过 Google Cloud Function 处理视频文件(存储在 Google Firebase 存储中)。我有将整个视频文件下载到 NodeJS Google 云函数中的工作代码:await bucket.file(filePath).download({ destination: tempFile })。
但目标只是读取帧速率,因此视频文件的标题就足够了。但是createReadStream 给了我一个空的临时文件。任何建议都非常感谢!
exports.checkFramerate = functions.region('europe-west1').storage.object().onFinalize(async (object, context) => {
const bucket = admin.storage().bucket(object.bucket); // Bucket class
const filePath = object.name; // videos/xbEXdMNFb1Blbd9r2E8m/comp_test.mp4
const fileName = filePath.split('/').pop(); // comp_test.mp4
const bucketDir = path.dirname(filePath); // videos/xbEXdMNFb1Blbd9r2E8m
const tempFile = path.join(os.tmpdir(), 'temp.mp4')
fs.closeSync(fs.openSync(tempFile, 'w'))
console.log("tempFile size1", fs.statSync(tempFile).size)
// await bucket.file(filePath).download({ destination: tempFile }); // this works: tempFile size2 = 3180152
await bucket.file(filePath).createReadStream({ // this does not work: tempFile size2 = 0
start: 10000,
end: 20000
})
.on('error', function(err) {console.log(err)})
.pipe(fs.createWriteStream(tempFile));
console.log("tempFile size2", fs.statSync(tempFile).size)
mi(tempFile).then(data => {
console.log("frameRate", data[0].general.frame_rate[0])
return data[0].general.frame_rate[0];
}).catch(e => {console.error(e)});
});
我什至尝试实现https://googleapis.dev/nodejs/storage/latest/File.html#createReadStream 的示例,但无济于事。 remoteFile.download 效果很好,但 remoteFile.createReadStream 给了我空文件...
const remoteFile = bucket.file(filePath);
const localFilename = tempFile;
remoteFile.createReadStream()
.on('error', function(err) {})
.on('response', function(response) {})
.on('end', function() {})
.pipe(fs.createWriteStream(localFilename));
fs.stat(localFilename, (err, stats) => {
if (err) {console.log(err)}
return console.log("stats async",stats.size)
})
【问题讨论】:
标签: javascript node.js google-cloud-functions google-cloud-storage