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 = ;//最长拍摄时间
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(@"视频录制取消了..."); }

上一篇:配置磁盘映射(在服务器和eclipse 中)


下一篇:Win7 64位 VS2013环境编译boost1_58_0