forked from rFlex/SCRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCRecorderTools.m
170 lines (132 loc) · 6.06 KB
/
SCRecorderTools.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// SCRecorderTools.m
// SCRecorder
//
// Created by Simon CORSIN on 24/12/14.
// Copyright (c) 2014 rFlex. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "SCRecorderTools.h"
#define kFULL_HD (1920 x 1080)
#define kHD_READY (1280 x 720)
@implementation SCRecorderTools
+ (BOOL)formatInRange:(AVCaptureDeviceFormat*)format frameRate:(CMTimeScale)frameRate {
CMVideoDimensions dimensions;
dimensions.width = 0;
dimensions.height = 0;
return [SCRecorderTools formatInRange:format frameRate:frameRate dimensions:dimensions];
}
+ (BOOL)formatInRange:(AVCaptureDeviceFormat*)format frameRate:(CMTimeScale)frameRate dimensions:(CMVideoDimensions)dimensions {
CMVideoDimensions size = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
if (size.width >= dimensions.width && size.height >= dimensions.height) {
for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
if (range.minFrameDuration.timescale >= frameRate && range.maxFrameDuration.timescale <= frameRate) {
return YES;
}
}
}
return NO;
}
+ (CMTimeScale)maxFrameRateForFormat:(AVCaptureDeviceFormat *)format minFrameRate:(CMTimeScale)minFrameRate {
CMTimeScale lowerTimeScale = 0;
for (AVFrameRateRange *range in format.videoSupportedFrameRateRanges) {
if (range.minFrameDuration.timescale >= minFrameRate && (lowerTimeScale == 0 || range.minFrameDuration.timescale < lowerTimeScale)) {
lowerTimeScale = range.minFrameDuration.timescale;
}
}
return lowerTimeScale;
}
+ (AVCaptureDevice *)videoDeviceForPosition:(AVCaptureDevicePosition)position {
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in videoDevices) {
if (device.position == (AVCaptureDevicePosition)position) {
return device;
}
}
return nil;
}
+ (NSString *)captureSessionPresetForDimension:(CMVideoDimensions)videoDimension {
if (videoDimension.width >= 1920 && videoDimension.height >= 1080) {
return AVCaptureSessionPreset1920x1080;
}
if (videoDimension.width >= 1280 && videoDimension.height >= 720) {
return AVCaptureSessionPreset1280x720;
}
if (videoDimension.width >= 960 && videoDimension.height >= 540) {
return AVCaptureSessionPresetiFrame960x540;
}
if (videoDimension.width >= 640 && videoDimension.height >= 480) {
return AVCaptureSessionPreset640x480;
}
if (videoDimension.width >= 352 && videoDimension.height >= 288) {
return AVCaptureSessionPreset352x288;
}
return AVCaptureSessionPresetLow;
}
+ (NSString *)bestCaptureSessionPresetForDevicePosition:(AVCaptureDevicePosition)devicePosition withMaxSize:(CGSize)maxSize {
return [SCRecorderTools bestCaptureSessionPresetForDevice:[SCRecorderTools videoDeviceForPosition:devicePosition] withMaxSize:maxSize];
}
+ (NSString *)bestCaptureSessionPresetForDevice:(AVCaptureDevice *)device withMaxSize:(CGSize)maxSize {
CMVideoDimensions highestDeviceDimension;
highestDeviceDimension.width = 0;
highestDeviceDimension.height = 0;
for (AVCaptureDeviceFormat *format in device.formats) {
CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription);
if (dimension.width <= (int)maxSize.width && dimension.height <= (int)maxSize.height && dimension.width * dimension.height > highestDeviceDimension.width * highestDeviceDimension.height) {
highestDeviceDimension = dimension;
}
}
return [SCRecorderTools captureSessionPresetForDimension:highestDeviceDimension];
}
+ (NSString *)bestCaptureSessionPresetCompatibleWithAllDevices {
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
CMVideoDimensions highestCompatibleDimension;
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) {
highestDeviceDimension = dimension;
}
}
if (!lowestSet || (highestCompatibleDimension.width * highestCompatibleDimension.height > highestDeviceDimension.width * highestDeviceDimension.height)) {
lowestSet = YES;
highestCompatibleDimension = highestDeviceDimension;
}
}
return [SCRecorderTools captureSessionPresetForDimension:highestCompatibleDimension];
}
+ (NSArray *)assetWriterMetadata {
AVMutableMetadataItem *creationDate = [AVMutableMetadataItem new];
creationDate.keySpace = AVMetadataKeySpaceCommon;
creationDate.key = AVMetadataCommonKeyCreationDate;
creationDate.value = [[NSDate date] toISO8601];
AVMutableMetadataItem *software = [AVMutableMetadataItem new];
software.keySpace = AVMetadataKeySpaceCommon;
software.key = AVMetadataCommonKeySoftware;
software.value = @"SCRecorder";
return @[software, creationDate];
}
@end
@implementation NSDate (SCRecorderTools)
+ (NSDateFormatter *)_getFormatter {
static NSDateFormatter *dateFormatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
});
return dateFormatter;
}
- (NSString*)toISO8601 {
return [[NSDate _getFormatter] stringFromDate:self];
}
+ (NSDate *)fromISO8601:(NSString *)iso8601 {
return [[NSDate _getFormatter] dateFromString:iso8601];
}
@end