I'm using AVFoundation on OSX Lion to do screen capture. Accomplished as follows:
self->screenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:self->screen];
self->dataOutput = [[AVCaptureVideoDataOutput alloc] init];
self->session = [[AVCaptureSession alloc] init];
self->assetWriter = [[AVAssetWriter alloc] initWithURL:url
fileType:AVFileTypeQuickTimeMovie
error:&error];
self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:nil] retain];
self->dataOutput.videoSettings=videosettings;
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
if(!self->startedWriting)
{
[self->assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
self->startedWriting = YES;
}
if(self->writerInput.readyForMoreMediaData)
{
[self->writerInput appendSampleBuffer:sampleBuffer]
}
}
This results in a framerate roughly 1 Mbps -> 3 Mbps. The problem with this is that in video settings I've specified:
NSMutableDictionary * compressionSettings = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease];
[compressionSettings setObject:[NSNumber numberWithInt:512000] forKey:AVVideoAverageBitRateKey];
[videosettings setObject:compressionSettings forKey:AVVideoCompressionPropertiesKey];
are for 512K, and having a higher bitrate causes the files to be too big (we need to upload these files after all).
When I remove the line
self->dataOutput.videoSettings=videosettings;
and instead apply the video settings to the writerinput via
self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videosettings] retain];
I get a bitrate that is too low (usually 100 Kbps => 300 Kbps). I assume this is because the encoding is taking place via software instead of hardware (it is happening after the data is returned from the AVCaptureSession
).
What can I do to force the capture to go down from 1-3 Mbps and down to just 512K? If it can go higher, I can't imagine why it wouldn't be able to just cap the rate it's using.
Thanks,
-G