Search code examples
iosavcapturesessiongpuimage

AVCaptureSession image dimensions (with GPUImage)


I am trying to implement video recording in iOS on top of the GPUImage.

I have a GPUImageVideoCamera and I am trying to set up recording like this:

-(void)toggleVideoRecording {
if (!self.recording) {
    self.recording = true;
    GPUImageMovieWriter * mw = [self createMovieWriter: self.filter1];

    [self.filter1 addTarget:mw];
    mw.shouldPassthroughAudio = YES;
    [mw startRecording];
}
else {
    self.recording = false;
    [self finishWritingMovie];
}

}

-(GPUImageMovieWriter *) createMovieWriter:(GPUImageFilter *) forFilter {
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/CurrentMovie.m4v"];
    unlink([pathToMovie UTF8String]);
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

    GPUImageMovieWriter * mw = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:forFilter->currentFilterSize];
    self.movieWriter = mw;
    self.movieURL = movieURL;

    return mw;
}

This actually mostly works. However, MoviewWriter needs the CGSize of the input media, and I don't know how to pull it out of the VideoCamera. First of all, the size varies, because the camera is using sessionPreset: AVCaptureSessionPresetHigh, meaning it varies depending on the device's capabilities. And secondly, the orientation changes the relevant CGSize. If the user starts recording in landscape mode, I want to transpose the dimensions.

For anyone who knows how to do this with vanilla CoreVideo, I can pull the AVCaptureSession out of my GPUImageVideoCamera. (But I've read the API for AVCaptureSession and nothing looked promising).

Even referring me to a sample project that is handling this would be very helpful.


Solution

  • It is indeed possible to pull the dimensions out of the capture session. Here's how:

    // self.videoCamera is a GPUImageVideoCamera
    
    AVCaptureVideoDataOutput *output = [[[self.videoCamera captureSession] outputs] lastObject];
    NSDictionary* outputSettings = [output videoSettings];
    
    long width  = [[outputSettings objectForKey:@"Width"]  longValue]; 
    long height = [[outputSettings objectForKey:@"Height"] longValue];
    
    if (UIInterfaceOrientationIsPortrait([self.videoCamera outputImageOrientation])) {
        long buf = width;
        width = height;
        height = buf;
    }
    
    GPUImageMovieWriter * mw = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(width, height)];