@property (nonatomic, strong) NSDictionary *videoCompressionSettings;
self.assetWriter = [AVAssetWriter assetWriterWithURL:self.videoURL fileType:AVFileTypeMPEG4 error:nil];
//写入视频大小
NSInteger numPixels = kScreenWidth * kScreenHeight;
//每像素比特
CGFloat bitsPerPixel = 6;//12
NSInteger bitsPerSecond = numPixels * bitsPerPixel;
// 码率和帧率设置
NSDictionary *compressionProperties = @{ AVVideoAverageBitRateKey : @(bitsPerSecond),
AVVideoExpectedSourceFrameRateKey : @(10),
AVVideoMaxKeyFrameIntervalKey : @(10),
AVVideoProfileLevelKey : AVVideoProfileLevelH264BaselineAutoLevel };
//视频属性
self.videoCompressionSettings = @{ AVVideoCodecKey : AVVideoCodecH264,
AVVideoScalingModeKey : AVVideoScalingModeResizeAspectFill,
AVVideoWidthKey : @(kScreenHeight * 2),
AVVideoHeightKey : @(kScreenWidth * 2),
AVVideoCompressionPropertiesKey : compressionProperties };
_assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:self.videoCompressionSettings];
//expectsMediaDataInRealTime 必须设为yes,需要从capture session 实时获取数据
_assetWriterVideoInput.expectsMediaDataInRealTime = YES;
--------------------------------详细说明-------------------------------
AVVideoCompressionPropertiesKey:设置对应的值为以下的compressionProperties2。*
*NSMutableDictionary compressionProperties2 = [[NSMutableDictionary alloc]init]
[compressionProperties2 setObject: [NSNumber numberWithInt:heightwidth7.5] forKey:AVVideoAverageBitRateKey];
[compressionProperties2 setObject: [NSNumber numberWithInt:10] forKey:AVVideoMaxKeyFrameIntervalKey];
[compressionProperties2 setObject:AVVideoProfileLevelH264Main30 forKey:AVVideoProfileLevelKey];
....
AVVideoAverageBitRateKey:视频尺寸*比率,10.1相当于AVCaptureSessionPresetHigh,数值越大,显示越精细
...
AVVideoMaxKeyFrameIntervalKey:关键帧最大间隔,1为每个都是关键帧,数值越大压缩率越高。
....
AVVideoProfileLevelKey:
P-Baseline Profile:基本画质。支持I/P 帧,只支持无交错(Progressive)和CAVLC;
EP-Extended profile:进阶画质。支持I/P/B/SP/SI 帧,只支持无交错(Progressive)和CAVLC;
MP-Main profile:主流画质。提供I/P/B 帧,支持无交错(Progressive)和交错(Interlaced),也支持CAVLC 和CABAC 的支持;
HP-High profile:高级画质。在main Profile 的基础上增加了8×8内部预测、自定义量化、 无损视频编码和更多的YUV 格式;
实时直播:
低清Baseline Level 1.3
标清Baseline Level 3
半高清Baseline Level 3.1
全高清Baseline Level 4.1
存储媒体:
低清 Main Level 1.3
标清 Main Level 3
半高清 Main Level 3.1
全高清 Main Level 4.1
高清存储:
半高清 High Level 3.1
全高清 High Level 4.1
iPad 支持:
Baseline Level 1-3.1
Main Level 1-3.1
High Level 1-3.1
iphone 支持 H.264 视频最高可达 720p,每秒 30 帧,Main Profile level 3.1
来源:H.264的档次和级别。
在iOS7.0以上,可以直接选择AVVideoProfileLevelH264BaselineAutoLevel,之前的版本既然我们是实时直播,那就应该选择Baseline级别的了,根据视频分辨率和比特率,选择编码标准,例如标清视频使用AVVideoProfileLevelH264Baseline30。
Github有一个非常好硬编码封装库,简化了编码设置的工作,建议直接使用VideoToolboxPlus。
原文链接:http://www.jianshu.com/p/2c592daeb3b9