重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

关于UIImagePickerViewController拍摄video的分辨率和方向的一些代码整理

近期项目由于要使用拍摄视频,用的是UIImagePickerViewController,要求是分辨率640x480,但是试了各种

站在用户的角度思考问题,与客户深入沟通,找到色尼网站设计与色尼网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、成都网站制作、成都外贸网站建设、企业官网、英文网站、手机端网站、网站推广、域名与空间、雅安服务器托管、企业邮箱。业务覆盖色尼地区。

p_w_picpathPickerController.videoQuality

拍出来的视频导出来依然达不到640x480.

并且旋转设备拍摄后拍摄出来的视频导出来也是反的。

最后查阅了各种网站找了各种资料之后,看到国外大牛在视频拍摄完成之后使用AVFoundation做的压缩和视频调整。整理了一下代码做了部分调整,感觉到AVfoundation的强大之处,哈哈

这些代码用在拍摄之后的回掉中,

AVAsset *firstAsset = [AVAssetassetWithURL:[infoobjectForKey:UIImagePickerControllerMediaURL]];

        if(firstAsset !=nil && [[firstAsset tracksWithMediaType:AVMediaTypeVideo] count]>0){

           //Create AVMutableComposition Object.This object will hold our multiple AVMutableCompositionTrack.

           AVMutableComposition* mixComposition = [[AVMutableCompositionalloc]init];

            

           //VIDEO TRACK

           AVMutableCompositionTrack *firstTrack = [mixCompositionaddMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];

            [firstTrackinsertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)ofTrack:[[firstAssettracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0]atTime:kCMTimeZeroerror:nil];

           AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstructionvideoCompositionInstruction];

            MainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, firstAsset.duration);

            

            if ([[firstAsset tracksWithMediaType:AVMediaTypeAudio] count]>0) {

                //AUDIO TRACK

               AVMutableCompositionTrack *firstAudioTrack = [mixCompositionaddMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid];

                [firstAudioTrackinsertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration)ofTrack:[[firstAssettracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]atTime:kCMTimeZeroerror:nil];

            }else{

               NSLog(@"warning: video has no audio");

            }

            

           //FIXING ORIENTATION//

           AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstructionvideoCompositionLayerInstructionWithAssetTrack:firstTrack];

            

            AVAssetTrack *FirstAssetTrack = [[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

            

            UIImageOrientation FirstAssetOrientation_  = UIImageOrientationUp;

            

            BOOL  isFirstAssetPortrait_  = NO;

            

            CGAffineTransform firstTransform = FirstAssetTrack.preferredTransform;

            

            if(firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0)

            {

                FirstAssetOrientation_= UIImageOrientationRight; isFirstAssetPortrait_ = YES;

            }

            if(firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0)

            {

                FirstAssetOrientation_ =  UIImageOrientationLeft; isFirstAssetPortrait_ = YES;

            }

            if(firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0)

            {

                FirstAssetOrientation_ =  UIImageOrientationUp;

            }

            if(firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0)

            {

                FirstAssetOrientation_ = UIImageOrientationDown;

            }

            

            CGFloat FirstAssetScaleToFitRatio = 768.0/FirstAssetTrack.naturalSize.width;

            

            if(isFirstAssetPortrait_)

            {

                FirstAssetScaleToFitRatio = 768.0/FirstAssetTrack.naturalSize.height;

                CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio);

                [FirstlayerInstruction setTransform:CGAffineTransformConcat(FirstAssetTrack.preferredTransform, FirstAssetScaleFactor) atTime:kCMTimeZero];

            }

            else

            {

                CGAffineTransform FirstAssetScaleFactor = CGAffineTransformMakeScale(FirstAssetScaleToFitRatio,FirstAssetScaleToFitRatio);

                [FirstlayerInstructionsetTransform:CGAffineTransformConcat(CGAffineTransformConcat(FirstAssetTrack.preferredTransform, FirstAssetScaleFactor),CGAffineTransformMakeTranslation(0,0))atTime:kCMTimeZero];

            }

            [FirstlayerInstruction setOpacity:0.0 atTime:firstAsset.duration];

            

            MainInstruction.layerInstructions = [NSArray arrayWithObjects:FirstlayerInstruction,nil];;

            

           AVMutableVideoComposition *MainCompositionInst = [AVMutableVideoCompositionvideoComposition];

            MainCompositionInst.instructions = [NSArray arrayWithObject:MainInstruction];

            MainCompositionInst.frameDuration = CMTimeMake(1, 30);

            if(isFirstAssetPortrait_)

            {

                MainCompositionInst.renderSize = CGSizeMake(480.0, 640.0);

            }else{

                MainCompositionInst.renderSize = CGSizeMake(640.0, 480.0);

            }

//            MainCompositionInst.renderSize = CGSizeMake(640.0, 480.0);

            

            NSURL *url = [NSURL fileURLWithPath:videoPath];

            

            exporter = nil;

           exporter = [[AVAssetExportSessionalloc]initWithAsset:mixCompositionpresetName:AVAssetExportPreset640x480];

            

            exporter.outputURL=url;

           exporter.outputFileType =AVFileTypeMPEG4;

            exporter.videoComposition = MainCompositionInst;

           exporter.shouldOptimizeForNetworkUse =YES;

            [exporterexportAsynchronouslyWithCompletionHandler:^

             {

                 dispatch_async(dispatch_get_main_queue(), ^{

//                     [self exportDidFinish:exporter];

                    if(exporter.status ==AVAssetExportSessionStatusCompleted){

                         //视频保存成功之后的处理

                     }else{

                         NSLog(@"error fixing orientation");

                     }

                 });

             }];


当前文章:关于UIImagePickerViewController拍摄video的分辨率和方向的一些代码整理
链接URL:http://cqcxhl.cn/article/psddoi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP