Objective-C如何使用AVFoundation向视频添加覆盖图? [英] Objective-C How To Add Overlay To Video Using AVFoundation?

查看:148
本文介绍了Objective-C如何使用AVFoundation向视频添加覆盖图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在尝试向视频添加叠加,但我不知道为什么保存时视频会向左旋转90度.我一直在尝试对其进行修复,但这与我所获得的解决方案非常接近.目前,会话预设位于AVCaptureSessionPreset1920x1080.旋转视频时,视频会缩小,并且必须将其平移到中心.旋转视频后,我无法使视频全屏显示.请有人帮我,我真的很需要它.我会做任何事!

I've been trying to add overlay to a video for days now, and I just can't figure out why when I save, the video gets rotated ninety degrees to the left. I've been trying to fix it but this is as close to it as I have gotten. At the moment the session preset is at AVCaptureSessionPreset1920x1080. When I rotate the video it shrinks, and I have to translate it to the center. I can't manage to get the video to be full screen after I rotate it. Please someone help I really really need it. I'll do anything!

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

推荐答案

在此处使用示例代码 http://www.raywenderlich.com/30200/avfoundation-tutorial-addings-over-animations-videos

Use the sample code from here http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

它会进行视频方向计算,并将此方向信息输入到AVMutableVideoCompositionLayerInstruction中,该指令会产生所需的输出.

It does the video orientation calculation and feeds this orientation info into the AVMutableVideoCompositionLayerInstruction which should produce the desired output.

这篇关于Objective-C如何使用AVFoundation向视频添加覆盖图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆