Commit 19724816 authored by banchichen's avatar banchichen

新增iOS8以下的视频导出代码

parent 5acab807
...@@ -491,7 +491,7 @@ static CGFloat TZScreenScale; ...@@ -491,7 +491,7 @@ static CGFloat TZScreenScale;
#pragma mark - Export video #pragma mark - Export video
/// Export Video / 导出视频,暂仅仅支持iOS8.0+ /// Export Video / 导出视频
- (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion { - (void)getVideoOutputPathWithAsset:(id)asset completion:(void (^)(NSString *outputPath))completion {
if ([asset isKindOfClass:[PHAsset class]]) { if ([asset isKindOfClass:[PHAsset class]]) {
PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init]; PHVideoRequestOptions* options = [[PHVideoRequestOptions alloc] init];
...@@ -500,45 +500,67 @@ static CGFloat TZScreenScale; ...@@ -500,45 +500,67 @@ static CGFloat TZScreenScale;
options.networkAccessAllowed = YES; options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset* avasset, AVAudioMix* audioMix, NSDictionary* info){ [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset* avasset, AVAudioMix* audioMix, NSDictionary* info){
// NSLog(@"Info:\n%@",info); // NSLog(@"Info:\n%@",info);
AVURLAsset* myAsset = (AVURLAsset*)avasset; AVURLAsset *videoAsset = (AVURLAsset*)avasset;
// NSLog(@"AVAsset URL: %@",myAsset.URL); // NSLog(@"AVAsset URL: %@",myAsset.URL);
[self startExportVideoWithVideoAsset:videoAsset completion:completion];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:myAsset]; }];
// NSLog(@"%@",compatiblePresets); } else if ([asset isKindOfClass:[ALAsset class]]) {
NSURL *videoURL =[asset valueForProperty:ALAssetPropertyAssetURL]; // ALAssetPropertyURLs
if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) { AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:myAsset presetName:AVAssetExportPresetMediumQuality]; [self startExportVideoWithVideoAsset:videoAsset completion:completion];
NSDateFormatter *formater = [[NSDateFormatter alloc] init]; }
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"]; }
NSString *outputPath = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/output-%@.mp4", [formater stringFromDate:[NSDate date]]];
NSLog(@"video outputPath = %@",outputPath); - (void)startExportVideoWithVideoAsset:(AVURLAsset *)videoAsset completion:(void (^)(NSString *outputPath))completion {
// Find compatible presets by video asset.
exportSession.outputURL = [NSURL fileURLWithPath:outputPath]; NSArray *presets = [AVAssetExportSession exportPresetsCompatibleWithAsset:videoAsset];
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse = YES; // Begin to compress video
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) { // Now we just compress to low resolution if it supports
switch (exportSession.status) { // If you need to upload to the server, but server does't support to upload by streaming,
case AVAssetExportSessionStatusUnknown: // You can compress the resolution to lower. Or you can support more higher resolution.
NSLog(@"AVAssetExportSessionStatusUnknown"); break; if ([presets containsObject:AVAssetExportPreset640x480]) {
case AVAssetExportSessionStatusWaiting: AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:videoAsset presetName:AVAssetExportPreset640x480];
NSLog(@"AVAssetExportSessionStatusWaiting"); break;
case AVAssetExportSessionStatusExporting: NSDateFormatter *formater = [[NSDateFormatter alloc] init];
NSLog(@"AVAssetExportSessionStatusExporting"); break; [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
case AVAssetExportSessionStatusCompleted: NSString *outputPath = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/output-%@.mp4", [formater stringFromDate:[NSDate date]]];
NSLog(@"AVAssetExportSessionStatusCompleted"); NSLog(@"video outputPath = %@",outputPath);
if (completion) { session.outputURL = [NSURL fileURLWithPath:outputPath];
completion(outputPath);
} // Optimize for network use.
break; session.shouldOptimizeForNetworkUse = true;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed"); break; NSArray *supportedTypeArray = session.supportedFileTypes;
default: break; if ([supportedTypeArray containsObject:AVFileTypeMPEG4]) {
session.outputFileType = AVFileTypeMPEG4;
} else if (supportedTypeArray.count == 0) {
NSLog(@"No supported file types 视频类型暂不支持导出");
return;
} else {
session.outputFileType = [supportedTypeArray objectAtIndex:0];
}
// Begin to export video to the output path asynchronously.
[session exportAsynchronouslyWithCompletionHandler:^(void) {
switch (session.status) {
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown"); break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting"); break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting"); break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"AVAssetExportSessionStatusCompleted");
if (completion) {
completion(outputPath);
} }
}]; break;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed"); break;
default: break;
} }
}]; }];
} else {
NSLog(@"iOS8以前的系统,导出视频代码暂未更新...");
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment