【问题标题】:Unable to use Sox commands in Google cloud functions via child_process无法通过 child_process 在 Google 云功能中使用 Sox 命令
【发布时间】:2018-05-20 12:11:02
【问题描述】:

我正忙于编写一个由谷歌云存储活动激活的谷歌云功能。我需要在 child_process 中使用 sox - 但没有太大成功。

这个函数获取音频文件/事件,然后需要计算文件的长度,然后显示。

当我运行以下代码时(在我的 index.js 中):

'use strict';

const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const storage = require('@google-cloud/storage')();
const vision = require('@google-cloud/vision').v1p1beta1;

const client = new vision.ImageAnnotatorClient();


exports.processAudiofile = (event) => {
  const object = event.data;

  if (object.resourceState === 'not_exists') {
    console.log('This is a deletion event.');
    return;
  } else if (!object.name) {
    console.log('This is a deploy event.');
    return;
  }

  const file = storage.bucket(object.bucket).file(object.name);
  const filePath = `gs://${object.bucket}/${object.name}`;

  console.log(`Analyzing ${file.name}.`);
  console.log(`Filepath is ${filePath}.`);

  return new Promise((resolve, reject) => {
    exec(`sox --i -d ${filePath}`, function(err, stdout){
        if (err){
             throw err;
    }
    console.log("Duration:" + stdout);//Prints
    });
  });

};

使用以下 package.json:

{
  "name": "audiotest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@google-cloud/language": "^1.2.0",
    "@google-cloud/speech": "^1.5.0",
    "@google-cloud/storage": "^1.6.0",
    "@google-cloud/vision": "^0.19.0",
    "child_process": "^1.0.2",
    "fs": "0.0.1-security",
    "got": "^8.3.1",
    "path": "^0.12.7",
    "sox": "^0.1.0"
  }
}

我收到以下错误:

Error: Command failed: sox --i -d gs://rawaudiobucket/shortcall.wav 
/bin/sh: 1: sox: not found at ChildProcess.exithandler 
(child_process.js:199:12) at emitTwo (events.js:106:13) at 
ChildProcess.emit (events.js:191:7) at maybeClose 
(internal/child_process.js:920:16) at Socket.<anonymous> 
(internal/child_process.js:351:11) at emitOne (events.js:96:13) at 
Socket.emit (events.js:188:7) at Pipe._handle.close [as _onclose] 
(net.js:509:12)

对此的任何帮助将不胜感激!

【问题讨论】:

    标签: google-cloud-functions child-process sox


    【解决方案1】:

    您似乎假设 sox 在运行 Cloud Functions 的实例中可作为命令行程序使用。除非当前已经记录了该特定程序(例如 ImageMagic convert),否则您不应该依赖它。

    请注意,npm page for the sox module 表示:

    需要安装 sox CLI。这可以通过大多数 linux 发行版的包管理器安装。

    如果您提供自己的 Linux 编译二进制文件,则可以直接执行它。通过节点模块构建和运行它将是它自己的问题。

    【讨论】:

    • 感谢道格的反馈!所以,如果我理解正确 - 因为 sox 似乎不起作用 - 这意味着触发函数时创建的 vm 不包含 sox?简而言之 - 我不能在谷歌功能上使用 sox 吗?如果我仍然想使用谷歌功能提供的优雅和无服务器选项,解决方法是什么?
    • 关于此的另一个问题:我在 package.json 文件中指定 sox 的事实是否默认情况下不会在为该函数创建的 vm 上安装模块?
    • 如果您有与第一个不同的后续问题,请单独提出。
    • @JanCN npm 安装的包是节点包,可能依赖系统二进制文件才能正常运行(它们只是包装器),所以你仍然需要事先由谷歌安装二进制文件,你可以'不要自己做这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多