中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

關于UIImagePickerViewController拍攝video的分辨率和方向的一些代碼整理

發布時間:2020-07-02 21:59:17 來源:網絡 閱讀:1504 作者:zql5666641 欄目:移動開發

近期項目由于要使用拍攝視頻,用的是UIImagePickerViewController,要求是分辨率640x480,但是試了各種

p_w_picpathPickerController.videoQuality

拍出來的視頻導出來依然達不到640x480.

并且旋轉設備拍攝后拍攝出來的視頻導出來也是反的。

最后查閱了各種網站找了各種資料之后,看到國外大牛在視頻拍攝完成之后使用AVFoundation做的壓縮和視頻調整。整理了一下代碼做了部分調整,感覺到AVfoundation的強大之處,哈哈


這些代碼用在拍攝之后的回掉中,

AVAsset *firstAsset = [AVAsset assetWithURL:[info objectForKey:UIImagePickerControllerMediaURL]];

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

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

            AVMutableComposition* mixComposition = [[AVMutableComposition alloc] init];

            

            //VIDEO TRACK

            AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

            [firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

            AVMutableVideoCompositionInstruction * MainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

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

            

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

                //AUDIO TRACK

                AVMutableCompositionTrack *firstAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

                [firstAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

            }else{

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

            }

            

            //FIXING ORIENTATION//

            AVMutableVideoCompositionLayerInstruction *FirstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack: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);

                [FirstlayerInstruction setTransform: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 = [AVMutableVideoComposition videoComposition];

            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 = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];

            

            exporter.outputURL=url;

            exporter.outputFileType = AVFileTypeMPEG4;

            exporter.videoComposition = MainCompositionInst;

            exporter.shouldOptimizeForNetworkUse = YES;

            [exporter exportAsynchronouslyWithCompletionHandler:^

             {

                 dispatch_async(dispatch_get_main_queue(), ^{

//                     [self exportDidFinish:exporter];

                     if(exporter.status == AVAssetExportSessionStatusCompleted){

                         //視頻保存成功之后的處理

                     }else{

                         NSLog(@"error fixing orientation");

                     }

                 });

             }];


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

佛山市| 湘阴县| 岳阳市| 加查县| 贵德县| 合江县| 东丰县| 青河县| 来凤县| 和平区| 济南市| 满城县| 浦东新区| 房山区| 钦州市| 凉山| 东港市| 浮梁县| 青神县| 武隆县| 达州市| 资阳市| 郴州市| 建水县| 曲水县| 上高县| 昆山市| 泊头市| 同心县| 凭祥市| 庆元县| 高尔夫| 亚东县| 普兰店市| 宜兰市| 长岛县| 宁晋县| 郁南县| 洪洞县| 滕州市| 竹溪县|