我正在使用Google云端功能和存储桶触发器:
如果文件小于5MB,但在文件永远不会上传到另一个存储桶之后,一切都很好。我试图将它上传到我收到视频的同一个存储桶上,但没有用。
我检查了google storage nodejs代码,代码在上传5Mb后自动执行流式处理,但它永远不会结束。
有人已经有这个问题吗?
我的代码正在执行:
// Imports the Google Cloud client library
const storage = require('@google-cloud/storage')();
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path
const ffmpeg = require('fluent-ffmpeg');
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf'); // rimraf directly
// Binaries for ffmpeg
ffmpeg.setFfmpegPath(ffmpegPath);
/**
* Triggered from a message on a Cloud Storage bucket.
*
* @param {!Object} event The Cloud Functions event.
* @param {!Function} The callback function.
*/
exports.processFile = function(event, callback) {
console.log(JSON.stringify(event));
console.log('Processing file: ' + event.data.name);
const bucket = storage.bucket(event.data.bucket);
const bucketAudio = storage.bucket('distant-bucket');
const videoFile = bucket.file(event.data.name);
const destination = '/tmp/' + path.basename( event.data.name );
return new Promise((resolve, reject) => { return videoFile.download({
destination: destination
}).then((error) => {
if (error.length > 0) {
reject(error);
} else {
resolve(destination);
}
})
})
.then((fileinfo) => {
const filename = destination.replace( '.mp4', '.flac' );
return new Promise((resolve, reject) => {
ffmpeg(fileinfo)
.videoBitrate(19200)
.inputOptions('-vn')
.format('flac')
.audioChannels(1)
.audioBitrate(44100)
.output(filename)
.on('end', function() {
console.log('extracted audio : '+event.data.name+" to "+filename);
resolve(filename);
})
.on('error', function(err, stdout, stderr) {
reject(err);
})
.run()
});
})
.then((flacOutput) => {
console.log('save file '+flacOutput);
const stats = fs.statSync(flacOutput)
console.log(stats.size / 1000000.0);
// TODO: upload FLAC file to Cloud Storage
return bucket
.upload( flacOutput ).catch((err) => {
console.error('Failed to upload flac.', err);
return Promise.reject(err);
});
}).then(() => {
console.log(`Audio uploaded.`);
// Delete the temporary file.
return new Promise((resolve, reject) => {
fs.unlink(destination);
fs.unlink(destination.replace( '.mp4', '.flac' ), (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
};