Node.js NodeJs-Fluent FFMPEG找不到firebase云函数的FFMPEG

Node.js NodeJs-Fluent FFMPEG找不到firebase云函数的FFMPEG,node.js,firebase,firebase-realtime-database,google-cloud-functions,fluent-ffmpeg,Node.js,Firebase,Firebase Realtime Database,Google Cloud Functions,Fluent Ffmpeg,我试图学习如何使用ffmpeg-fluent编写firebase云函数,并参考此示例。我复制了代码,只是将gcs的初始化更改为const gcs=admin.storage()。节点10上的部署成功,但在上载mp3文件以测试功能时,出现以下错误 Error: Cannot find ffmpeg at /srv/node_modules/fluent-ffmpeg/lib/processor.js:136:22 at FfmpegCommand.proto._getFfmpegPath (/s

我试图学习如何使用
ffmpeg-fluent
编写firebase云函数,并参考此示例。我复制了代码,只是将
gcs
的初始化更改为
const gcs=admin.storage()。节点10上的部署成功,但在上载
mp3
文件以测试功能时,出现以下错误

Error: Cannot find ffmpeg at /srv/node_modules/fluent-ffmpeg/lib/processor.js:136:22 
at FfmpegCommand.proto._getFfmpegPath (/srv/node_modules/fluent-ffmpeg/lib/capabilities.js:90:14) 
at FfmpegCommand.proto._spawnFfmpeg (/srv/node_modules/fluent-ffmpeg/lib/processor.js:132:10) 

at FfmpegCommand.proto.availableFormats.proto.getAvailableFormats (/srv/node_modules/fluent-ffmpeg/lib/capabilities.js:517:10) 
at /srv/node_modules/fluent-ffmpeg/lib/capabilities.js:568:14 
at nextTask (/srv/node_modules/async/dist/async.js:4578:27) 
at Object.waterfall (/srv/node_modules/async/dist/async.js:4589:9) 
at Object.awaitable(waterfall) [as waterfall] (/srv/node_modules/async/dist/async.js:208:32) 
at FfmpegCommand.proto._checkCapabilities (/srv/node_modules/fluent-ffmpeg/lib/capabilities.js:565:11) 
at /srv/node_modules/fluent-ffmpeg/lib/processor.js:298:14
有人能告诉我我遗漏了哪些安装步骤吗

以下是先前存储库中的代码段

index.js

const functions = require('firebase-functions');
const gcs = require('@google-cloud/storage')();
const path = require('path');
const os = require('os');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');

// Makes an ffmpeg command return a promise.
function promisifyCommand(command) {
  return new Promise((resolve, reject) => {
    command.on('end', resolve).on('error', reject).run();
  });
}

/**
 * When an audio is uploaded in the Storage bucket We generate a mono channel audio automatically using
 * node-fluent-ffmpeg.
 */
exports.generateMonoAudio = functions.storage.object().onFinalize(async (object) => {
  const fileBucket = object.bucket; // The Storage bucket that contains the file.
  const filePath = object.name; // File path in the bucket.
  const contentType = object.contentType; // File content type.

  // Exit if this is triggered on a file that is not an audio.
  if (!contentType.startsWith('audio/')) {
    console.log('This is not an audio.');
    return null;
  }

  // Get the file name.
  const fileName = path.basename(filePath);
  // Exit if the audio is already converted.
  if (fileName.endsWith('_output.flac')) {
    console.log('Already a converted audio.');
    return null;
  }

  // Download file from bucket.
  const bucket = gcs.bucket(fileBucket);
  const tempFilePath = path.join(os.tmpdir(), fileName);
  // We add a '_output.flac' suffix to target audio file name. That's where we'll upload the converted audio.
  const targetTempFileName = fileName.replace(/\.[^/.]+$/, '') + '_output.flac';
  const targetTempFilePath = path.join(os.tmpdir(), targetTempFileName);
  const targetStorageFilePath = path.join(path.dirname(filePath), targetTempFileName);

  await bucket.file(filePath).download({destination: tempFilePath});
  console.log('Audio downloaded locally to', tempFilePath);
  // Convert the audio to mono channel using FFMPEG.

  let command = ffmpeg(tempFilePath)
      .setFfmpegPath(ffmpeg_static.path)
      .audioChannels(1)
      .audioFrequency(16000)
      .format('flac')
      .output(targetTempFilePath);

  await promisifyCommand(command);
  console.log('Output audio created at', targetTempFilePath);
  // Uploading the audio.
  await bucket.upload(targetTempFilePath, {destination: targetStorageFilePath});
  console.log('Output audio uploaded to', targetStorageFilePath);

  // Once the audio has been uploaded delete the local file to free up disk space.
  fs.unlinkSync(tempFilePath);
  fs.unlinkSync(targetTempFilePath);

  return console.log('Temporary files removed.', targetTempFilePath);
});
package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "@google-cloud/storage": "^5.0.1",
    "child-process-promise": "^2.2.1",
    "ffmpeg-static": "^4.2.5",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1",
    "fluent-ffmpeg": "^2.1.2",
    "fs-extra": "^8.1.0",
    "sharp": "^0.25.3"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

此处未定义
ffmpeg\u static.path
路径

let command = ffmpeg(tempFilePath)
      .setFfmpegPath(ffmpeg_static.path)
      .audioChannels(1)
      .audioFrequency(16000)
      .format('flac')
      .output(targetTempFilePath);
您应该改为安装“ffmpeg安装程序/ffmpeg”。你可以在这里找到它:

然后设置正确的路径,如:

const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');

let command = ffmpeg(tempFilePath)
      .setFfmpegPath(ffmpegPath)
      .audioChannels(1)
      .audioFrequency(16000)
      .format('flac')
      .output(targetTempFilePath);

您是否也可以添加
package.json
?你使用的是与你链接的存储库中相同的吗?我已经用我自己的
包更新了帖子。json
并且它成功部署,如果你需要更多信息,请告诉我导入
ffmpeg
库时似乎有错误。我建议只使用导入和构造函数
var command=ffmpeg(),部署函数代码的最小简化版本以缩小问题范围。我已尝试减少代码以缩小问题范围,我同意问题发生在导入中。日志仅在我尝试上载将调用云函数的文件时显示错误。由于此代码段已公开了相当长的时间,我希望成功导入此代码的人与我分享他们的经验,因为我相信我密切关注文档。对于我来说,使用
ffmpeg\u static
而不使用
。path
有效,例如:
ffmpeg(“filepath”)。setFfmpegPath(ffmpeg\u static)
,而不是
ffmpeg(“文件路径”)。setFfmpegPath(ffmpeg\u static.path)