简体   繁体   中英

Specific filepath to store Screen Record using CGDisplayStream in OSX

I have been working on a c++ command line tool to record screen. After some searching I have come up with this following code. Looks like screen is being recorded when I compile and run the code. I am looking for functions where I can provide the specific filepath where the screen record is to be stored. Also I would like to append the timestamp along with filename. If anybody has better approach or method to this problem please suggest here. Any leads are appreciated. Thanks

#include <ApplicationServices/ApplicationServices.h>

int main(int argc, const char * argv[]) {
    // insert code here...

    CGRect mainMonitor = CGDisplayBounds(CGMainDisplayID());
    CGFloat monitorHeight = CGRectGetHeight(mainMonitor);
    CGFloat monitorWidth = CGRectGetWidth(mainMonitor);
    const void *keys[1] = { kCGDisplayStreamSourceRect };
    const void *values[1] = { CGRectCreateDictionaryRepresentation(CGRectMake(0, 0, 100, 100)) };

    CFDictionaryRef properties = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

    CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), monitorWidth, monitorHeight, '420f' , properties,  ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef){});

    CGDirectDisplayID displayID = CGMainDisplayID();
    CGImageRef image_create = CGDisplayCreateImage(displayID);

    CFRunLoopSourceRef runLoop = CGDisplayStreamGetRunLoopSource(stream);

   // CFRunLoopAddSource(<#CFRunLoopRef rl#>, runLoop, <#CFRunLoopMode mode#>);

    CGError err = CGDisplayStreamStart(stream);
    if (err == CGDisplayNoErr) {
        std::cout<<"WORKING"<<std::endl;
        sleep(5);
    } else {
        std::cout<<"Error: "<<err<<std::endl;
    }

    //std::cout << "Hello, World!\n";
    return 0;
}

You should do that in the callback which you provide in CGDisplayStreamCreate . You can access the pixels via IOSurfaceGetBaseAddress (see other IOSurface functions). If you don't want to do the pixel twiddling yourself, you could create a CVPixelBuffer with CVPixelBufferCreateWithBytes from the IOSurface and then create a CIImage with [CIImage imageWithCVImageBuffer] and save that to file as seen here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM