0

I have an AWS Lambda layer containing nodejs ffmpeg-static. Calling "ffmpeg.path" will return the correct location of the ffmpeg executable in the layer.

But any call to ffmpeg will stop silently, making it impossible for me to know what caused the error. Here is my test function:

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
            }
        }
    )

The exec() callback is never triggered. How can I identify the problem ?

standac
  • 1,027
  • 1
  • 11
  • 26

1 Answers1

0

I found more information here So My solution ended being like this:

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 {};
        });

}

So it handles running the process as async/await spawning the command /opt/bin/ffmpeg --help.

vencedor
  • 663
  • 7
  • 9