h264-live-player代码学习与注解(三):raspivid.js
"use strict";
const util = require('util');
const spawn = require('child_process').spawn;
const merge = require('mout/object/merge');
const Server = require('./_server');
class RpiServer extends Server {
constructor(server, opts) {
super(server, merge({
fps : 12,
}, opts));
}
get_feed() {
var msk = "raspivid -t 0 -o - -w %d -h %d -fps %d";
var cmd = util.format(msk, this.options.width, this.options.height, this.options.fps);
console.log(cmd);
var streamer = spawn('raspivid', ['-t', '0', '-o', '-', '-w', this.options.width, '-h', this.options.height, '-fps', this.options.fps, '-pf', 'baseline']);
streamer.on("exit", function(code){
console.log("Failure", code);
});
return streamer.stdout;
}
};
module.exports = RpiServer;
1.child_process
在node中,child_process这个模块非常重要。掌握了它,等于在node的世界开启了一扇新的大门。熟悉shell脚本的同学,可以用它来完成很多有意思的事情,比如文件压缩、增量部署等。
个人理解:就是使用node打开shell执行操作,然后可以重定向输入输出这些
var spawn = require('child_process').spawn;
var ls = spawn('ls', ['-al']);
ls.stdout.on('data', function(data){
console.log('data from child: ' + data);
});
ls.stderr.on('data', function(data){
console.log('error from child: ' + data);
});
ls.on('close', function(code){
console.log('child exists with code: ' + code);
});
2.js.merge
NodeJS合并用于将多个对象合并为一个对象
举个例子:
console.log(merge({one:'hello'}, {two: 'world'}));
// -> {"one":"hello","two":"world"}
3.module.exports = RpiServer;
module.exports 对象是由模块系统创建的。在我们自己写模块的时候,需要在模块最后写好模块接口,声明这个模块对外暴露什么内容,module.exports 提供了暴露接口的方法。
4.raspivid的参数
项目 | Value |
---|---|
-hf | 垂直翻转图像 |
-vf | 水平翻转图像 |
-t 1000 | 录制时间为1000ms |
-t 0 | 一直录制 |
手机 | $12 |
导管 | $1 |
参数有待拷贝,补充