forked from chihsuan/react-native-mov-to-mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
movToMp4.m
61 lines (49 loc) · 2.11 KB
/
movToMp4.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
#import "movToMp4.h"
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetExportSession.h>
#import <AVFoundation/AVMediaFormat.h>
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>
@implementation movToMp4
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(convertMovToMp4: (NSString*)filename
toPath:(NSString*)outputPath
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject
)
{
NSURL *urlFile = [NSURL fileURLWithPath:filename];
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:urlFile options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
NSString * resultPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@.mp4", outputPath];
exportSession.outputURL = [NSURL fileURLWithPath:resultPath];
//set the output file format if you want to make it in other file format (ex .3gp)
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status])
{
case AVAssetExportSessionStatusFailed: {
NSError* error = exportSession.error;
NSString *codeWithDomain = [NSString stringWithFormat:@"E%@%zd", error.domain.uppercaseString, error.code];
reject(codeWithDomain, error.localizedDescription, error);
break;
}
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
case AVAssetExportSessionStatusCompleted:
{
//Video conversion finished
//NSLog(@"Successful!");
resolve(resultPath);
}
break;
default:
break;
}
}];
}
@end