说在最前面!
感谢开源!!!
做过直播的朋友们肯定不会不熟悉来疯的这个开源框架 LFLiveKit
通过它我们可以通过简单的几行代码就能进行高清的视频直播推流。如果自己写我觉得我得改行了。
不过。。。我们老板要求手机能直播1080p 🤷♂️。。
好吧 那就翻代码呗
先找到这个文件LFLiveVideoConfiguration
有关视频推送配置都在这个文件中
先在LFLiveVideoConfiguration.h
文件中加个枚举值方便后面调用
- 视频分辨率枚举 主要加了
LFCaptureSessionPreset1080x1920
这个枚举
/// 视频分辨率(都是16:9 当此设备不支持当前分辨率,自动降低一级)
typedef NS_ENUM (NSUInteger, LFLiveVideoSessionPreset){
/// 低分辨率
LFCaptureSessionPreset360x640 = 0,
/// 中分辨率
LFCaptureSessionPreset540x960 = 1,
/// 高分辨率
LFCaptureSessionPreset720x1280 = 2,
/// 1080p分辨率
LFCaptureSessionPreset1080x1920 = 3,
// 4 : 3
LFCaotureSessionPreset480x640 = 4,
};
- 视频分辨率详细配置枚举 加了这个枚举
LFLiveVideoQuality_High4
/// 视频质量
typedef NS_ENUM (NSUInteger, LFLiveVideoQuality){
/// 分辨率: 360 *640 帧数:15 码率:500Kps
LFLiveVideoQuality_Low1 = 0,
/// 分辨率: 360 *640 帧数:24 码率:800Kps
LFLiveVideoQuality_Low2 = 1,
/// 分辨率: 360 *640 帧数:30 码率:800Kps
LFLiveVideoQuality_Low3 = 2,
/// 分辨率: 540 *960 帧数:15 码率:800Kps
LFLiveVideoQuality_Medium1 = 3,
/// 分辨率: 540 *960 帧数:24 码率:800Kps
LFLiveVideoQuality_Medium2 = 4,
/// 分辨率: 540 *960 帧数:30 码率:800Kps
LFLiveVideoQuality_Medium3 = 5,
/// 分辨率: 720 *1280 帧数:15 码率:1000Kps
LFLiveVideoQuality_High1 = 6,
/// 分辨率: 720 *1280 帧数:24 码率:1200Kps
LFLiveVideoQuality_High2 = 7,
/// 分辨率: 720 *1280 帧数:30 码率:1200Kps
LFLiveVideoQuality_High3 = 8,
/// 分辨率: 1080 * 1920 帧数:24 码率:1200Kps
LFLiveVideoQuality_High4 = 9,
/// 分辨率: 480 * 640 4 : 3
LFLiveVideoQuality_Medium4 = 10,
/// 默认配置
LFLiveVideoQuality_Default = LFLiveVideoQuality_Low2
};
在LFLiveVideoConfiguration.m 中 修改代码
先到方法 + (instancetype)defaultConfigurationForQuality:(LFLiveVideoQuality)videoQuality outputImageOrientation:(UIInterfaceOrientation)outputImageOrientation;
中加入 LFLiveVideoQuality_High4
这个枚举的case
case LFLiveVideoQuality_High4: {
configuration.sessionPreset = LFCaptureSessionPreset1080x1920;// 增加
configuration.videoFrameRate = 24;//帧数 越高越流畅 打过fps游戏的都懂
configuration.videoMaxFrameRate = 24;
configuration.videoMinFrameRate = 15;
configuration.videoBitRate = 1400 * 1000; //bit率越高越清晰 为了手机不是特别烫 我这设的不是很高
configuration.videoMaxBitRate = 2160 * 1000;
configuration.videoMinBitRate = 500 * 1000;
configuration.videoSize = CGSizeMake(1080, 1920);// size 这里决定输出时视频尺寸的大小
}
break;
接着设置一下手机的采集率
在这个方法中- (NSString *)avSessionPreset
增加case
case LFCaptureSessionPreset1080x1920:{////
avSessionPreset = AVCaptureSessionPreset1920x1080;
}
break;
其他还需要修改以下方法
#pragma mark -- Custom Method
- (LFLiveVideoSessionPreset)supportSessionPreset:(LFLiveVideoSessionPreset)sessionPreset {
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *inputCamera;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices){
if ([device position] == AVCaptureDevicePositionFront){
inputCamera = device;
}
}
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:inputCamera error:nil];
if ([session canAddInput:videoInput]){
[session addInput:videoInput];
}
if (![session canSetSessionPreset:self.avSessionPreset]) {
if (sessionPreset == LFCaptureSessionPreset1080x1920) { //====增加
sessionPreset = LFCaptureSessionPreset720x1280; //====增加
} else if (sessionPreset == LFCaptureSessionPreset720x1280) {
sessionPreset = LFCaptureSessionPreset540x960;
if (![session canSetSessionPreset:self.avSessionPreset]) {
sessionPreset = LFCaptureSessionPreset360x640;
}
} else if (sessionPreset == LFCaptureSessionPreset540x960) {
sessionPreset = LFCaptureSessionPreset360x640;
} else {
sessionPreset = LFCaotureSessionPreset480x640;
}
}
return sessionPreset;
}
- (CGSize)captureOutVideoSize{
CGSize videoSize = CGSizeZero;
switch (_sessionPreset) {
case LFCaptureSessionPreset360x640:{
videoSize = CGSizeMake(360, 640);
}
break;
case LFCaptureSessionPreset540x960:{
videoSize = CGSizeMake(540, 960);
}
break;
case LFCaptureSessionPreset720x1280:{
videoSize = CGSizeMake(720, 1280);
}
break;
case LFCaptureSessionPreset1080x1920: { // ====增加
videoSize = CGSizeMake(1080, 1920);
}
break;
case LFCaotureSessionPreset480x640: {
videoSize = CGSizeMake(640, 480);
}
break;
default:{
videoSize = CGSizeMake(360, 640);
}
break;
}
if (self.landscape){
return CGSizeMake(videoSize.height, videoSize.width);
}
return videoSize;
}
网友评论