Skip to content

Instantly share code, notes, and snippets.

@Tonyliu2ca
Forked from timsutton/record_screencap.py
Created March 18, 2016 01:59
Show Gist options
  • Save Tonyliu2ca/c7e6625f44aec9df5830 to your computer and use it in GitHub Desktop.
Save Tonyliu2ca/c7e6625f44aec9df5830 to your computer and use it in GitHub Desktop.
Screen recording with Python and AVFoundation
#!/usr/bin/python
# pylint: disable-msg=e1101,e0611
import time
import AVFoundation as AVF
import Quartz
from Foundation import NSObject, NSURL
def main():
display_id = Quartz.CGMainDisplayID()
session = AVF.AVCaptureSession.alloc().init()
screen_input = AVF.AVCaptureScreenInput.alloc().initWithDisplayID_(display_id)
file_output = AVF.AVCaptureMovieFileOutput.alloc().init()
session.addInput_(screen_input)
session.addOutput_(file_output)
session.startRunning()
file_url = NSURL.fileURLWithPath_('foo.mov')
# Cheat and pass a dummy delegate object where normally we'd have a
# AVCaptureFileOutputRecordingDelegate
file_url = file_output.startRecordingToOutputFileURL_recordingDelegate_(
file_url, NSObject.alloc().init())
time.sleep(10)
session.stopRunning()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment