开发者

exporting CALayer with AVMutableVideoComposition

开发者 https://www.devze.com 2023-03-13 05:25 出处:网络
I am trying to export an image over video, but when the export fininshes I get a video of black screen with the image.

I am trying to export an image over video, but when the export fininshes I get a video of black screen with the image. Here's the code that sets up the compositions:

UIImage *myImage = [UIImage imageNamed:@"block.png"];
CALayer *aLayer = [CALayer layer];
aLayer.frame = CGRectMake(100, 100, 40, 40);
aLayer.contents = (id)myImage.CGImage ;
NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1" ofType:"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
cmp = [开发者_开发技巧[AVMutableComposition alloc] init] ;  
AVMutableCompositionTrack *trackA = [cmp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil ;
AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
[trackA insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error] ;
animComp = [[AVMutableVideoComposition videoComposition] retain] ;
animComp.renderSize = CGSizeMake(640, 480);
animComp.frameDuration = CMTimeMake(1,30);
animComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithAdditionalLayer:aLayer asTrackID:2];
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [asset duration]);
AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:trackA];
[layerInstruction setTrackID:2];
[layerInstruction setOpacity:1.0 atTime:kCMTimeZero ];
instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction] ;
animComp.instructions = [NSArray arrayWithObject:instruction];

To export the movie I use the following code:

-(IBAction) exportMovie:(id)sender{
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *tempPath = [docPaths objectAtIndex:0];
NSLog(@"Temp Path: %@",tempPath);

NSString *fileName = [NSString stringWithFormat:@"%@/output-anot.MOV",tempPath];
NSFileManager *fileManager = [NSFileManager defaultManager] ;
if([fileManager fileExistsAtPath:fileName ]){
    NSError *ferror = nil ;
    BOOL success = [fileManager removeItemAtPath:fileName error:&ferror];
}

NSURL *exportURL = [NSURL fileURLWithPath:fileName];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:cmp presetName:AVAssetExportPresetHighestQuality]  ;
exporter.outputURL = exportURL ;
exporter.videoComposition = animComp ;
exporter.outputFileType= AVFileTypeQuickTimeMovie ;
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
    switch (exporter.status) {
        case AVAssetExportSessionStatusFailed:{
            NSLog(@"Fail");
            break;
        }
        case AVAssetExportSessionStatusCompleted:{
            NSLog(@"Success");
            break;
        }

        default:
            break;
    } 
}];

}

Am I missing sthg here or done something wrong.


yes I see the issue, thanks for the code BTW! I have been trying to do this, and I just copy paste (I'm a noob) but I found your issue while editing the code for my issue, this line

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1.mp4" ofType:nil];

that is wrong, funny kind of, anyways change it to this

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"video-1" ofType:@"mp4"];

and that should be the only issue. the reason its black is because it doesn't have the right movie.

0

精彩评论

暂无评论...
验证码 换一张
取 消