bbgRecording_1.js 4.8 KB
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;