2011-06-16 47 views
2

我想通過視頻導出圖像,但是當導出fininshes我得到一個黑色的視頻圖像。 下面是樹立組成的代碼:導出CALayer與AVMutableVideoComposition

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

}

我錯過STHG這裏還是做錯了。

回答

1

是的我看到了這個問題,感謝代碼BTW!我一直在努力做到這一點,我只是複製粘貼(我是小白),但我發現您的問題,而編輯我的問題的代碼,該行

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

這是錯誤的,搞笑的那種,無論如何改變它到這

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

這應該是唯一的問題。其黑色的原因是因爲它沒有正確的電影。