Objective c 使用AVMutableVideoCompositionLayerInstruction的iOS旋转视频

Objective c 使用AVMutableVideoCompositionLayerInstruction的iOS旋转视频,objective-c,xcode,avfoundation,Objective C,Xcode,Avfoundation,目前,每当我导出视频时,它似乎会向右旋转90度,但我不知道为什么。我目前的代码是: AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:self.video options:nil]; AVMutableComposition *mixComposition = [AVMutableComposition composition]; AVMutableCompositionTrack *compositionVide

目前,每当我导出视频时,它似乎会向右旋转90度,但我不知道为什么。我目前的代码是:

AVURLAsset *videoAsset = [[AVURLAsset alloc] initWithURL:self.video options:nil];
    AVMutableComposition *mixComposition = [AVMutableComposition composition];

    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] lastObject];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:clipVideoTrack
                                    atTime:kCMTimeZero
                                     error:nil];

    [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]];

    CGSize videoSize = [clipVideoTrack naturalSize];
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    [parentLayer addSublayer:videoLayer];

    AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
    videoComp.renderSize = [clipVideoTrack naturalSize];
    videoComp.frameDuration = CMTimeMake(1, 30);
    videoComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration);
    AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

    instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
    videoComp.instructions = [NSArray arrayWithObject:instruction];

    AVAssetExportSession *assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
    assetExport.videoComposition = videoComp;
    NSString *videoName = @"output.mov";

    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:videoName];
    NSURL *exportURL = [NSURL fileURLWithPath:exportPath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];

    assetExport.outputFileType = AVFileTypeQuickTimeMovie;
    assetExport.outputURL = exportURL;
    assetExport.shouldOptimizeForNetworkUse = YES;

    [assetExport exportAsynchronouslyWithCompletionHandler:^(void){
        dispatch_async(dispatch_get_main_queue(), ^{
            [self exportDidFinish:assetExport];
        });
    }];
我尝试使用以下代码对其进行转换,尽管它在技术上确实会旋转视频,但它会将视频缩小为正方形,我不知道如何将其放大以适应整个屏幕:

CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI / 2);
        CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(1000, 1000);
        CGAffineTransform mixedTransform = CGAffineTransformConcat(rotation, translateToCenter);
        [layerInstruction setTransform:mixedTransform atTime:kCMTimeZero];

有什么建议吗?

我也有同样的问题。如果你找到任何解决办法,请指导我。