forked from rFlex/SCRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCRecordSessionSegment.m
158 lines (124 loc) · 4.52 KB
/
SCRecordSessionSegment.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
//
// SCRecordSessionSegment.m
// SCRecorder
//
// Created by Simon CORSIN on 10/03/15.
// Copyright (c) 2015 rFlex. All rights reserved.
//
#import "SCRecordSessionSegment.h"
#import "SCRecordSession.h"
@interface SCRecordSessionSegment() {
AVAsset *_asset;
__weak UIImage *_thumbnail;
__weak UIImage *_lastImage;
}
@end
@implementation SCRecordSessionSegment
- (instancetype)initWithDictionaryRepresentation:(NSDictionary *)dictionary directory:(NSString *)directory {
NSString *filename = dictionary[SCRecordSessionSegmentFilenameKey];
NSDictionary *info = dictionary[SCRecordSessionSegmentInfoKey];
if (filename != nil) {
NSURL *url = [SCRecordSessionSegment segmentURLForFilename:filename andDirectory:directory];
return [self initWithURL:url info:info];
}
return nil;
}
- (instancetype)initWithURL:(NSURL *)url info:(NSDictionary *)info {
self = [self init];
if (self) {
_url = url;
_info = info;
}
return self;
}
- (void)deleteFile {
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:_url error:&error];
_url = nil;
_asset = nil;
}
- (AVAsset *)asset {
if (_asset == nil) {
_asset = [AVAsset assetWithURL:_url];
}
return _asset;
}
- (CMTime)duration {
return [self asset].duration;
}
- (UIImage *)thumbnail {
UIImage *image = _thumbnail;
if (image == nil) {
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:self.asset];
imageGenerator.appliesPreferredTrackTransform = YES;
NSError *error = nil;
CGImageRef thumbnailImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:nil error:&error];
if (error == nil) {
image = [UIImage imageWithCGImage:thumbnailImage];
_thumbnail = image;
} else {
NSLog(@"Unable to generate thumbnail for %@: %@", self.url, error.localizedDescription);
}
}
return image;
}
- (UIImage *)lastImage {
UIImage *image = _lastImage;
if (image == nil) {
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:self.asset];
imageGenerator.appliesPreferredTrackTransform = YES;
NSError *error = nil;
CGImageRef lastImage = [imageGenerator copyCGImageAtTime:self.duration actualTime:nil error:&error];
if (error == nil) {
image = [UIImage imageWithCGImage:lastImage];
_lastImage = image;
} else {
NSLog(@"Unable to generate lastImage for %@: %@", self.url, error.localizedDescription);
}
}
return image;
}
- (float)frameRate {
NSArray *tracks = [self.asset tracksWithMediaType:AVMediaTypeVideo];
if (tracks.count == 0) {
return 0;
}
AVAssetTrack *videoTrack = [tracks firstObject];
return videoTrack.nominalFrameRate;
}
- (void)setUrl:(NSURL *)url {
_url = url;
_asset = nil;
}
- (NSDictionary *)dictionaryRepresentation {
if (self.info == nil) {
return @{ SCRecordSessionSegmentFilenameKey : self.url.lastPathComponent };
} else {
return @{
SCRecordSessionSegmentFilenameKey : self.url.lastPathComponent,
SCRecordSessionSegmentInfoKey : self.info
};
}
}
- (BOOL)fileUrlExists {
return [[NSFileManager defaultManager] fileExistsAtPath:self.url.path];
}
+ (NSURL *)segmentURLForFilename:(NSString *)filename andDirectory:(NSString *)directory {
NSURL *directoryUrl = nil;
if ([SCRecordSessionTemporaryDirectory isEqualToString:directory]) {
directoryUrl = [NSURL fileURLWithPath:NSTemporaryDirectory()];
} else if ([SCRecordSessionCacheDirectory isEqualToString:directory]) {
NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
directoryUrl = [NSURL fileURLWithPath:myPathList.firstObject];
} else if ([SCRecordSessionDocumentDirectory isEqualToString:directory]) {
NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
directoryUrl = [NSURL fileURLWithPath:myPathList.firstObject];
} else {
directoryUrl = [NSURL fileURLWithPath:directory];
}
return [directoryUrl URLByAppendingPathComponent:filename];
}
+ (SCRecordSessionSegment *)segmentWithURL:(NSURL *)url info:(NSDictionary *)info {
return [[SCRecordSessionSegment alloc] initWithURL:url info:info];
}
@end