2011-09-29 60 views
3

我在OSX Lion上使用AVFoundation來執行屏幕捕獲。完成如下:OSX Lion上AVFoundation屏幕捕獲的比特率限制

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] 
    } 

} 

這會導致幀率大約爲1 Mbps - > 3 Mbps。這裏的問題是,在視頻設置我指定:

NSMutableDictionary * compressionSettings = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease]; 
[compressionSettings setObject:[NSNumber numberWithInt:512000] forKey:AVVideoAverageBitRateKey]; 
[videosettings setObject:compressionSettings forKey:AVVideoCompressionPropertiesKey]; 

是512K,並且具有較高的比特率會導致文件過大(我們需要畢竟對這些文件上傳)。

當我刪除

self->dataOutput.videoSettings=videosettings; 

,而是經由

self->writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo 
                 outputSettings:videosettings] retain]; 

應用視頻設置到writerinput的線I得到的比特率太低(通常爲100 Kbps的=> 300 Kbps)的。我認爲這是因爲編碼是通過軟件而不是硬件發生的(從AVCaptureSession返回數據後發生)。

我能做些什麼來強制捕獲從1-3 Mbps下降到512K?如果它可以走得更高,我無法想象它爲什麼不能限制它使用的速率。

感謝,

-G

回答

1

從文檔的AVCaptureVideoDataOutput videoSettings財產

Currently, the only supported key is kCVPixelBufferPixelFormatTypeKey. Supported pixel formats are 
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange and kCVPixelFormatType_32BGRA, 
except on iPhone 3G, where the supported pixel formats are kCVPixelFormatType_422YpCbCr8 and kCVPixelFormatType_32BGRA. 

設置壓縮設置這個類是沒有意義的。這意味着您AVAssetWriterInput的壓縮設置爲零。因此,您將獲得該設備的任何默認速率。

雖然在OS-X AVFoundaton實現中肯定會存在一個錯誤,但您接收的比特率可能是正確的。例如視頻中有多少動作?場景有多複雜?另外請記住,H264/AVC不是一個恆定的比特率編解碼器。

+0

「iPhone 3G以外」讓我相信這是來自iPhone版AVFoundation的文檔。 – George

+0

你是對的。即使如此,至少在我看來,將H264編碼參數附加到視頻輸出上很有意義。至少在iOS上,直接進行H264編碼的唯一類是AVAssetWriter。 –