|
| 1 | +"""Allows for capturing the screen and audio on macOS. |
| 2 | +
|
| 3 | +This is based on: https://gist.github.com/timsutton/0c6439eb6eb1621a5964 |
| 4 | +
|
| 5 | +usage: see bottom of file |
| 6 | +""" |
| 7 | +from datetime import datetime |
| 8 | +from sys import platform |
| 9 | +import os |
| 10 | + |
| 11 | +from Foundation import NSURL, NSObject # type: ignore # noqa |
| 12 | +from Quartz import CGMainDisplayID # type: ignore # noqa |
| 13 | +import AVFoundation as AVF # type: ignore # noqa |
| 14 | +import objc # type: ignore # noqa |
| 15 | + |
| 16 | +from openadapt import config |
| 17 | + |
| 18 | + |
| 19 | +class Capture: |
| 20 | + """Capture the screen, audio, and camera on macOS.""" |
| 21 | + |
| 22 | + def __init__(self) -> None: |
| 23 | + """Initialize the capture object.""" |
| 24 | + if platform != "darwin": |
| 25 | + raise NotImplementedError( |
| 26 | + "This is the macOS implementation, please use the Windows version" |
| 27 | + ) |
| 28 | + |
| 29 | + objc.options.structs_indexable = True |
| 30 | + |
| 31 | + def start(self, audio: bool = False, camera: bool = False) -> None: |
| 32 | + """Start capturing the screen, audio, and camera. |
| 33 | +
|
| 34 | + Args: |
| 35 | + audio (bool, optional): Whether to capture audio (default: False). |
| 36 | + camera (bool, optional): Whether to capture the camera (default: False). |
| 37 | + """ |
| 38 | + self.display_id = CGMainDisplayID() |
| 39 | + self.session = AVF.AVCaptureSession.alloc().init() |
| 40 | + self.screen_input = AVF.AVCaptureScreenInput.alloc().initWithDisplayID_( |
| 41 | + self.display_id |
| 42 | + ) |
| 43 | + self.file_output = AVF.AVCaptureMovieFileOutput.alloc().init() |
| 44 | + self.camera_session = None # not used if camera=False |
| 45 | + |
| 46 | + # Create an audio device input with the default audio device |
| 47 | + self.audio_input = AVF.AVCaptureDeviceInput.alloc().initWithDevice_error_( |
| 48 | + AVF.AVCaptureDevice.defaultDeviceWithMediaType_(AVF.AVMediaTypeAudio), None |
| 49 | + ) |
| 50 | + |
| 51 | + if not os.path.exists(config.CAPTURE_DIR_PATH): |
| 52 | + os.mkdir(config.CAPTURE_DIR_PATH) |
| 53 | + self.file_url = NSURL.fileURLWithPath_( |
| 54 | + os.path.join( |
| 55 | + config.CAPTURE_DIR_PATH, |
| 56 | + datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".mov", |
| 57 | + ) |
| 58 | + ) |
| 59 | + if audio and self.session.canAddInput_(self.audio_input[0]): |
| 60 | + self.session.addInput_(self.audio_input[0]) |
| 61 | + |
| 62 | + if self.session.canAddInput_(self.screen_input): |
| 63 | + self.session.addInput_(self.screen_input) |
| 64 | + |
| 65 | + self.session.addOutput_(self.file_output) |
| 66 | + |
| 67 | + self.session.startRunning() |
| 68 | + |
| 69 | + # Cheat and pass a dummy delegate object where |
| 70 | + # normally we'd have a AVCaptureFileOutputRecordingDelegate |
| 71 | + self.file_url = ( |
| 72 | + self.file_output.startRecordingToOutputFileURL_recordingDelegate_( |
| 73 | + self.file_url, NSObject.alloc().init() |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + if camera: |
| 78 | + self._use_camera() |
| 79 | + |
| 80 | + def _use_camera(self) -> None: |
| 81 | + """Start capturing the camera.""" |
| 82 | + self.camera_session = AVF.AVCaptureSession.alloc().init() |
| 83 | + self.camera_file_output = AVF.AVCaptureMovieFileOutput.alloc().init() |
| 84 | + self.camera_input = AVF.AVCaptureDeviceInput.alloc().initWithDevice_error_( |
| 85 | + AVF.AVCaptureDevice.defaultDeviceWithMediaType_(AVF.AVMediaTypeVideo), None |
| 86 | + ) |
| 87 | + |
| 88 | + if self.camera_session.canAddInput_(self.camera_input[0]): |
| 89 | + self.camera_session.addInput_(self.camera_input[0]) |
| 90 | + self.camera_session.startRunning() |
| 91 | + |
| 92 | + self.camera_session.addOutput_(self.camera_file_output) |
| 93 | + |
| 94 | + self.camera_url = ( |
| 95 | + self.camera_file_output.startRecordingToOutputFileURL_recordingDelegate_( |
| 96 | + NSURL.fileURLWithPath_( |
| 97 | + os.path.join( |
| 98 | + config.CAPTURE_DIR_PATH, |
| 99 | + datetime.now().strftime("camera.%Y-%m-%d-%H-%M-%S") + ".mov", |
| 100 | + ) |
| 101 | + ), |
| 102 | + NSObject.alloc().init(), |
| 103 | + ) |
| 104 | + ) |
| 105 | + |
| 106 | + def stop(self) -> None: |
| 107 | + """Stop capturing the screen, audio, and camera.""" |
| 108 | + self.session.stopRunning() |
| 109 | + if self.camera_session: |
| 110 | + self.camera_session.stopRunning() |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + capture = Capture() |
| 115 | + capture.start(audio=True, camera=False) |
| 116 | + input("Press enter to stop") |
| 117 | + capture.stop() |
0 commit comments