AVMutableComposition of a Solid Color with No AVAsset

后端 未结 1 1867
粉色の甜心
粉色の甜心 2021-02-02 00:09

Here\'s my end goal: I\'d like to use AVVideoCompositionCoreAnimationTool to create a video from Core Animation. I will not be using an existing AVAsset in

相关标签:
1条回答
  • 2021-02-02 00:33

    After playing around with this a few months ago I found that the only reliable way to get it to work is to use a short, blank video in your AVMutableCompositionTrack, then overlay it with the desired layers.

    I uploaded a project to GitHub about a month ago as a result of a bug in the simulator. You can download the blank video here, if you'd like.

    -(void)exportVideo
    {
        CGSize renderingSize = CGSizeMake(640, 360); // The desired size of your video
        float displayDuration = 2.0f; //The duration of the desired video, in seconds
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"FinalVideo-%d.mov",arc4random() % 1000]];
        NSURL *url = [NSURL fileURLWithPath:myPathDocs];
    
        NSError *error;
        [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
    
        mutableComposition = [AVMutableComposition composition];
        mutableCompositionVideoTrack = [mutableComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    
        videoComposition = [AVMutableVideoComposition videoComposition];
        videoComposition.renderSize = renderingSize;
        videoComposition.frameDuration = CMTimeMake(1, 30);
    
        CALayer *parentLayer = [CALayer layer];
        CALayer *videoLayer = [CALayer layer];
        parentLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height);
        videoLayer.frame = CGRectMake(0, 0, videoComposition.renderSize.width, videoComposition.renderSize.height);
        [parentLayer addSublayer:videoLayer];
    
        videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_1080p" ofType:@"mp4"];
        NSURL *trackUrl = [NSURL fileURLWithPath:path];
        AVAsset *asset = [AVAsset assetWithURL:trackUrl];
        AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        [mutableCompositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,CMTimeMakeWithSeconds(displayDuration, 600)) ofTrack:track atTime:kCMTimeZero error:nil];
    
        CALayer *imageLayer = [CALayer layer];
        imageLayer.bounds = parentLayer.frame;
        imageLayer.anchorPoint = CGPointMake(0.5, 0.5);
        imageLayer.position = CGPointMake(CGRectGetMidX(imageLayer.bounds), CGRectGetMidY(imageLayer.bounds));
        imageLayer.backgroundColor = [UIColor blueColor].CGColor;
        imageLayer.contentsGravity = kCAGravityResizeAspectFill;
        [parentLayer addSublayer:imageLayer];
    
        AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
        instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(displayDuration, 600));
        videoComposition.instructions = @[instruction];
    
        exporter = [[AVAssetExportSession alloc] initWithAsset:mutableComposition presetName:AVAssetExportPresetHighestQuality];
        exporter.outputURL = url;
        exporter.videoComposition = videoComposition;
        exporter.outputFileType= AVFileTypeMPEG4;
        exporter.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(displayDuration, 600));
        exporter.shouldOptimizeForNetworkUse = YES;
    
        [exporter exportAsynchronouslyWithCompletionHandler:^(void){
            dispatch_async(dispatch_get_main_queue(), ^{
                [self exportDidFinish:exporter];
            });
        }];
    }
    
    0 讨论(0)
提交回复
热议问题