我有一个包含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()回调。如何识别问题?
答案 0 :(得分:0)
我找到了更多信息here 所以我的解决方案最终是这样的:
#include <stdio.h>
#include <stdlib.h>
int compare_ints(const void* a, const void* b)
{
int arg1 = *(const int*)a;
int arg2 = *(const int*)b;
if (arg1 < arg2) return -1;
if (arg1 > arg2) return 1;
return 0;
}
int count_sum (int* A, int n, int target) {
int count = 0;
qsort(A, n, sizeof(int), compare_ints);
int left = 0;
int right = n-1;
while (left < right) {
int sum = A[left] + A[right];
if (sum == target) {
count++;
left++;
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
return count;
}
int main() {
int A[] = {8, 2, 7, 5, 3, 1};
int target = 10;
int n = sizeof(A)/sizeof(A[0]);
int ans = count_sum (A, n, target);
printf ("count = %d\n", ans);
return 0;
}
因此它将进程作为异步/等待生成命令 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 {};
});
}
来处理。