2025-07-03 17:39:09 +08:00
|
|
|
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":
|
2025-10-21 16:20:22 +08:00
|
|
|
case "arm64":
|
2025-07-03 17:39:09 +08:00
|
|
|
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);
|
|
|
|
|
})
|
|
|
|
|
.on("data", function(data) {});
|
|
|
|
|
this.shell.unref();
|
|
|
|
|
}
|
|
|
|
|
end(cb) {
|
|
|
|
|
if (!this.shell.killed) {
|
|
|
|
|
console.log(this.shell);
|
|
|
|
|
this.shell.stdin.write("q");
|
|
|
|
|
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 };
|