【问题标题】:How to debug ffmpeg-static in a AWS Lambda layer如何在 AWS Lambda 层中调试 ffmpeg-static
【发布时间】:2019-08-08 19:49:22
【问题描述】:

我有一个包含 nodejs ffmpeg-static 的 AWS Lambda 层。调用“ffmpeg.path”将返回该层中ffmpeg可执行文件的正确位置。

但是对 ffmpeg 的任何调用都会静默停止,让我无法知道导致错误的原因。这是我的测试功能:

const exec = require( "child_process" ).exec
const ffmpeg = require( "ffmpeg-static" )
exports.handler = async (event, context, callback ) => {
    console.log( ffmpeg.path ) // Outputs: "/opt/nodejs/node_modules/ffmpeg-static/bin/linux/x64/ffmpeg"
    exec( ffmpeg.path + " -version",
        function( error, stdout, stderr ) {
            console.log( stdout ) // Nothing
            console.log( stderr ) // Nothing
            if ( error ) {
                console.log( error ) // Nothing
            }
        }
    )

永远不会触发 exec() 回调。如何识别问题?

【问题讨论】:

  • 尝试用外壳包裹 exec,它可能会提供一些线索。所以像exec("sh -c \""+ffmpeg.path+" -version\"")
  • @MattiasWadman,谢谢你的建议......仍然默默地失败。

标签: node.js ffmpeg aws-lambda


【解决方案1】:

我找到了更多信息here 所以我的解决方案是这样结束的:

const childProcess = require('child_process');

/*
 * Handle the chile process and returns a Promise
 * that resoved when process finishes executing
 * 
 * The Promise resolves an  exit_code
 */ 
async function handleProcess(process: any) {
  return new Promise((resolve, reject) => {
    let dataObj: string[] = [];
    process.stdout.on("data", (data: any) => {
      console.log(`OUT_stdout: ${data}`);
      dataObj.push(data.toString());
    });

    process.stderr.on("data", (data: any) => {
      console.log(`ERR_stderr: ${data}`);
      dataObj.push(data.toString());
    });

    process.on("close", (code: any) => {
      console.log(`child process exited with code ${code}`);
      if (code === 0) {
        resolve({ code, data: dataObj });
      } else {
        reject({ code, data: dataObj });
      }
    });
  });
}



exports.handler = async (event, context, callback) => {

    /* be aware that the path to your ffmpeg binary depends on how you uploaded your layer. 
    *  My layer was a .zip with dir bin, and in the dir bin the binary file ffmpeg */
    return await handleProcess(
        childProcess.spawn("/opt/bin/ffmpeg", ["--help"])
      )
        .then((resp: any) => {
          console.log(`exit_code = ${resp.code}`);
          let response = {
            statusCode: 0 == resp.code ? 200 : 500,
            body: JSON.stringify(resp.data),
          };
          console.log("response:::", response);
          return response;
        })

        .catch((error) => {
          console.error(error);
          let response = {
            statusCode: 500,
            body: error,
          };
          console.log("catch-error-response:::", response);
          return {};
        });

}

因此它将进程运行为异步/等待产生命令/opt/bin/ffmpeg --help

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 2018-10-03
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多