AVVideoAverageBitRateKey not taking effect

ios, video

Solution

Nobody's answered this yet, but here goes the resurrection.. Bitrate is a complicated value proportional to various other variables in the video equation - related to framerate, resolution, and other factors. Codec used. Constant bitrate or variable bitrate? Etc.. Some of these factors themselves will have limiting effects, which are passed on to the ultimately observed average bitrate.. Notice the key you're using to specify bitrate is called `AVVideoAverageBitRateKey` - average value, not fixed value. Requesting a bitrate is like requesting a ballpark figure, the outcome should be close to what you requested, but probably not exactly the same.

Problem

I'm setting AVVideoAverageBitRateKey to 900000, but the videos I record get different bitrate values. Always different values, sometimes 850k, sometimes 780k, 810k, never 900k. Why? Here is the settings I do: ``` NSNumber *compression = [NSNumber numberWithLong:900000]; AVAsset *videoAsset = [[AVURLAsset alloc] initWithURL:inputURL options:nil]; AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; CGSize videoSize = videoTrack.naturalSize; NSDictionary *videoWriterCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:compression, AVVideoAverageBitRateKey, nil]; NSDictionary *videoWriterSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey, videoWriterCompressionSettings, AVVideoCompressionPropertiesKey, [NSNumber numberWithFloat:videoSize.width], AVVideoWidthKey, [NSNumber numberWithFloat:videoSize.height], AVVideoHeightKey, nil]; AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoWriterSettings]; videoWriterInput.expectsMediaDataInRealTime = YES; videoWriterInput.transform = videoTrack.preferredTransform; AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:outputURL fileType:AVFileTypeQuickTimeMovie error:nil]; [videoWriter addInput:videoWriterInput]; ```

Original source