AVAssetWriter failing to encode video with AVVideo

2019-07-03 13:17发布

问题:

Can anyone help me out with this problem? I am able to encode a video using AVAssetWriter with following output settings.

NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                          AVVideoCodecH264, AVVideoCodecKey,
                                          [NSNumber numberWithInteger:dimensions.width], AVVideoWidthKey,
                                          [NSNumber numberWithInteger:dimensions.height], AVVideoHeightKey,
                                          [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
                                           [NSNumber numberWithInteger:30],AVVideoMaxKeyFrameIntervalKey,
                                           nil], AVVideoCompressionPropertiesKey,   
                                          nil];

However when i try to encode by specifying a profile say AVVideoProfileLevelH264Baseline30 output settings after this will look like

NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                          AVVideoCodecH264, AVVideoCodecKey,
                                          [NSNumber numberWithInteger:dimensions.width], AVVideoWidthKey,
                                          [NSNumber numberWithInteger:dimensions.height], AVVideoHeightKey,
                                          [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
                                           [NSNumber numberWithInteger:30],AVVideoMaxKeyFrameIntervalKey,
                                           AVVideoProfileLevelH264Baseline30,AVVideoProfileLevelKey,
                                           nil], AVVideoCompressionPropertiesKey,   
                                          nil];

My video recording fails with these settings.

So where is my problem here ? I have initialized Asset writer like this

_assetWriter = [[AVAssetWriter alloc] initWithURL:_movieURL fileType:AVFileTypeMPEG4 error:&error];

回答1:

I've had this fail too. For me, the problem was only on OS X 10.7 (Lion), which simply does not support AVVideoProfileLevelKey... You can still encode in H.264, but just need to omit that parameter, and the OS will handle choosing a profile to use. If you're seeing this fail on OS X 10.8/10.9, or iOS 4 or later, then there's something else going wrong for you.