Skip to content

Commit

Permalink
优化视频导出方法,可以设置presetName
Browse files Browse the repository at this point in the history
  • Loading branch information
banchichen committed Nov 15, 2017
1 parent 88b5f37 commit 1ab7b5a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@
- (void)getVideoWithAsset:(id)asset completion:(void (^)(AVPlayerItem * playerItem, NSDictionary * info))completion;
- (void)getVideoWithAsset:(id)asset progressHandler:(void (^)(double progress, NSError *error, BOOL *stop, NSDictionary *info))progressHandler completion:(void (^)(AVPlayerItem *, NSDictionary *))completion;

/// Export video 导出视频
/// Export video 导出视频 presetName: 预设名字,默认值是AVAssetExportPreset640x480
- (void)getVideoOutputPathWithAsset:(id)asset success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure;
- (void)getVideoOutputPathWithAsset:(id)asset presetName:(NSString *)presetName success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure;
/// Deprecated, Use -getVideoOutputPathWithAsset:failure:success:
- (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion __attribute__((deprecated("Use -getVideoOutputPathWithAsset:failure:success:")));

Expand Down
19 changes: 14 additions & 5 deletions TZImagePickerController/TZImagePickerController/TZImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,10 @@ - (void)getVideoWithAsset:(id)asset progressHandler:(void (^)(double progress, N

/// Export Video / 导出视频
- (void)getVideoOutputPathWithAsset:(id)asset success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure {
[self getVideoOutputPathWithAsset:asset presetName:AVAssetExportPreset640x480 success:success failure:failure];
}

- (void)getVideoOutputPathWithAsset:(id)asset presetName:(NSString *)presetName success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure {
if ([asset isKindOfClass:[PHAsset class]]) {
PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
Expand All @@ -724,12 +728,12 @@ - (void)getVideoOutputPathWithAsset:(id)asset success:(void (^)(NSString *output
// NSLog(@"Info:\n%@",info);
AVURLAsset *videoAsset = (AVURLAsset*)avasset;
// NSLog(@"AVAsset URL: %@",myAsset.URL);
[self startExportVideoWithVideoAsset:videoAsset success:success failure:failure];
[self startExportVideoWithVideoAsset:videoAsset presetName:presetName success:success failure:failure];
}];
} else if ([asset isKindOfClass:[ALAsset class]]) {
NSURL *videoURL =[asset valueForProperty:ALAssetPropertyAssetURL]; // ALAssetPropertyURLs
AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
[self startExportVideoWithVideoAsset:videoAsset success:success failure:failure];
[self startExportVideoWithVideoAsset:videoAsset presetName:presetName success:success failure:failure];
}
}

Expand All @@ -738,16 +742,16 @@ - (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *out
[self getVideoOutputPathWithAsset:asset success:completion failure:nil];
}

- (void)startExportVideoWithVideoAsset:(AVURLAsset *)videoAsset success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure {
- (void)startExportVideoWithVideoAsset:(AVURLAsset *)videoAsset presetName:(NSString *)presetName success:(void (^)(NSString *outputPath))success failure:(void (^)(NSString *errorMessage, NSError *error))failure {
// Find compatible presets by video asset.
NSArray *presets = [AVAssetExportSession exportPresetsCompatibleWithAsset:videoAsset];

// Begin to compress video
// Now we just compress to low resolution if it supports
// If you need to upload to the server, but server does't support to upload by streaming,
// You can compress the resolution to lower. Or you can support more higher resolution.
if ([presets containsObject:AVAssetExportPreset640x480]) {
AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:videoAsset presetName:AVAssetExportPreset640x480];
if ([presets containsObject:presetName]) {
AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:videoAsset presetName:presetName];

NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss-SSS"];
Expand Down Expand Up @@ -816,6 +820,11 @@ - (void)startExportVideoWithVideoAsset:(AVURLAsset *)videoAsset success:(void (^
}
});
}];
} else {
if (failure) {
NSString *errorMessage = [NSString stringWithFormat:@"当前设备不支持该预设:%@", presetName];
failure(errorMessage, nil);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion TZImagePickerController/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ - (void)imagePickerController:(TZImagePickerController *)picker didFinishPicking
_selectedPhotos = [NSMutableArray arrayWithArray:@[coverImage]];
_selectedAssets = [NSMutableArray arrayWithArray:@[asset]];
// open this code to send video / 打开这段代码发送视频
[[TZImageManager manager] getVideoOutputPathWithAsset:asset success:^(NSString *outputPath) {
[[TZImageManager manager] getVideoOutputPathWithAsset:asset presetName:AVAssetExportPreset640x480 success:^(NSString *outputPath) {
NSLog(@"视频导出到本地完成,沙盒路径为:%@",outputPath);
// Export completed, send video here, send by outputPath or NSData
// 导出完成,在这里写上传代码,通过路径或者通过NSData上传
Expand Down

0 comments on commit 1ab7b5a

Please sign in to comment.