Created
March 7, 2024 13:35
-
-
Save wsd1/0fad86fb5e0abf01d494249305fc981b to your computer and use it in GitHub Desktop.
这是AI辅助生成的代码:用nodejs向/dev/video0 推送动态帧。测试用途。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const WIDTH = 512; | |
const HEIGHT = 32; | |
// 打开虚拟的 V4L 设备文件 | |
const v4l2Device = fs.openSync('/dev/video0', 'w'); | |
// 创建虚拟视频帧数据 | |
const frameData = Buffer.alloc(WIDTH * HEIGHT * 3); // 24-bit RGB | |
// 循环生成虚拟视频数据并写入 V4L 设备 | |
const intervalId = setInterval(() => { | |
generateFrame(); | |
writeFrame(); | |
}, 1000 / 3); // 30 fps | |
// 监听 SIGINT 信号,当用户按下 CTRL+C 时执行退出程序的操作 | |
process.on('SIGINT', () => { | |
console.log('Received SIGINT, exiting gracefully...'); | |
clearInterval(intervalId); // 停止生成帧数据的循环 | |
closeAndExit(); // 执行退出程序的操作 | |
}); | |
function generateFrame() { | |
// 在这里可以编写生成视频帧数据的逻辑 | |
// 例如,随机生成彩色帧数据 | |
for (let i = 0; i < WIDTH * HEIGHT * 3; i += 3) { | |
frameData[i] = Math.floor(Math.random() * 256); // R | |
frameData[i + 1] = Math.floor(Math.random() * 256); // G | |
frameData[i + 2] = Math.floor(Math.random() * 256); // B | |
} | |
} | |
function writeFrame() { | |
// 将视频帧数据写入虚拟 V4L 设备 | |
fs.writeSync(v4l2Device, frameData, 0, frameData.length, 0); | |
} | |
// 关闭虚拟 V4L 设备文件并退出程序 | |
function closeAndExit() { | |
fs.closeSync(v4l2Device); | |
process.exit(0); // 退出 Node.js 进程 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment