版权声明:本文为博主原创文章,未经博主允许不得转载。
- #pragma mark - 录像
- - (void)recodVideo{
- if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
- UIImagePickerController* pickerView = [[UIImagePickerController alloc] init];
- pickerView.sourceType = UIImagePickerControllerSourceTypeCamera;
- NSArray* availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
- pickerView.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];
- pickerView.videoQuality = UIImagePickerControllerQualityTypeMedium;
- [self pushVieCtr:pickerView];
- // pickerView.videoMaximumDuration = 60;
- pickerView.delegate = self;
- [pickerView release];
- }
- }
- #pragma mark - UIImagePickerControllerDelegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- _videoURL = [info[UIImagePickerControllerMediaURL] retain];
- NSLog(@"_videoURL = %@",_videoURL);
- [picker.flipboardNavigationController popViewController];
- [self encodeVideoOrientation:_videoURL];
- NSLog(@"_videoURL.absoluteString %@",_videoURL.path);
- // NSString *videoPath = _videoURL.path;
- // if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath)) {
- // UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, NULL, NULL);
- // }
- }
- - (void)encodeVideoOrientation:(NSURL *)anOutputFileURL
- {
- _alert = [[UIAlertView alloc] init];
- [_alert setTitle:@"Waiting.."];
- UIActivityIndicatorView* activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- activity.frame = CGRectMake(140,
- 80,
- CGRectGetWidth(_alert.frame),
- CGRectGetHeight(_alert.frame));
- [_alert addSubview:activity];
- [activity startAnimating];
- [activity release];
- [_alert show];
- [_alert release];
- _startDate = [[NSDate date] retain];
- AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];
- AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:videoAsset
- presetName:AVAssetExportPresetMediumQuality];
- NSDateFormatter* formater = [[NSDateFormatter alloc] init];
- [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
- _mp4Path = [[NSHomeDirectory() stringByAppendingFormat:@"/Documents/videos/%@.mp4", [formater stringFromDate:[NSDate date]]] retain];
- [formater release];
- assetExport.outputURL = [NSURL fileURLWithPath: _mp4Path];
- assetExport.shouldOptimizeForNetworkUse = YES;
- assetExport.outputFileType = AVFileTypeMPEG4;
- assetExport.videoComposition = [self getVideoComposition:videoAsset];
- [assetExport exportAsynchronouslyWithCompletionHandler:^{
- switch ([assetExport status]) {
- case AVAssetExportSessionStatusFailed:
- {
- NSLog(@"AVAssetExportSessionStatusFailed!");
- [_alert dismissWithClickedButtonIndex:0 animated:NO];
- [self performSelectorOnMainThread:@selector(converFailed:) withObject:assetExport waitUntilDone:NO];
- break;
- }
- case AVAssetExportSessionStatusCancelled:
- NSLog(@"Export canceled");
- [_alert dismissWithClickedButtonIndex:0
- animated:YES];
- break;
- case AVAssetExportSessionStatusCompleted:
- NSLog(@"Successful!");
- [self performSelectorOnMainThread:@selector(convertFinish) withObject:nil waitUntilDone:NO];
- break;
- default:
- break;
- }
- [assetExport release];
- }];
- }
- #pragma mark - 解决录像保存角度问题
- -(AVMutableVideoComposition *) getVideoComposition:(AVAsset *)asset
- {
- AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
- AVMutableComposition *composition = [AVMutableComposition composition];
- AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
- CGSize videoSize = videoTrack.naturalSize;
- BOOL isPortrait_ = [self isVideoPortrait:asset];
- if(isPortrait_) {
- NSLog(@"video is portrait ");
- videoSize = CGSizeMake(videoSize.height, videoSize.width);
- }
- composition.naturalSize = videoSize;
- videoComposition.renderSize = videoSize;
- // videoComposition.renderSize = videoTrack.naturalSize; //
- videoComposition.frameDuration = CMTimeMakeWithSeconds( 1 / videoTrack.nominalFrameRate, 600);
- AVMutableCompositionTrack *compositionVideoTrack;
- compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
- [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:videoTrack atTime:kCMTimeZero error:nil];
- AVMutableVideoCompositionLayerInstruction *layerInst;
- layerInst = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
- [layerInst setTransform:videoTrack.preferredTransform atTime:kCMTimeZero];
- AVMutableVideoCompositionInstruction *inst = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
- inst.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
- inst.layerInstructions = [NSArray arrayWithObject:layerInst];
- videoComposition.instructions = [NSArray arrayWithObject:inst];
- return videoComposition;
- }
- -(BOOL) isVideoPortrait:(AVAsset *)asset
- {
- BOOL isPortrait = FALSE;
- NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeVideo];
- if([tracks count] > 0) {
- AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
- CGAffineTransform t = videoTrack.preferredTransform;
- // Portrait
- if(t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0)
- {
- isPortrait = YES;
- }
- // PortraitUpsideDown
- if(t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0) {
- isPortrait = YES;
- }
- // LandscapeRight
- if(t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0)
- {
- isPortrait = FALSE;
- }
- // LandscapeLeft
- if(t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0)
- {
- isPortrait = FALSE;
- }
- }
- return isPortrait;
- }