Objective-C如何使用AVFoundation为视频添加叠加?

时间:2015-03-26 03:42:05

标签: ios objective-c video avfoundation

我一直试图在视频中添加叠加层数天,我只是无法弄清楚为什么当我保存时,视频会向左旋转90度。我一直试图修复它,但这和我一样接近它。目前会话预设位于AVCaptureSessionPreset1920x1080。当我旋转视频时,它会收缩,我必须将其翻译到中心。旋转后,我无法将视频设为全屏。请有人帮忙我真的需要它。我会做任何事情!

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];
    NSLog(@"WIDTH: %f HEIGHT: %f", clipVideoTrack.naturalSize.width, clipVideoTrack.naturalSize.height);
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    parentLayer.frame = CGRectMake(0, 0, videoSize.width, 5000);
    [parentLayer addSublayer:videoLayer];
    [parentLayer addSublayer:_text.layer];

    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];

    CGAffineTransform t1 = CGAffineTransformMakeTranslation(1500, 0);
    CGAffineTransform t2 = CGAffineTransformRotate(t1, M_PI_2);

    CGAffineTransform finalTransform = t2;
    [layerInstruction setTransform:finalTransform atTime:kCMTimeZero];

    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];
        });
    }];

1 个答案:

答案 0 :(得分:1)

使用此处的示例代码 http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

它进行视频方向计算并将此方向信息提供给AVMutableVideoCompositionLayerInstruction,它应该产生所需的输出。