UIImagePickController錄制視頻的旋轉(zhuǎn)90度

調(diào)用系統(tǒng)的這個(gè)玩意竟然會有這個(gè)問題,真是不爽.

于是乎開始查資料:

-(void)useTheSystemRecordViewController{

UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];

imagePicker.delegate = self;

//判斷是否可以拍攝

if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

//判斷是否擁有拍攝權(quán)限

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){

return;

}

//拍攝

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

//錄制的類型 下面為視頻

imagePicker.mediaTypes=@[(NSString*)kUTTypeMovie];

//錄制的時(shí)長

imagePicker.videoMaximumDuration=10.0;

//模態(tài)視圖的彈出效果

imagePicker.modalPresentationStyle=UIModalPresentationOverFullScreen;

[self presentViewController:imagePicker animated:YES completion:nil];

}

}

-(void)encodeVideoOrientation:(NSURL*)anOutputFileURL{

AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];

AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:videoAsset

presetName:AVAssetExportPresetMediumQuality];

NSString* mp4Path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/Movie.mp4"];

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!");

break;

}

case AVAssetExportSessionStatusCancelled:

NSLog(@"Export canceled");

break;

case AVAssetExportSessionStatusCompleted:

NSLog(@"Successful!");

break;

default:

break;

}

}];

}

#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;

}

反正整個(gè)過程的邏輯大概就是判斷視頻方向,重新做一個(gè)壓縮處理,轉(zhuǎn)換成正確的方向.親測可用!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容