node-mac-recorder
Version:
Native macOS screen recording package for Node.js applications
143 lines (122 loc) ⢠4.37 kB
JavaScript
const MacRecorder = require("./index");
const path = require("path");
const fs = require("fs");
async function testRecording() {
console.log("š¬ Testing ScreenCaptureKit Recording...\n");
const recorder = new MacRecorder();
const outputDir = path.join(__dirname, "test-output");
// Create test output directory
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
console.log("š Created test-output directory");
}
const outputFile = path.resolve(outputDir, "sck-test-recording.mov");
console.log(`š¹ Output file: ${outputFile}`);
// Test recording options
const recordingOptions = {
captureCursor: true,
excludeCurrentApp: true,
includeMicrophone: true,
includeSystemAudio: true, // Disable to avoid permission issues for now
displayId: null, // Will use main display
};
console.log(
"š Recording options:",
JSON.stringify(recordingOptions, null, 2)
);
console.log("\nš Starting recording test...");
try {
// Test current cursor position before recording
const cursor = recorder.getCurrentCursorPosition();
console.log(
`š±ļø Current cursor: x=${cursor.x}, y=${cursor.y}, type=${cursor.cursorType}`
);
// Determine a window to exclude (prefer a window with name containing "Cursor")
try {
const windows = await recorder.getWindows();
if (Array.isArray(windows) && windows.length) {
const pick =
windows.find(
(w) =>
(typeof w.appName === "string" && /cursor/i.test(w.appName)) ||
(typeof w.name === "string" && /cursor/i.test(w.name)) ||
(typeof w.title === "string" && /cursor/i.test(w.title))
) || windows[0];
const wid = pick?.id ?? pick?.windowId ?? pick?.windowID ?? null;
if (wid != null) {
recordingOptions.excludeWindowIds = [Number(wid)];
}
}
} catch (_) {}
// Start recording
console.log("ā¶ļø Attempting to start recording...");
// Start recording without callback first
console.log("š Attempting startRecording without callback...");
let startResult;
try {
startResult = await recorder.startRecording(outputFile, recordingOptions);
console.log(`š startRecording resolved: ${startResult}`);
} catch (error) {
console.error("ā startRecording threw error:", error.message);
console.error("Stack:", error.stack);
return;
}
if (startResult) {
console.log("ā
Recording started successfully");
console.log("ā±ļø Recording for 3 seconds...");
// Record for ~6 seconds
setTimeout(async () => {
console.log("ā¹ļø Stopping recording...");
let stopResult;
try {
stopResult = await recorder.stopRecording();
console.log(
`š stopRecording resolved: ${JSON.stringify(stopResult)}`
);
if (stopResult && stopResult.code === 0) {
console.log("ā
Stop recording command sent");
} else {
console.log("ā Failed to send stop recording command");
}
} catch (error) {
console.error("ā stopRecording threw error:", error.message);
console.error("Stack:", error.stack);
}
// Final check after a longer delay
setTimeout(() => {
console.log("\nš Final Results:");
try {
if (fs.existsSync(outputFile)) {
const stats = fs.statSync(outputFile);
console.log(`ā
Recording file exists: ${stats.size} bytes`);
console.log(`š Location: ${outputFile}`);
console.log("šÆ ScreenCaptureKit recording test PASSED");
} else {
console.log("ā Recording file does not exist");
console.log(
"š This might be due to permissions or ScreenCaptureKit configuration"
);
}
} catch (error) {
console.error("ā Final check error:", error.message);
}
console.log("\nā
Recording test completed");
}, 4000);
}, 6000);
} else {
console.log("ā Failed to start recording");
console.log("š Possible causes:");
console.log(" ⢠Screen recording permissions not granted");
console.log(" ⢠ScreenCaptureKit not available (requires macOS 12.3+)");
console.log(" ⢠Display/window selection issues");
}
} catch (error) {
console.error("ā Recording test failed with exception:", error);
}
}
// Handle process exit
process.on("SIGINT", () => {
console.log("\nā ļø Recording test interrupted");
process.exit(0);
});
testRecording();