91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

關于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

武冈市| 故城县| 克什克腾旗| 会同县| 合川市| 凌云县| 安国市| 镶黄旗| 大邑县| 胶南市| 渭源县| 当阳市| 乌鲁木齐市| 金川县| 伊金霍洛旗| 休宁县| 辰溪县| 思茅市| 五台县| 卢氏县| 丘北县| 福泉市| 腾冲县| 日土县| 平阴县| 邵东县| 临邑县| 永嘉县| 阳城县| 临洮县| 德清县| 襄垣县| 望城县| 射阳县| 本溪| 涟源市| 巢湖市| 邮箱| 冷水江市| 安国市| 河源市|