AVMutableComposition-旋转肖像视频以适合横向(16:9)视频

时间:2018-08-28 12:41:26

标签: ios objective-c avmutablecomposition

我正在使用AVMutableComposition组合并创建16:9的风景视频。问题在于,以纵向模式拍摄的所有视频在视频中均显示为旋转状态。现在,我相信我需要使用一个指令层,但是我不确定如何将其添加到以下代码中。

- (void)combineTheAssets {


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

    [mixComposition setNaturalSize:CGSizeMake(1280, 720)];

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

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



    int i = 0;

    for (NSMutableDictionary * dict in self.arraySelectedAssets) {

        AVAsset *asset = [dict objectForKey:@"avasset"];


        CMTime insertTime = kCMTimeZero;


            CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

            [track insertTimeRange:timeRange
                                ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                 atTime:insertTime
                                  error:nil];

            [audioTrack insertTimeRange:timeRange
                                ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                 atTime:insertTime
                                  error:nil];


            insertTime = CMTimeAdd(insertTime,asset.duration);


        i = i + 1;

    }

    // 4 - Get path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]];
    self.combinedVideoURL = [NSURL fileURLWithPath:myPathDocs];

    // 5 - Create exporter

    self.timerExporter = [NSTimer scheduledTimerWithTimeInterval:0.01f
                                             target:self
                                           selector:@selector(exporterProgress)
                                           userInfo:nil
                                            repeats:YES];

    // 5 - Create exporter
    self.exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                      presetName:AVAssetExportPresetHighestQuality];
    self.exporter .outputURL=self.combinedVideoURL;
    self.exporter .outputFileType = AVFileTypeQuickTimeMovie;
    self.exporter .shouldOptimizeForNetworkUse = YES;
    [self.exporter  exportAsynchronouslyWithCompletionHandler:^{

        [self.timerExporter invalidate];

        dispatch_async(dispatch_get_main_queue(), ^{
            self.labelProgressText.text = [NSString stringWithFormat:@"%@ (100%%)", NSLocalizedString(@"Combining The Videos", nil)];
            [self applyTheFilter];
        });
    }];

}

0 个答案:

没有答案