使用AVMutableVideoComposition导出CALayer

时间:2011-06-16 12:20:04

标签: ios ipad avfoundation

我正在尝试通过视频导出图像,但是当导出完成后,我会得到带有图像的黑屏视频。 以下是设置合成的代码:

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

要导出影片,请使用以下代码:

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

}

我在这里错过了什么或做错了什么。

1 个答案:

答案 0 :(得分:1)

是的,我看到了这个问题,感谢代码BTW!我一直试图这样做,我只是复制粘贴(我是菜鸟)但我在编辑我的问题的代码时发现了你的问题,这一行

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

这是错误的,有趣的,无论如何改变它到这个

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

这应该是唯一的问题。黑色的原因是因为它没有合适的电影。