bbgRecording_1.js
4.8 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
var express = require('express');
var router = express.Router();
const { exec } = require('child_process');
const fs = require("fs");
const axios = require('axios');
const method = require("../config/method")
const { dayTimeYMD } = method
let classids = []
/**
*
* @param {*} id 课堂id
*/
class MediaCreat {
constructor() {
}
wrieLog(text) {
// 写入log
let logFile = `./log/${dayTimeYMD().ymd}.txt`
fs.appendFileSync(logFile, new Date().toLocaleString() + " " + text + '\r\n');
}
recordingCreat(id,siteId,classUrl,duration,width,height,frameRate,bitrate) {
this.wrieLog(" 课堂录制开始:------>" + id)
let fileConfig = this.getConfigFileJson()
if (!fileConfig) return false
const { BACKMEDIACONFIG, PROJECTWINCATALOG, PROJECTCATALOG } = JSON.parse(fileConfig)
let mediaDir = PROJECTCATALOG + "/media/"
let classDir = PROJECTCATALOG + "/media/" + siteId
let ymdDir = null
let timeDir = dayTimeYMD().ymd
ymdDir = PROJECTCATALOG + "/media/" + siteId + "/" + timeDir
if (!fs.existsSync(mediaDir)) {
fs.mkdirSync(mediaDir);
}
if (!fs.existsSync(classDir)) {
fs.mkdirSync(classDir);
}
if (!fs.existsSync(ymdDir)) {
fs.mkdirSync(ymdDir);
}
let files = fs.readdirSync(ymdDir);
// let interValGetFile = setInterval(()=>{
this.wrieLog("files:" + files)
if (files.indexOf(id + ".mp4") != -1) {
this.wrieLog("已存在:" + id + "课堂号,停止继续录制")
return
}
// if(!classUrl){
// classUrl = `${BACKMEDIACONFIG.url}?classId=${id}&recordMp4=${BACKMEDIACONFIG.recordMp4}&userId=${BACKMEDIACONFIG.userId}&userName=${BACKMEDIACONFIG.userName}&userRole=${BACKMEDIACONFIG.userRole}&portalIP=${BACKMEDIACONFIG.portalIP}&portalPort=${BACKMEDIACONFIG.portalPort}&channels=${BACKMEDIACONFIG.channels}"`
// }
// if(!duration){
// duration = BACKMEDIACONFIG.d
// }
let url = `${PROJECTWINCATALOG}/web_capture_c -o=${ymdDir}/${id}.mp4 -u="${classUrl}" -d=${duration} -s=${BACKMEDIACONFIG.s} -k=${BACKMEDIACONFIG.k} -w=${width} -h=${height} -fa=${frameRate} -vb=${bitrate}`
this.wrieLog("测试全地址"+url)
exec(url, { maxBuffer: 1073741824 }, (err, stdout, stderr) => {
if (err != null) {
this.wrieLog(" 错误" + id + ":" + err)
this.wrieLog(" 错误 stdout" + id + ":" + stdout)
this.wrieLog(" 错误 stderr" + id + ":" + stderr)
// 删除已存元素
classids.splice(classids.findIndex(item => item === id),1)
return
}
let files = fs.readdirSync(ymdDir);
if (files.indexOf(id + ".mp4") != -1) {
this.wrieLog(" 录制完成 课堂号:" + id)
// 删除已存元素
classids.splice(classids.findIndex(item => item === id),1)
}
})
}
getConfigFileJson() {
const buffer = fs.readFileSync(process.cwd() + "/config/realTimeConfig.json")
return String(buffer)
}
}
/**
* {
* "classId":[{ classId: '389675110', siteId: 'kuaikuenglish' }]
* }
*/
router.post('/recording', function (req, res, next) {
new MediaCreat().wrieLog("录制启动:------>")
let fileConfig = new MediaCreat().getConfigFileJson()
if (!fileConfig) return false
/**
* classId:课堂号
* siteId:机构编码
* url:课堂地址
* duration:录制时长
* width height 录制视频宽高
* frameRate 帧率
* bitrate 码率
*/
let { classId,siteId,url:classUrl,duration,width,height,frameRate,bitrate } = req.body
if (classId && siteId && classUrl && duration && width && height && frameRate && bitrate) {
if(!Number.isInteger(classId) || !Number.isInteger(duration) || !Number.isInteger(width) || !Number.isInteger(height) || !Number.isInteger(frameRate) || !Number.isInteger(bitrate)){
res.send({ code: "1", message: "参数类型错误" });
return
}
let maxTime = 10*60*60*1000
if(duration < 0 || duration > maxTime){
res.send({ code: "1", message: "参数范围错误" });
return
}
// 避免多次重复课堂号请求
if(!classids.includes(classId)){
// 没有正在录制的该课堂
classids.push(classId)
bitrate = bitrate * 1024 // 转换 * 1024
new MediaCreat().recordingCreat(classId, siteId,classUrl,duration,width,height,frameRate,bitrate)
res.send({ code: "0" });
}else{
res.send({ code: "2", message: "重复请求课堂号" });
}
} else {
res.send({ code: "1", message: "缺少参数" });
}
})
module.exports = router;