Skip to content

Commit

Permalink
SAVE_IMAGES flag
Browse files Browse the repository at this point in the history
  • Loading branch information
0dm committed Mar 23, 2024
1 parent 33098b3 commit 3a4deff
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions openadapt/capture/_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
from sys import platform
import os

from Foundation import NSObject, NSLog, NSAutoreleasePool
from Quartz import CIImage
from AppKit import NSBitmapImageRep, NSImage, NSImageCompressionFactor, NSPNGFileType
from CoreMedia import CMSampleBufferGetImageBuffer
from Foundation import NSAutoreleasePool, NSLog, NSObject
from Quartz import CIImage
from Quartz.CoreGraphics import CGMainDisplayID
from AppKit import NSBitmapImageRep, NSImage, NSPNGFileType, NSImageCompressionFactor
import AVFoundation as AVF
import objc


NULL_PTR = POINTER(c_int)()

SAVE_IMAGES = None

_lib = cdll.LoadLibrary("/usr/lib/system/libdispatch.dylib")
_dispatch_queue_create = _lib.dispatch_queue_create
_dispatch_queue_create.argtypes = [c_char_p, c_void_p]
Expand Down Expand Up @@ -52,11 +53,13 @@ def captureOutput_didOutputSampleBuffer_fromConnection_(
bitmapRep = NSBitmapImageRep.alloc().initWithCIImage_(ciImage)
dict = {NSImageCompressionFactor: 1.0}
pngData = bitmapRep.representationUsingType_properties_(NSPNGFileType, dict)
if not os.path.exists("captures"):
os.makedirs("captures")
pngData.writeToFile_atomically_(
os.path.join("captures", "frame{}.png".format(datetime.now())), False
)

if SAVE_IMAGES:
if not os.path.exists("captures"):
os.makedirs("captures")
pngData.writeToFile_atomically_(
os.path.join("captures", "frame{}.png".format(datetime.now())), False
)


class Capture:
Expand All @@ -71,9 +74,10 @@ def __init__(self) -> None:

objc.options.structs_indexable = True

def start(self) -> None:
def start(self, save_images=False) -> None:
"""Start capture"""

global SAVE_IMAGES
SAVE_IMAGES = save_images
self.display_id = CGMainDisplayID()
self.session = AVF.AVCaptureSession.alloc().init()
self.screen_input = AVF.AVCaptureScreenInput.alloc().initWithDisplayID_(
Expand Down Expand Up @@ -107,6 +111,6 @@ def stop(self) -> None:

if __name__ == "__main__":
capture = Capture()
capture.start()
capture.start(save_images=False)
input("Press enter to stop")
capture.stop()

0 comments on commit 3a4deff

Please sign in to comment.