-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontor_index.js
More file actions
113 lines (101 loc) · 3.47 KB
/
Copy pathcontor_index.js
File metadata and controls
113 lines (101 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var request = require('request');
var cmd=require('node-cmd');
var fetch_publish_url = 'http://demo.xinqigu.com/api/live/create';
// var fetch_publish_url = 'http://192.168.1.183/api/live/create';
function sleep(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
};
var headers = {
'Accept': 'application/x.edu.v1.0.1+json'
}
var devices = [
'USB2.0 PC CAMERA'
]
var form = {
name: '测试实验室2号',
uuid: 'windows_00000001',
type: '2'
};
// 创建直播流
var start = function(fetch_publish_url, form, timeout, filePath){
return new Promise(function(resolve, reject){
request.post(fetch_publish_url, {
headers: headers,
form: form
}, function(err, response, body){
if(!err && response.statusCode == 200){
resolve({body: body, path: filePath});
}else{
console.log('statusCode: ' + response.statusCode)
setTimeout(function(){
start(fetch_publish_url, form, timeout, filePath).then(function(body, path){
resolve({body: body, path: path});
});
}, timeout);
}
});
});
}
// ffmpeg -f dshow -i video="USB2.0 PC CAMERA" -vcodec libx264 -f flv "rtmp://pili-publish.xinqigu.com/education-live/control_936ae4df35def94475adee5ef46b2d9e_0?e=1502078776&token=FLyjJ-r-pHxLMRETyMHSVCK0lUa_6VnjMyd1aDtn:nqUrGaikVt9XPBmtDPfgoga6JQ8="
// 推送视频流
function start_command(publish_url, path){
var command = 'ffmpeg' + ' ' +
'-f dshow' + ' ' +
'-i video=' + "\"" + path + "\"" + " " +
'-vcodec libx264' + " " +
'-b:v 1024k' + " " +
'-r 30' + ' ' +
'-f flv' + " " +
'\"' + publish_url + "\"";
console.log(command);
cmd.run(command)
// if (cmd.run(command).code !== 0) {
// console.log('ffmpeg error!');
// exit(1);
// }
// var command = ffmpeg();
// command
// // .input(path)
// .inputOptions(['-f dshow', ['-i video='+ "\"" +path + "\""]])
// // .inputFormat('video')
// // .inputOptions(['-s 320*300'])
// .on('start', function(commandLine){
// console.log('Spawned FFmpeg with command:' + commandLine);
// }).on('error', function(err, stdout, stderr){
// console.log('error:' + err.message);
// console.log('stdout:' + stdout);
// console.log('stderr:' + stderr);
// }).on('end', function(){
// console.log('Processing finished!');
// })
// //.size('320x300')
// .videoCodec('libx264')
// .videoBitrate(1024) // 比特率 1024
// .format('flv')
// .fps(30)
// .output(publish_url, { end: true }).run();
}
/**
* 遍历文件,创建视频流,并开启推送视频流
**/
// var pattern = '/dev/video**';
// glob(pattern, {nodir: true}, function (err, files) {
// if (!err) {
// }
// })
var len = devices.length;
// 遍历所有的摄像头挂载文件
for (var i = 0; i < len; i++) {
form['index'] = i;
var filePath = devices[len - i -1];
console.log(filePath);
start(fetch_publish_url, form, 5000, filePath).then(function(data){
// console.log(data['body']);
// console.log(data['path']);
var json = JSON.parse(data['body']);
start_command(json.publish_url, data['path']);
// 睡眠
sleep(5000);
});
}