Repository navigation
Expand file tree
/
Copy pathMediaUtils.m
More file actions
167 lines (144 loc) · 6.3 KB
/
Copy pathMediaUtils.m
File metadata and controls
167 lines (144 loc) · 6.3 KB
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
//
// MediaUtils.m
// KeybaseShare
//
// Created by Song Gao on 2/20/20.
// Copyright © 2020 Keybase. All rights reserved.
//
#import "MediaUtils.h"
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
@implementation MediaUtils
+ (CFDictionaryRef) _scaledImageOptions{
return (__bridge CFDictionaryRef) @{
(id) kCGImageSourceCreateThumbnailWithTransform : @YES,
(id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(id) kCGImageSourceThumbnailMaxPixelSize : @(1200),
};
}
+ (NSError *) _scaleDownCGImageSourceRef:(CGImageSourceRef)img dstURL:(NSURL *)dstURL options:(CFDictionaryRef)options {
NSLog(@"dstURL: %@", dstURL);
CGImageRef scaledRef = CGImageSourceCreateThumbnailAtIndex(img, 0, options);
NSData * scaled = UIImageJPEGRepresentation([UIImage imageWithCGImage:scaledRef], 0.85);
CGImageRelease(scaledRef);
BOOL OK = [scaled writeToURL:dstURL atomically:true];
if (!OK) {
return [NSError errorWithDomain:@"MediaUtils" code:1 userInfo:@{NSLocalizedDescriptionKey:@"error writing scaled down image"}];
}
return nil;
}
+ (NSError *) _stripImageExifAtURL:(NSURL*)url {
NSError * error;
CGImageSourceRef cgSource = CGImageSourceCreateWithURL((__bridge CFURLRef)url, nil);
CFStringRef type = CGImageSourceGetType(cgSource);
size_t count = CGImageSourceGetCount(cgSource);
NSURL * tmpDstURL = [url URLByAppendingPathExtension:@"tmp"];
CGImageDestinationRef cgDestination = CGImageDestinationCreateWithURL((__bridge CFURLRef)tmpDstURL, type, count, NULL);
NSDictionary *removeExifProperties = @{(id)kCGImagePropertyExifDictionary: (id)kCFNull,
(id)kCGImagePropertyGPSDictionary : (id)kCFNull};
for (size_t index = 0; index < count; index++) {
CGImageDestinationAddImageFromSource(cgDestination, cgSource, index, (__bridge CFDictionaryRef)removeExifProperties);
}
if (!CGImageDestinationFinalize(cgDestination)) {
CFRelease(cgDestination);
CFRelease(cgSource);
return [NSError errorWithDomain:@"MediaUtils" code:1 userInfo:@{@"message":@"CGImageDestinationFinalize failed"}];
}
CFRelease(cgDestination);
CFRelease(cgSource);
[[NSFileManager defaultManager] replaceItemAtURL:url withItemAtURL:tmpDstURL backupItemName:nil options:0 resultingItemURL:nil error:&error];
return error;
}
+ (void) processImageFromOriginal:(NSURL*)url completion:(ProcessMediaCompletion)completion {
NSError * error = [MediaUtils _stripImageExifAtURL:url];
if (error != nil){
completion(error, nil);
return;
}
NSString * basename = [[url URLByDeletingPathExtension] lastPathComponent];
NSURL * parent = [url URLByDeletingLastPathComponent];
NSURL * scaledURL = [parent URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.scaled.jpg", basename]];
CGImageSourceRef cgSource = CGImageSourceCreateWithURL((__bridge CFURLRef)url, nil);
error = [MediaUtils _scaleDownCGImageSourceRef:cgSource dstURL:scaledURL options:[MediaUtils _scaledImageOptions]];
if (error != nil) {
CFRelease(cgSource);
completion(error, nil);
return;
}
CFRelease(cgSource);
completion(nil, scaledURL);
}
+ (BOOL) _needScaleDownAsset:(AVURLAsset *) asset {
NSArray<AVAssetTrack *> * tracks = [asset tracks];
if (tracks != nil){
for (int i = 0; i < tracks.count; ++i) {
CGSize size = [tracks[i] naturalSize];
if (size.height * size.width > 640*480) {
return true;
}
}
}
return false;
}
+ (void) _stripVideoExifAndCompleteAsset:(AVURLAsset *) asset originalURL:(NSURL *)originalURL completion:(ProcessMediaCompletionErrorOnly)completion {
NSURL * tmpDstURL = [originalURL URLByAppendingPathExtension:@"tmp"];
AVAssetExportSession * exportSessionOriginal = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSessionOriginal.shouldOptimizeForNetworkUse = true;
exportSessionOriginal.outputFileType = AVFileTypeMPEG4;
exportSessionOriginal.outputURL = tmpDstURL;
[exportSessionOriginal exportAsynchronouslyWithCompletionHandler:^{
if (exportSessionOriginal.error != nil) {
completion(exportSessionOriginal.error);
return;
}
NSError * error;
[[NSFileManager defaultManager] replaceItemAtURL:originalURL withItemAtURL:tmpDstURL backupItemName:nil options:0 resultingItemURL:nil error:&error];
completion(error);
}];
}
+ (void) processVideoFromOriginal:(NSURL*)url completion:(ProcessMediaCompletion)completion {
NSString * basename = [[url URLByDeletingPathExtension] lastPathComponent];
NSURL * parent = [url URLByDeletingLastPathComponent];
NSURL * normalVideoURL = [parent URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.scaled.mp4", basename]];
NSURL * thumbnailURL = [parent URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.thumbnail.jpg", basename]];
NSError * error;
AVURLAsset * asset = [[AVURLAsset alloc] initWithURL:url options:nil];
CMTime time = CMTimeMake(1, 1);
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset];
[generateImg setAppliesPreferredTrackTransform:YES];
CGImageRef cgOriginal = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error];
if (error != nil) {
CFRelease(cgOriginal);
completion(error, nil);
return;
}
BOOL needScaleDown = [MediaUtils _needScaleDownAsset:asset];
if (!needScaleDown) {
[MediaUtils _stripVideoExifAndCompleteAsset:asset originalURL:url completion:^(NSError * _Nullable error) {
if (error != nil) {
completion(error, nil);
} else {
completion(nil, url);
}
}];
return;
}
AVAssetExportSession * exportSessionNormal = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
exportSessionNormal.shouldOptimizeForNetworkUse = true;
exportSessionNormal.outputFileType = AVFileTypeMPEG4;
exportSessionNormal.outputURL = normalVideoURL;
[exportSessionNormal exportAsynchronouslyWithCompletionHandler:^{
if (exportSessionNormal.error != nil) {
completion(exportSessionNormal.error, nil);
return;
}
[MediaUtils _stripVideoExifAndCompleteAsset:asset originalURL:url completion:^(NSError * _Nullable error) {
if (error != nil) {
completion(error, nil);
} else {
completion(nil, normalVideoURL);
}
}];
}];
}
@end