-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathRNMediaMeta.m
107 lines (93 loc) · 3.7 KB
/
RNMediaMeta.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
#import "RNMediaMeta.h"
static NSArray *metadatas;
@implementation RNMediaMeta
- (NSArray *)metadatas
{
if (!metadatas) {
metadatas = @[
@"albumName",
@"artist",
@"comment",
@"copyrights",
@"creationDate",
@"date",
@"encodedby",
@"genre",
@"language",
@"location",
@"lastModifiedDate",
@"performer",
@"publisher",
@"title"
];
}
return metadatas;
}
RCT_EXPORT_MODULE()
- (dispatch_queue_t)methodQueue
{
return dispatch_queue_create("com.mybigday.rn.MediaMetaQueue", DISPATCH_QUEUE_SERIAL);
}
RCT_EXPORT_METHOD(get:(NSString *)path
withOptions:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
if (![fileManager fileExistsAtPath:path isDirectory:&isDir] || isDir){
NSError *err = [NSError errorWithDomain:@"file not found" code:-15 userInfo:nil];
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
return;
}
NSMutableDictionary *result = [NSMutableDictionary new];
NSDictionary *assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:path] options:assetOptions];
NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
// string keys
for (NSString *key in [self metadatas]) {
NSArray *items = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
withKey:key
keySpace:AVMetadataKeySpaceCommon];
for (AVMetadataItem *item in items) {
[result setObject:item.value forKey:key];
}
}
if (options[@"getThumb"]) {
UIImage *thumbnail;
NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
withKey:AVMetadataCommonKeyArtwork
keySpace:AVMetadataKeySpaceCommon];
// artwork thumb
for (AVMetadataItem *item in artworks) {
thumbnail = [UIImage imageWithData:item.value];
}
if (thumbnail) {
[result setObject:@(thumbnail.size.width) forKey:@"width"];
[result setObject:@(thumbnail.size.height) forKey:@"height"];
NSString *data = [UIImagePNGRepresentation(thumbnail) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
[result setObject:data
forKey:@"thumb"];
}
// video frame thumb
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
CMTime duration = [asset duration];
CMTime time = CMTimeMake(duration.value / 2, duration.timescale);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
thumbnail = [UIImage imageWithCGImage:imageRef];
if (thumbnail) {
[result setObject:@(thumbnail.size.width) forKey:@"width"];
[result setObject:@(thumbnail.size.height) forKey:@"height"];
[result setObject:@((CMTimeGetSeconds(asset.duration) * 1000)) forKey:@"duration"];
NSString *data = [UIImagePNGRepresentation(thumbnail) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
[result setObject:data
forKey:@"thumb"];
}
CGImageRelease(imageRef);
}
resolve(result);
}];
}
@end