import path from "path"; import fs from "fs"; import { GetHomeDir } from "./config"; const os = require("os"); let platform = os.platform(); const arch = os.arch(); const moment = require("moment"); const { spawn } = require("child_process"); const EventEmitter = require("events"); let ffmpegExePath = path.join(GetHomeDir(), "ffplay"); class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter(); class Recorder { constructor(option = {}) { this.shell = undefined; this.filename = moment(parseInt(new Date().getTime())).format("YYYYMMDDHHmmss") + ".mp4"; this.exe = "ffmpeg.exe"; if (platform === "win32") { this.exe = "ffmpeg.exe"; this.params = "-f gdigrab -r 30 -y -i desktop -pix_fmt yuv420p"; } if (platform === "linux") { switch (arch) { case "x64": this.exe = "ffmpeg_x86"; break; case "arm": case "arm64": this.exe = "ffmpeg_arm"; break; } this.params = "-v verbose -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0 -c:v libx264 -preset ultrafast -crf 18"; //-s 1920x1080 // ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0 -c:v libx264 -preset ultrafast -crf 18 output.mp4 } this.commands = path.join(GetHomeDir(), "/ffplay/" + this.exe); } get_path() { return path.join(ffmpegExePath, this.filename); } start() { this.exec(this.commands, this.params); } exec(commands, param) { let arr = param.split(" "); arr.push(this.get_path()); console.log("commands, arr", commands, arr); this.shell = spawn(commands, arr, { ffmpegExePath, // stdio: "ignore", // shell: true }) .on("exit", (err) => { console.log("exit", err); myEmitter.emit("process-exit"); }) .on("data", function(data) { // console.log(typeof data); }); this.shell.stdout.on('data', (data) => { }); this.shell.stderr.on('data', (data) => { }); this.shell.unref(); } end(cb) { if (!this.shell.killed) { console.log(this.shell); this.shell.stdin.write("q\n"); myEmitter.once("process-exit", () => { cb(); }); } else { cb(); } } move(dst, cb) { let readStream = fs.createReadStream(this.get_path()); let writeStream = fs.createWriteStream(dst); readStream.pipe(writeStream); readStream.on("end", () => { fs.unlinkSync(this.get_path()); cb(); }); } } /*function start() { config.recorder = new Recorder() config.recorder.start() } function end(cb) { config.recorder.end(() => { cb() }) } function getRecorder() { return config.recorder } function resetRecorder() { config.recorder = null } exports.Recorder = { start, end, getRecorder, resetRecorder, }*/ export { Recorder };