Я пытаюсь использовать облачную функцию Google для перекодирования видео из хранилища и вывода в другое хранилище с помощью кода, размещенного в сети, но с некоторыми изменениями.
но я получаю следующую ошибку:
TypeError: Невозможно прочитать свойство 'name' из undefined в transcodeVideo
(/srv/index.js:17:56) по адресу /worker/worker.js:825:24 по адресу
process._tickDomainCallback (внутренняя / 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
};