-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (56 loc) · 1.72 KB
/
Copy pathindex.js
File metadata and controls
66 lines (56 loc) · 1.72 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
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const { Server } = require("socket.io");
const io = new Server(http, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/default", (req, res) => {
res.sendFile(__dirname + "/default.html");
});
let frameCount = 0;
const framesDir = path.join(__dirname, "frames");
// 创建存储帧的目录
if (!fs.existsSync(framesDir)) {
fs.mkdirSync(framesDir);
}
io.on("connection", (socket) => {
console.log("A user connected");
socket.on("recognized-image", (imageDataURL) => {
// 将base64图像数据转换为Buffer
const base64Data = imageDataURL.replace(/^data:image\/png;base64,/, "");
const buffer = Buffer.from(base64Data, "base64");
// 保存每一帧图片
const framePath = path.join(framesDir, `frame_${frameCount}.png`);
fs.writeFileSync(framePath, buffer);
frameCount++;
console.log(`Frame ${frameCount} saved`);
});
socket.on("disconnect", () => {
console.log("User disconnected");
// 当用户断开连接时,可以合成视频
exec(
`ffmpeg -framerate 15 -i ${framesDir}/frame_%d.png -s 1280x720 -c:v libx264 -pix_fmt yuv420p output.mp4`,
(error, stdout, stderr) => {
if (error) {
console.error(`Error creating video: ${error}`);
} else {
console.log("Video created successfully");
}
}
);
});
});
const PORT = 3000;
http.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});