Google函数:TypeError:无法读取未定义的属性“名称”

时间:2019-06-09 07:19:33

标签: javascript ffmpeg google-cloud-platform google-cloud-functions google-cloud-storage

我正在尝试使用Google云功能对存储分区中的视频进行转码,并使用在线发布的代码进行一些调整后再输出到另一个存储分区中。

但出现以下错误:

  

TypeError:无法读取转码视频中未定义的属性“名称”   (/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
};

1 个答案:

答案 0 :(得分:0)

函数transcodeVideo的问题是使用参数(事件,回调)来获取文件数据。根据此document,这些参数(事件和回调)用于Node.js 6,对于Node.js 8/10,则应使用这些参数(数据和上下文)。请使用命令node -v检查Node.js版本,或仅在控制台的“云功能”部分中检查它。 解决方案是安装旧版本的Node.js 6或编辑与Node.js 8/10兼容的代码。