Skip to content

Commit

Permalink
-SCRecorder没有对1080P以上的相机更新。但它的扩展性很好。可以在不影响其代码的情况下对1080P以上的相机兼容。
Browse files Browse the repository at this point in the history
  • Loading branch information
lincf0912 committed Apr 16, 2020
1 parent 0e69584 commit f475125
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

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

/* Begin PBXFileReference section */
1A6A91C38ED318A2C9956E69 /* libPods-LFCameraPickerControllerDEMO.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-LFCameraPickerControllerDEMO.a"; sourceTree = BUILT_PRODUCTS_DIR; };
3B5E9F07244845C10069E1C8 /* LFCameraRecorderTools.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LFCameraRecorderTools.h; sourceTree = "<group>"; };
3B5E9F08244845C10069E1C8 /* LFCameraRecorderTools.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LFCameraRecorderTools.m; sourceTree = "<group>"; };
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>"; };
6F05EE241EC44B1C006FC1BF /* LFCameraPickerControllerDEMO.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LFCameraPickerControllerDEMO.app; sourceTree = BUILT_PRODUCTS_DIR; };
6F05EE281EC44B1C006FC1BF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -88,6 +91,15 @@
name = Pods;
sourceTree = "<group>";
};
3B5E9F06244845AB0069E1C8 /* Tools */ = {
isa = PBXGroup;
children = (
3B5E9F07244845C10069E1C8 /* LFCameraRecorderTools.h */,
3B5E9F08244845C10069E1C8 /* LFCameraRecorderTools.m */,
);
path = Tools;
sourceTree = "<group>";
};
6F05EE1B1EC44B1C006FC1BF = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -146,6 +158,7 @@
6FAA50591ED2BB30002D88A2 /* class */ = {
isa = PBXGroup;
children = (
3B5E9F06244845AB0069E1C8 /* Tools */,
6FAA505A1ED2BB30002D88A2 /* Category */,
6FAA505E1ED2BB30002D88A2 /* Controller */,
6F359B031F1C4D8C009F2035 /* view */,
Expand Down Expand Up @@ -367,6 +380,7 @@
6FAA50711ED2BB30002D88A2 /* LFCameraPickerController.m in Sources */,
6F05EE2F1EC44B1C006FC1BF /* ViewController.m in Sources */,
6F9F32A21F7CC1B3004B6438 /* LFCameraPlayerView.m in Sources */,
3B5E9F09244845C10069E1C8 /* LFCameraRecorderTools.m in Sources */,
6FAA506E1ED2BB30002D88A2 /* LFCameraTakeViewController.m in Sources */,
6F359B061F1C4D8C009F2035 /* LFCameraWatermarkOverlayView.m in Sources */,
6FAA506F1ED2BB30002D88A2 /* LFCameraHeader.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#import "LFCameraDisplayController.h"
#import "LFCameraWatermarkOverlayView.h"

#import "LFCameraRecorderTools.h"

#import "UIImage+LFCamera_Orientation.h"

#import "LFRecordButton.h"
Expand Down Expand Up @@ -742,7 +744,7 @@ - (void)initRecorder

_recorder = [SCRecorder recorder];
if ([cameraPicker.cameraPreset isEqualToString:AVCaptureSessionPresetAuto]) {
_recorder.captureSessionPreset = [SCRecorderTools bestCaptureSessionPresetCompatibleWithAllDevices];
_recorder.captureSessionPreset = [LFCameraRecorderTools bestCaptureSessionPresetCompatibleWithAllDevices:(CMTimeScale)cameraPicker.framerate];
} else {
_recorder.captureSessionPreset = cameraPicker.cameraPreset;
}
Expand Down Expand Up @@ -775,7 +777,8 @@ - (void)initRecorder

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


UIView *previewView = self.previewView;
Expand Down Expand Up @@ -803,7 +806,8 @@ - (void)initRecorder

NSError *error;

[_recorder setActiveFormatWithFrameRate:(CMTimeScale)cameraPicker.framerate error:&error];
CMVideoDimensions videoDimensions = [LFCameraRecorderTools bestVideoDimensionsWithAllDevices:(CMTimeScale)cameraPicker.framerate];
[_recorder setActiveFormatWithFrameRate:(CMTimeScale)cameraPicker.framerate width:videoDimensions.width andHeight:videoDimensions.height error:&error];
if (error) {
NSLog(@"set frameRate error: %@", error.localizedDescription);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// LFCameraRecorderTools.h
// LFCameraPickerControllerDEMO
//
// Created by TsanFeng Lam on 2020/4/16.
// Copyright © 2020 LamTsanFeng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LFCameraRecorderTools : NSObject

/**
Returns the best session preset that is compatible with all available video
devices (front and back camera). It will ensure that buffer output from
both camera has the same resolution.
*/
+ (NSString *__nonnull)bestCaptureSessionPresetCompatibleWithAllDevices:(CMTimeScale)frameRate;

+ (CMVideoDimensions)bestVideoDimensionsWithAllDevices:(CMTimeScale)frameRate;


+ (UInt64)bitrateWithCaptureSessionPreset:(NSString *)preset;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// LFCameraRecorderTools.m
// LFCameraPickerControllerDEMO
//
// Created by TsanFeng Lam on 2020/4/16.
// Copyright © 2020 LamTsanFeng. All rights reserved.
//

#import "LFCameraRecorderTools.h"

@implementation LFCameraRecorderTools

+ (NSString *)captureSessionPresetForDimension:(CMVideoDimensions)videoDimension {

if (@available(iOS 9.0, *)) {
if (videoDimension.width >= 3840 && videoDimension.height >= 2160) {
return AVCaptureSessionPreset3840x2160;
}
}
if (videoDimension.width >= 1920 && videoDimension.height >= 1080) {
return AVCaptureSessionPreset1920x1080;
}
if (videoDimension.width >= 1280 && videoDimension.height >= 720) {
return AVCaptureSessionPreset1280x720;
}
if (videoDimension.width >= 640 && videoDimension.height >= 480) {
return AVCaptureSessionPreset640x480;
}
if (videoDimension.width >= 352 && videoDimension.height >= 288) {
return AVCaptureSessionPreset352x288;
}

return AVCaptureSessionPresetHigh;
}

+ (CMVideoDimensions)bestVideoDimensionsWithAllDevices:(CMTimeScale)frameRate {
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

CMVideoDimensions highestCompatibleDimension = (CMVideoDimensions){0, 0};
BOOL lowestSet = NO;

for (AVCaptureDevice *device in videoDevices) {
CMVideoDimensions highestDeviceDimension;
highestDeviceDimension.width = 0;
highestDeviceDimension.height = 0;

for (AVCaptureDeviceFormat *format in device.formats) {
CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription);

if (dimension.width * dimension.height > highestDeviceDimension.width * highestDeviceDimension.height) {
for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
if (range.minFrameDuration.timescale >= frameRate && range.maxFrameDuration.timescale <= frameRate) {
highestDeviceDimension = dimension;
}
}
}
}

if (!lowestSet || (highestCompatibleDimension.width * highestCompatibleDimension.height > highestDeviceDimension.width * highestDeviceDimension.height)) {
lowestSet = YES;
highestCompatibleDimension = highestDeviceDimension;
}

}
return highestCompatibleDimension;
}

+ (NSString *)bestCaptureSessionPresetCompatibleWithAllDevices:(CMTimeScale)frameRate
{
CMVideoDimensions highestCompatibleDimension = [self bestVideoDimensionsWithAllDevices:frameRate];

return [self captureSessionPresetForDimension:highestCompatibleDimension];
}

+ (UInt64)bitrateWithCaptureSessionPreset:(NSString *)preset
{
if (@available(iOS 9.0, *)) {
if ([preset isEqualToString:AVCaptureSessionPreset3840x2160]) {
return 31000000;
}
}
if ([preset isEqualToString:AVCaptureSessionPreset1920x1080]) {
return 7900000;
}
if ([preset isEqualToString:AVCaptureSessionPreset1280x720]) {
return 3500000;
}
if ([preset isEqualToString:AVCaptureSessionPreset640x480]) {
return 1200000;
}
if ([preset isEqualToString:AVCaptureSessionPreset352x288]) {
return 770000;
}

return 3500000;
}

@end

0 comments on commit f475125

Please sign in to comment.