AVAsset在iOS中保存视频,图像叠加会破坏视频

时间:2015-10-16 13:06:52

标签: ios video uiimage avasset

我正在尝试将图片叠加层和视频合并到一个新的视频文件中。这是我正在使用的代码:

- (void)mergeVideoUrl:(NSURL *)videoURL andOverlayImage:(UIImage *)overlayImage
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    // Create an AVURLAsset
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoURL options:nil];
    AVMutableComposition* mixComposition = [AVMutableComposition composition];

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

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

    CGSize videoSize = [clipVideoTrack naturalSize];

    // Get our image layer
    CALayer *aLayer = [CALayer layer];
    aLayer.contents = (id)overlayImage.CGImage;
    aLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    aLayer.opacity = 1.0;

    // Sort the layer order
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height);
    [parentLayer addSublayer:videoLayer];
    [parentLayer addSublayer:aLayer];

    // Create the composition and add the instructions to insert the layer
    AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition];
    videoComp.renderSize = videoSize;
    videoComp.frameDuration = CMTimeMake(1, 30);
    videoComp.animationTool = [AVVideoCompositionCoreAnimationTool      videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

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

    // Export
    AVAssetExportSession *assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
    assetExport.videoComposition = videoComp;

    NSString* videoName = @"mynewwatermarkedvideo.mov";

    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent: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)
     {
         //FINALIZATION CODE HERE
         NSLog(@"DONE: %@", exportUrl);
     }];
}

问题在于它搞砸了视频。知道为什么吗? 我感觉视频帧大小可能与它或宽高比有关,好像我设置了(2000,2000)的大小,它似乎工作正常,但有很多黑边。

enter image description here

EDIT ----- 似乎如果我将CGSize videoSize设置为2/3的比例,例如320x4801280x1920,那么它可以工作,但是生成的视频在右侧有一个黑条。为什么[clipVideoTrack naturalSize]会报告1080x1920的大小,但除非我使用1280x1920,否则会搞砸?我认为这可能是转型/作物问题?

EDIT2 ----- 在AVAssetExportSessionAVAssetExportPresetLowQuality&之间更改AVAssetExportPresetMediumQuality预设名称AVAssetExportPresetHighestQuality会产生不同大小的视频。所以我认为这会以某种方式覆盖输出视频帧大小。

1 个答案:

答案 0 :(得分:0)

CGSize videoSize = [clipVideoTrack naturalSize];

这为您提供了录制的尺寸,但您需要查看曲目的prefferedTransform以找到录制的方向。您可能会发现视频宽度实际上是naturalSize'高度'取决于录制期间设备的保持方式。

我看到你在构图上设置它,但你需要检查它才能为你的CALayers分配正确的帧