Skip to content

Commit f475125

Browse files
committed
-SCRecorder没有对1080P以上的相机更新。但它的扩展性很好。可以在不影响其代码的情况下对1080P以上的相机兼容。
1 parent 0e69584 commit f475125

4 files changed

Lines changed: 148 additions & 3 deletions

File tree

LFCameraPickerControllerDEMO/LFCameraPickerControllerDEMO.xcodeproj/project.pbxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
3B5E9F09244845C10069E1C8 /* LFCameraRecorderTools.m in Sources */ = {isa = PBXBuildFile; fileRef = 3B5E9F08244845C10069E1C8 /* LFCameraRecorderTools.m */; };
1011
6F05EE291EC44B1C006FC1BF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 6F05EE281EC44B1C006FC1BF /* main.m */; };
1112
6F05EE2C1EC44B1C006FC1BF /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6F05EE2B1EC44B1C006FC1BF /* AppDelegate.m */; };
1213
6F05EE2F1EC44B1C006FC1BF /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6F05EE2E1EC44B1C006FC1BF /* ViewController.m */; };
@@ -30,6 +31,8 @@
3031

3132
/* Begin PBXFileReference section */
3233
1A6A91C38ED318A2C9956E69 /* libPods-LFCameraPickerControllerDEMO.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-LFCameraPickerControllerDEMO.a"; sourceTree = BUILT_PRODUCTS_DIR; };
34+
3B5E9F07244845C10069E1C8 /* LFCameraRecorderTools.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LFCameraRecorderTools.h; sourceTree = "<group>"; };
35+
3B5E9F08244845C10069E1C8 /* LFCameraRecorderTools.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LFCameraRecorderTools.m; sourceTree = "<group>"; };
3336
424631D3E6B6637347970A73 /* Pods-LFCameraPickerControllerDEMO.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LFCameraPickerControllerDEMO.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-LFCameraPickerControllerDEMO/Pods-LFCameraPickerControllerDEMO.debug.xcconfig"; sourceTree = "<group>"; };
3437
6F05EE241EC44B1C006FC1BF /* LFCameraPickerControllerDEMO.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LFCameraPickerControllerDEMO.app; sourceTree = BUILT_PRODUCTS_DIR; };
3538
6F05EE281EC44B1C006FC1BF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -88,6 +91,15 @@
8891
name = Pods;
8992
sourceTree = "<group>";
9093
};
94+
3B5E9F06244845AB0069E1C8 /* Tools */ = {
95+
isa = PBXGroup;
96+
children = (
97+
3B5E9F07244845C10069E1C8 /* LFCameraRecorderTools.h */,
98+
3B5E9F08244845C10069E1C8 /* LFCameraRecorderTools.m */,
99+
);
100+
path = Tools;
101+
sourceTree = "<group>";
102+
};
91103
6F05EE1B1EC44B1C006FC1BF = {
92104
isa = PBXGroup;
93105
children = (
@@ -146,6 +158,7 @@
146158
6FAA50591ED2BB30002D88A2 /* class */ = {
147159
isa = PBXGroup;
148160
children = (
161+
3B5E9F06244845AB0069E1C8 /* Tools */,
149162
6FAA505A1ED2BB30002D88A2 /* Category */,
150163
6FAA505E1ED2BB30002D88A2 /* Controller */,
151164
6F359B031F1C4D8C009F2035 /* view */,
@@ -367,6 +380,7 @@
367380
6FAA50711ED2BB30002D88A2 /* LFCameraPickerController.m in Sources */,
368381
6F05EE2F1EC44B1C006FC1BF /* ViewController.m in Sources */,
369382
6F9F32A21F7CC1B3004B6438 /* LFCameraPlayerView.m in Sources */,
383+
3B5E9F09244845C10069E1C8 /* LFCameraRecorderTools.m in Sources */,
370384
6FAA506E1ED2BB30002D88A2 /* LFCameraTakeViewController.m in Sources */,
371385
6F359B061F1C4D8C009F2035 /* LFCameraWatermarkOverlayView.m in Sources */,
372386
6FAA506F1ED2BB30002D88A2 /* LFCameraHeader.m in Sources */,

LFCameraPickerControllerDEMO/LFCameraPickerControllerDEMO/class/Controller/LFCameraTakeViewController.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#import "LFCameraDisplayController.h"
1313
#import "LFCameraWatermarkOverlayView.h"
1414

15+
#import "LFCameraRecorderTools.h"
16+
1517
#import "UIImage+LFCamera_Orientation.h"
1618

1719
#import "LFRecordButton.h"
@@ -742,7 +744,7 @@ - (void)initRecorder
742744

743745
_recorder = [SCRecorder recorder];
744746
if ([cameraPicker.cameraPreset isEqualToString:AVCaptureSessionPresetAuto]) {
745-
_recorder.captureSessionPreset = [SCRecorderTools bestCaptureSessionPresetCompatibleWithAllDevices];
747+
_recorder.captureSessionPreset = [LFCameraRecorderTools bestCaptureSessionPresetCompatibleWithAllDevices:(CMTimeScale)cameraPicker.framerate];
746748
} else {
747749
_recorder.captureSessionPreset = cameraPicker.cameraPreset;
748750
}
@@ -775,7 +777,8 @@ - (void)initRecorder
775777

776778
_recorder.delegate = self;
777779
// _recorder.autoSetVideoOrientation = YES; //YES causes bad orientation for video from camera roll
778-
// _recorder.videoConfiguration.size = CGSizeMake(640, 480);
780+
_recorder.videoConfiguration.profileLevel = AVVideoProfileLevelH264HighAutoLevel;
781+
_recorder.videoConfiguration.bitrate = [LFCameraRecorderTools bitrateWithCaptureSessionPreset:_recorder.captureSessionPreset];
779782

780783

781784
UIView *previewView = self.previewView;
@@ -803,7 +806,8 @@ - (void)initRecorder
803806

804807
NSError *error;
805808

806-
[_recorder setActiveFormatWithFrameRate:(CMTimeScale)cameraPicker.framerate error:&error];
809+
CMVideoDimensions videoDimensions = [LFCameraRecorderTools bestVideoDimensionsWithAllDevices:(CMTimeScale)cameraPicker.framerate];
810+
[_recorder setActiveFormatWithFrameRate:(CMTimeScale)cameraPicker.framerate width:videoDimensions.width andHeight:videoDimensions.height error:&error];
807811
if (error) {
808812
NSLog(@"set frameRate error: %@", error.localizedDescription);
809813
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// LFCameraRecorderTools.h
3+
// LFCameraPickerControllerDEMO
4+
//
5+
// Created by TsanFeng Lam on 2020/4/16.
6+
// Copyright © 2020 LamTsanFeng. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <AVFoundation/AVFoundation.h>
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
@interface LFCameraRecorderTools : NSObject
15+
16+
/**
17+
Returns the best session preset that is compatible with all available video
18+
devices (front and back camera). It will ensure that buffer output from
19+
both camera has the same resolution.
20+
*/
21+
+ (NSString *__nonnull)bestCaptureSessionPresetCompatibleWithAllDevices:(CMTimeScale)frameRate;
22+
23+
+ (CMVideoDimensions)bestVideoDimensionsWithAllDevices:(CMTimeScale)frameRate;
24+
25+
26+
+ (UInt64)bitrateWithCaptureSessionPreset:(NSString *)preset;
27+
@end
28+
29+
NS_ASSUME_NONNULL_END
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// LFCameraRecorderTools.m
3+
// LFCameraPickerControllerDEMO
4+
//
5+
// Created by TsanFeng Lam on 2020/4/16.
6+
// Copyright © 2020 LamTsanFeng. All rights reserved.
7+
//
8+
9+
#import "LFCameraRecorderTools.h"
10+
11+
@implementation LFCameraRecorderTools
12+
13+
+ (NSString *)captureSessionPresetForDimension:(CMVideoDimensions)videoDimension {
14+
15+
if (@available(iOS 9.0, *)) {
16+
if (videoDimension.width >= 3840 && videoDimension.height >= 2160) {
17+
return AVCaptureSessionPreset3840x2160;
18+
}
19+
}
20+
if (videoDimension.width >= 1920 && videoDimension.height >= 1080) {
21+
return AVCaptureSessionPreset1920x1080;
22+
}
23+
if (videoDimension.width >= 1280 && videoDimension.height >= 720) {
24+
return AVCaptureSessionPreset1280x720;
25+
}
26+
if (videoDimension.width >= 640 && videoDimension.height >= 480) {
27+
return AVCaptureSessionPreset640x480;
28+
}
29+
if (videoDimension.width >= 352 && videoDimension.height >= 288) {
30+
return AVCaptureSessionPreset352x288;
31+
}
32+
33+
return AVCaptureSessionPresetHigh;
34+
}
35+
36+
+ (CMVideoDimensions)bestVideoDimensionsWithAllDevices:(CMTimeScale)frameRate {
37+
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
38+
39+
CMVideoDimensions highestCompatibleDimension = (CMVideoDimensions){0, 0};
40+
BOOL lowestSet = NO;
41+
42+
for (AVCaptureDevice *device in videoDevices) {
43+
CMVideoDimensions highestDeviceDimension;
44+
highestDeviceDimension.width = 0;
45+
highestDeviceDimension.height = 0;
46+
47+
for (AVCaptureDeviceFormat *format in device.formats) {
48+
CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
49+
50+
if (dimension.width * dimension.height > highestDeviceDimension.width * highestDeviceDimension.height) {
51+
for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
52+
if (range.minFrameDuration.timescale >= frameRate && range.maxFrameDuration.timescale <= frameRate) {
53+
highestDeviceDimension = dimension;
54+
}
55+
}
56+
}
57+
}
58+
59+
if (!lowestSet || (highestCompatibleDimension.width * highestCompatibleDimension.height > highestDeviceDimension.width * highestDeviceDimension.height)) {
60+
lowestSet = YES;
61+
highestCompatibleDimension = highestDeviceDimension;
62+
}
63+
64+
}
65+
return highestCompatibleDimension;
66+
}
67+
68+
+ (NSString *)bestCaptureSessionPresetCompatibleWithAllDevices:(CMTimeScale)frameRate
69+
{
70+
CMVideoDimensions highestCompatibleDimension = [self bestVideoDimensionsWithAllDevices:frameRate];
71+
72+
return [self captureSessionPresetForDimension:highestCompatibleDimension];
73+
}
74+
75+
+ (UInt64)bitrateWithCaptureSessionPreset:(NSString *)preset
76+
{
77+
if (@available(iOS 9.0, *)) {
78+
if ([preset isEqualToString:AVCaptureSessionPreset3840x2160]) {
79+
return 31000000;
80+
}
81+
}
82+
if ([preset isEqualToString:AVCaptureSessionPreset1920x1080]) {
83+
return 7900000;
84+
}
85+
if ([preset isEqualToString:AVCaptureSessionPreset1280x720]) {
86+
return 3500000;
87+
}
88+
if ([preset isEqualToString:AVCaptureSessionPreset640x480]) {
89+
return 1200000;
90+
}
91+
if ([preset isEqualToString:AVCaptureSessionPreset352x288]) {
92+
return 770000;
93+
}
94+
95+
return 3500000;
96+
}
97+
98+
@end

0 commit comments

Comments
 (0)