【问题标题】:google function: TypeError: Cannot read property 'name' of undefined谷歌函数:TypeError:无法读取未定义的属性“名称”
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 好吧,我按照你说的替换了过时的代码,现在函数在 5ms 内执行“ok”,没有输出文件,所以我尝试删除检查文件是否是新创建的代码行 13现在,当我上传一个新文件时,该函数以“使用命令启动 ffmpeg:..”开始,60 秒后给出超时“函数执行耗时 60002 毫秒,以状态完成:'超时'”
  • @Juggernaught 在这种情况下,请尝试增加超时 [1],因为函数比默认的 1 分钟值更有效。 [1]:cloud.google.com/functions/docs/concepts/exec#timeout
  • 让它工作直到遇到另一个问题“超出内存限制”,因为事实证明谷歌函数的限制是 2GB,这不足以转换 5K 视频加上函数的最大执行时间是9 分钟也不够,所以我想这对这个功能来说是一个死胡同。无论如何都非常感谢:)
  • @Juggernaught 没问题,你可以点赞或者采纳我的回答,帮助其他会员找到正确的答案
猜你喜欢
  • 2021-11-13
  • 2022-01-14
  • 2022-06-22
  • 2015-10-16
  • 2021-12-28
  • 2021-08-16
  • 2020-10-11
  • 2019-09-06
  • 1970-01-01
相关资源
最近更新 更多