iOS 录制视频MOV格式转MP4

Posted cocoajin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 录制视频MOV格式转MP4相关的知识,希望对你有一定的参考价值。

使用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的主要内容,如果未能解决你的问题,请参考以下文章

以 mp4 格式录制、保存和/或转换视频?

记MacOs视频mov与mp4格式转换问题解决

把mov的视频转成movie maker能认的格式,用啥软件啊?

xcode Swift 将录制的视频保存为 mov 或 mp4

iOS 视频录制压缩上传

快速将MOV格式视频转换成MP4无损方法