1. 程式人生 > >iOS 錄制視頻MOV格式轉MP4

iOS 錄制視頻MOV格式轉MP4

sse 攝像頭 idl del color nserror bject put nsobject

使用UIImagePickerController系統控制器錄制視頻時,默認生成的格式是MOV,如果要轉成MP4格式的,我們需要使用AVAssetExportSession;

支持轉換的視頻質量:低,中,高,640*480,720p,1080p等

如下代碼片段轉換

- (void)mov2mp4:(NSURL *)movUrl
{
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:movUrl options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    
/** AVAssetExportPresetMediumQuality 表示視頻的轉換質量, */ if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality]; //轉換完成保存的文件路徑
NSString * resultPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4",@"cvt"]; exportSession.outputURL = [NSURL fileURLWithPath:resultPath]; //要轉換的格式,這裏使用 MP4 exportSession.outputFileType = AVFileTypeMPEG4; //轉換的數據是否對網絡使用優化
exportSession.shouldOptimizeForNetworkUse = YES; //異步處理開始轉換 [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { //轉換狀態監控 switch (exportSession.status) { case AVAssetExportSessionStatusUnknown: NSLog(@"AVAssetExportSessionStatusUnknown"); break; case AVAssetExportSessionStatusWaiting: NSLog(@"AVAssetExportSessionStatusWaiting"); break; case AVAssetExportSessionStatusExporting: NSLog(@"AVAssetExportSessionStatusExporting"); break; case AVAssetExportSessionStatusFailed: NSLog(@"AVAssetExportSessionStatusFailed"); break; case AVAssetExportSessionStatusCancelled: NSLog(@"AVAssetExportSessionStatusCancelled"); break; case AVAssetExportSessionStatusCompleted: { //轉換完成 NSLog(@"AVAssetExportSessionStatusCompleted"); //測試使用,保存在手機相冊裏面 ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init]; [assetLibrary writeVideoAtPathToSavedPhotosAlbum:exportSession.outputURL completionBlock:^(NSURL *assetURL, NSError *error){ if (error) { NSLog(@"%@",error); } }]; break; } } }]; } }

完整的調用以及轉換代碼

#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    
}

- (IBAction)startCampVD:(id)sender {
 
    UIImagePickerController *pickerCon = [[UIImagePickerController alloc]init];
    pickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;
    pickerCon.mediaTypes = @[(NSString *)kUTTypeMovie];//設定相機為視頻
    pickerCon.cameraDevice = UIImagePickerControllerCameraDeviceFront;//設置相機後攝像頭
    pickerCon.videoMaximumDuration = 6;//最長拍攝時間
    pickerCon.videoQuality = UIImagePickerControllerQualityType640x480;//拍攝質量
    pickerCon.allowsEditing = NO;//是否可編輯
    pickerCon.delegate = self;
    
    
    [self presentViewController:pickerCon animated:YES completion:nil];


}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    NSLog(@"視頻錄制完成...");
    NSLog(@"%@",info);

    [self mov2mp4:[info objectForKey:UIImagePickerControllerMediaURL]];
    
    [self dismissViewControllerAnimated:YES completion:nil];

    
    


}

- (void)mov2mp4:(NSURL *)movUrl
{
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:movUrl options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    /**
     AVAssetExportPresetMediumQuality 表示視頻的轉換質量,
     */
    if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality]) {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
        
        //轉換完成保存的文件路徑
        NSString * resultPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4",@"cvt"];
        
        exportSession.outputURL = [NSURL fileURLWithPath:resultPath];
        
        //要轉換的格式,這裏使用 MP4
        exportSession.outputFileType = AVFileTypeMPEG4;
        
        //轉換的數據是否對網絡使用優化
        exportSession.shouldOptimizeForNetworkUse = YES;
        
        //異步處理開始轉換
        [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
         
         {
             //轉換狀態監控
             switch (exportSession.status) {
                 case AVAssetExportSessionStatusUnknown:
                     NSLog(@"AVAssetExportSessionStatusUnknown");
                     break;
                     
                 case AVAssetExportSessionStatusWaiting:
                     NSLog(@"AVAssetExportSessionStatusWaiting");
                     break;
                     
                 case AVAssetExportSessionStatusExporting:
                     NSLog(@"AVAssetExportSessionStatusExporting");
                     break;
                 case AVAssetExportSessionStatusFailed:
                     NSLog(@"AVAssetExportSessionStatusFailed");  
                     break;
                 case AVAssetExportSessionStatusCancelled:
                     NSLog(@"AVAssetExportSessionStatusCancelled");
                     break;
                  
                 case AVAssetExportSessionStatusCompleted:
                 {
                     //轉換完成
                     NSLog(@"AVAssetExportSessionStatusCompleted");
                     
                     //測試使用,保存在手機相冊裏面
                     ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
                     [assetLibrary writeVideoAtPathToSavedPhotosAlbum:exportSession.outputURL completionBlock:^(NSURL *assetURL, NSError *error){
                         if (error) {
                             NSLog(@"%@",error);
                         }
                     }];
                     break;
                 }
             }
             
         }];  
        
    }  

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    NSLog(@"視頻錄制取消了...");
    
}

iOS 錄制視頻MOV格式轉MP4