UNPKG

node-mac-recorder

Version:

Native macOS screen recording package for Node.js applications

143 lines (122 loc) • 4.37 kB
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();