【发布时间】:2019-06-09 07:19:33
【问题描述】:
我正在尝试使用 Google 云功能对存储桶中的视频进行转码,并使用在线发布的代码输出到另一个桶中,但进行了一些调整。
但我收到以下错误:
TypeError:无法在 transcodeVideo 读取未定义的属性“名称” (/srv/index.js:17:56) 在 /worker/worker.js:825:24 在 process._tickDomainCallback (internal/process/next_tick.js:229:7)
Index.js
const {Storage} = require('@google-cloud/storage');
const projectId = 'cc18-223318';
const storage = new Storage({
projectId: projectId,
});
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
const transcodedBucket = storage.bucket('2400p');
const uploadBucket = storage.bucket('inpuut');
ffmpeg.setFfmpegPath(ffmpegPath);
exports.transcodeVideo = function transcodeVideo(event, callback) {
const file = event.data;
// Ensure that you only proceed if the file is newly created, and exists.
if (file.metageneration !== '1' || file.resourceState !== 'exists') {
callback();
return;
}
// Open write stream to new bucket, modify the filename as needed.
const remoteWriteStream = transcodedBucket.file(file.name.replace('.webm', '.mp4'))
.createWriteStream({
metadata: {
metadata: file.metadata, // You may not need this, my uploads have associated metadata
contentType: 'video/mp4', // This could be whatever else you are transcoding to
},
});
// Open read stream to our uploaded file
const remoteReadStream = uploadBucket.file(file.name).createReadStream();
// Transcode
ffmpeg()
.input(remoteReadStream)
.outputOptions('-c:v libx264') // Change these options to whatever suits your needs
.outputOptions('-c:a copy')
.outputOptions('-vf "scale=4800:2400"')
.outputOptions('-b:a 160k')
.outputOptions('-b:a 160k')
.outputOptions('-x264-params mvrange=511')
.outputOptions('-f mp4')
.outputOptions('-preset slow')
.outputOptions('-movflags frag_keyframe+empty_moov')
.outputOptions('-crf 18')
// https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/346#issuecomment-67299526
.on('start', (cmdLine) => {
console.log('Started ffmpeg with command:', cmdLine);
})
.on('end', () => {
console.log('Successfully re-encoded video.');
callback();
})
.on('error', (err, stdout, stderr) => {
console.error('An error occured during encoding', err.message);
console.error('stdout:', stdout);
console.error('stderr:', stderr);
callback(err);
})
.pipe(remoteWriteStream, { end: true }); // end: true, emit end event when readable stream ends
};
【问题讨论】:
-
你可以尝试删除行“const projectId = 'cc18-223318';”只留下“const storage = new Storage();”,因为 projectId 是可选字段?
-
还是同样的错误
-
然后检查“transcodedBucket”和“uploadBucket”的名称。应创建“transcodedBucket”,“uploadBucket”应与触发的存储桶相同。
-
一切都应该是正确触发但执行中断并出现上述错误
标签: javascript ffmpeg google-cloud-platform google-cloud-functions google-cloud-storage