index.js
8.1 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var express = require('express');
var router = express.Router();
const { spawn } = require('child_process');
const fs = require("fs");
const method = require("../config/method")
const config = require("../config/config")
// const { GETCLASSURL, GETCLASSURLPARAMETER, PROJECTCATALOG, PROJECTWINCATALOG, BACKMEDIACONFIG } = config
const { YesterdayTime, getRequestClassIds,dayTimeYMD } = method
const { startTime, endTime, ymd } = YesterdayTime()
let siteIds = []
let classid = []
let parentData = {}
var classobj = {};
let className = ""
/**
*
* @param {*} id 课堂id
*/
class MediaCreat {
constructor() { }
// 取出所有数据
async allData() {
let fileConfig = await new MediaCreat().getConfigFileJson()
if(!fileConfig) return false
className = siteIds.shift()
const { GETCLASSURL,GETCLASSURLPARAMETER } = JSON.parse(fileConfig)
let page = 1
if (className) {
let result = await getRequestClassIds(GETCLASSURL, className, GETCLASSURLPARAMETER.key, startTime, endTime, page)
parentData[result.data.data.siteId] = new Set()
for (let j = 0; j < result.data.data.list.length; j++) {
classid.push(result.data.data.list[j])
parentData[result.data.data.siteId].add(result.data.data.list[j]['classId'])
}
const { siteId, list, totalPage } = result.data.data
for (let i = page += 1; i <= totalPage; i++) {
let result = await getRequestClassIds(GETCLASSURL, className, GETCLASSURLPARAMETER.key, startTime, endTime, i)
for (let j = 0; j < result.data.data.list.length; j++) {
classid.push(result.data.data.list[j])
parentData[result.data.data.siteId].add(result.data.data.list[j]['classId'])
}
}
parentData[result.data.data.siteId] = Array.from(parentData[result.data.data.siteId])
if (siteIds.length) {
return await new MediaCreat().allData()
}
return true
} else {
return false
}
}
async mediaCreat(id, siteId) {
let fileConfig = await new MediaCreat().getConfigFileJson()
if(!fileConfig) return false
const { BACKMEDIACONFIG,PROJECTWINCATALOG,PROJECTCATALOG } = JSON.parse(fileConfig)
let mediaDir = PROJECTCATALOG + "/media/"
let classDir = PROJECTCATALOG + "/media/" + siteId
let ymdDir = PROJECTCATALOG + "/media/" + siteId + "/" + ymd
if (!fs.existsSync(mediaDir)) {
fs.mkdirSync(mediaDir);
}
if (!fs.existsSync(classDir)) {
fs.mkdirSync(classDir);
}
if (!fs.existsSync(ymdDir)) {
fs.mkdirSync(ymdDir);
}
let url = `web_capture_c -o=../media/${siteId}/${ymd}/${id}.mp4 -u="${BACKMEDIACONFIG.url}?classId=${id}&userId=${BACKMEDIACONFIG.userId}&userName=${BACKMEDIACONFIG.userName}&userRole=${BACKMEDIACONFIG.userRole}&portalIP=${BACKMEDIACONFIG.portalIP}&portalPort=${BACKMEDIACONFIG.portalPort}&channels=${BACKMEDIACONFIG.channels}&playRecord=${BACKMEDIACONFIG.playRecord}" -d=${BACKMEDIACONFIG.d} -s=${BACKMEDIACONFIG.s} -fa=${BACKMEDIACONFIG.fa} -k=${BACKMEDIACONFIG.k} -w=${BACKMEDIACONFIG.w} -h=${BACKMEDIACONFIG.h}`
var workerProcess = spawn(url, { cwd: PROJECTWINCATALOG, shell: true })
// workerProcess.stdout.on('data', async function (data) {
// });
workerProcess.on('close', async function (code) {
let files = fs.readdirSync(ymdDir);
if (files.indexOf(id + ".mp4") == -1) {
//当前课堂没有写入到文件夹,进入重录
new MediaCreat().mediaCreat(id, siteId)
return false
} else {
if (parentData[siteId].indexOf(id.toString()) != -1) {
parentData[siteId].splice(parentData[siteId].indexOf(id.toString()), 1)
}
if (parentData[siteId].length == 0) {
// //全部录制完毕
fs.writeFile(ymdDir + "/download.json", `{ "code": "0", "success": "ok"}`, function (err) {
if (err) {
console.log(err);
}
});
}
if (classid.length) {
let shiftData = classid.shift()
new MediaCreat().mediaCreat(shiftData['classId'], shiftData['siteId'])
}
}
});
}
async recordingCreat(id, siteId) {
let fileConfig = await new MediaCreat().getConfigFileJson()
if(!fileConfig) return false
const { BACKMEDIACONFIG,PROJECTWINCATALOG,PROJECTCATALOG } = JSON.parse(fileConfig)
let mediaDir = PROJECTCATALOG + "/media/"
let recordingDir = mediaDir + "recording/"
let classDir = recordingDir + siteId +"/"
let dateDir = classDir + dayTimeYMD().ymd
if (!fs.existsSync(mediaDir)) {
fs.mkdirSync(mediaDir);
}
if (!fs.existsSync(recordingDir)) {
fs.mkdirSync(recordingDir);
}
if (!fs.existsSync(classDir)) {
fs.mkdirSync(classDir);
}
if (!fs.existsSync(dateDir)) {
fs.mkdirSync(dateDir);
}
let url = `web_capture_c -o=../media/recording/${siteId}/${dayTimeYMD().ymd}/${id}.mp4 -u="${BACKMEDIACONFIG.url}?classId=${id}&userId=${BACKMEDIACONFIG.userId}&userName=${BACKMEDIACONFIG.userName}&userRole=${BACKMEDIACONFIG.userRole}&portalIP=${BACKMEDIACONFIG.portalIP}&portalPort=${BACKMEDIACONFIG.portalPort}&channels=${BACKMEDIACONFIG.channels}&playRecord=${BACKMEDIACONFIG.playRecord}" -d=${BACKMEDIACONFIG.d} -s=${BACKMEDIACONFIG.s} -fa=${BACKMEDIACONFIG.fa} -k=${BACKMEDIACONFIG.k} -w=${BACKMEDIACONFIG.w} -h=${BACKMEDIACONFIG.h}`
var workerProcess = spawn(url, { cwd: PROJECTWINCATALOG, shell: true })
// workerProcess.stdout.on('data', async function (data) {
// });
workerProcess.on('close', async function (code) {
let files = fs.readdirSync(dateDir);
if (files.indexOf(id + ".mp4") == -1) {
//当前课堂没有写入到文件夹,进入重录
new MediaCreat().recordingCreat(id, siteId)
return false
} else {
if (parentData[siteId].indexOf(id.toString()) != -1) {
parentData[siteId].splice(parentData[siteId].indexOf(id.toString()), 1)
}
if (parentData[siteId].length == 0) {
// //全部录制完毕
fs.writeFile(dateDir + "/download.json", `{ "code": "0", "success": "ok"}`, function (err) {
if (err) {
console.log(err);
}
});
}
if (classid.length) {
let shiftData = classid.shift()
new MediaCreat().recordingCreat(shiftData['classId'], shiftData['siteId'])
}
}
});
}
async getConfigFileJson(){
const buffer= fs.readFileSync(process.cwd()+"/config/config.json")
return String(buffer)
}
}
router.get('/', async function (req, res, next) {
let fileConfig = await new MediaCreat().getConfigFileJson()
if(!fileConfig) return false
const {GETCLASSURLPARAMETER} = JSON.parse(fileConfig)
siteIds = GETCLASSURLPARAMETER.siteId
let result = await new MediaCreat().allData()
if (result) {
// 去重
classid = classid.reduce(function (item, next) {
classobj[next.classId] ? '' : classobj[next.classId] = true && item.push(next);
return item;
}, []);
if (classid.length) {
for (let i = 0; i < GETCLASSURLPARAMETER.maxMedia; i++) {
let shiftData = classid.shift()
if (shiftData) {
new MediaCreat().mediaCreat(shiftData['classId'], shiftData['siteId'])
} else {
return false
}
}
}
res.send({ code: "0" });
}
});
/**
* {
* "classId":[{ classId: '389675110', siteId: 'kuaikuenglish' }]
* }
*/
router.post('/recording', async function (req, res, next) {
let fileConfig = await new MediaCreat().getConfigFileJson()
if(!fileConfig) return false
const { classId } = req.body
if(classId && classId.length){
classid = classId
const { PROJECTCATALOG,GETCLASSURLPARAMETER } = JSON.parse(fileConfig)
for(let i=0;i<classId.length;i++){
if(!Array.isArray(parentData[classId[i].siteId])){
parentData[classId[i].siteId] = []
}
parentData[classId[i].siteId].push(classId[i].classId)
}
for(let i=0;i<GETCLASSURLPARAMETER.maxMedia;i++){
let shiftData = classid.shift()
if (shiftData) {
new MediaCreat().recordingCreat(shiftData['classId'], shiftData['siteId'])
} else {
return false
}
}
}
res.send({ code: "0" });
})
module.exports = router;