如何将视频文件作为输入传递给 NodeJS 中的子进程并接收帧/图像作为输出?
Posted
技术标签:
【中文标题】如何将视频文件作为输入传递给 NodeJS 中的子进程并接收帧/图像作为输出?【英文标题】:How to pass a video file as input to a child process in NodeJS and receive frames/images as output? 【发布时间】:2016-10-16 19:23:57 【问题描述】:我在 OpenCV C++ 中编写了一个代码,该代码从命令行获取视频和时间戳(采用 HH:MM:SS 格式)作为输入,并检测该帧中的人脸。检测到的人脸被裁剪并写入 PNG 文件。
$ ./executable video.mp4 00:00:01
现在我想在 NodeJS 中为 executable
构建一个 Web API。我使用了child_process.spawn
和命令行参数。该应用程序运行没有任何错误,但我没有得到裁剪的图像作为输出。看来我的应用程序要么无法将输入视频传递给executable
,要么无法从中读取输出。这是我的第一个 NodeJS 应用程序。这是我的代码。
/**
* app.js
* make sure to install express (npm install express)
* and place the executable beside this file
* or set executablePath to the correct path.
* run the app and open 'http://0.0.0.0:3000' in your browser.
*/
var child_process = require('child_process');
var path = require('path');
var express = require('express');
var app = express();
var executable = null;
app.get('/', function (req, res)
const args = process.argv;
console.log(args);
//console.log(process.argv[3]);
if (process.argv.length <= 3)
console.log("Usage: nodemon " + __filename + " <path/to/video> <HH:MM:SS>");
process.exit(-1);
if (executable !== null)
res.end('executable is already running.');
return;
var executablePath = path.join(__dirname, './executable');
executable = child_process.spawn(executablePath, [process.argv[2], process.argv[3]]);
executable.on('exit', function (signal, code)
console.log('executable exited with signal: %s and code: %d', signal, code);
executable = null;
);
res.end('Done.');
);
app.listen(3000);
console.log('Listening on port 3000');
我正在运行它
$ nodemon app.js video.mp4 00:00:01
我还需要知道如何从 API 本身传递参数 video.mp4
和 00:00:01
。
【问题讨论】:
【参考方案1】:我自己想出了解决方案。我需要在生成子进程时添加stdio: 'inherit'
选项。这是最终代码:
/**
* app.js
* make sure to install express (npm install express)
* and place the executable beside this file
* or set executablePath to the correct path.
* run the app and open 'localhost:3000/?vid=<path to video file>&tstamp=<timestamp in HH:MM:SS format>' in your browser.
*/
var child_process = require('child_process');
var path = require('path');
var express = require('express');
var app = express();
var fs = require('fs');
var executable = null;
app.get('/', function (req, res)
if (executable !== null)
res.end('executable is already running.');
return;
var video = req.param('vid');
var timestamp = req.param('tstamp');
var executablePath = path.join(__dirname, './executable2');
executable = child_process.spawn(executablePath, [video, timestamp], stdio: 'inherit');
executable.on('exit', function (signal, code)
console.log('executable exited with signal: %s and code: %d', signal, code);
executable = null;
);
res.end('Done.');
);
app.listen(3000);
console.log('Listening on port 3000');
之后,运行应用程序
$ nodemon app.js
并在浏览器中打开以下链接
localhost:3000/?vid=<path to video file>&tstamp=<timestamp in HH:MM:SS format>
它从url本身获取视频文件和时间戳,运行应用时无需从终端提供这些参数。
【讨论】:
以上是关于如何将视频文件作为输入传递给 NodeJS 中的子进程并接收帧/图像作为输出?的主要内容,如果未能解决你的问题,请参考以下文章
使用 Typescript(TSX 文件)将道具传递给 React 中的子组件