我試圖加載視頻,在其上添加一個動畫,然後導出它, 但動畫從未開始在導出的視頻中播放。 它只是顯示圖像「dogge_icon.png」。AV基金會動畫永遠不會開始播放
我試過不同類型的動畫,不知道我做錯了什麼。 任何幫助將不勝感激。
代碼:
-(void) createCompositionWithPicture {
AVMutableComposition* composition = [AVMutableComposition composition];
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSLog(@"Path: %@", videoPath);
NSURL *videoURL = [[NSURL alloc] initFileURLWithPath:videoPath];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
// Create an AVMutableVideoCompositionLayerInstruction for the video track.
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
AVMutableVideoCompositionLayerInstruction *videolayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
// Setup video composition
AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(videoTrack.naturalSize.width,videoTrack.naturalSize.height);
videoComposition.frameDuration = CMTimeMake(1, 30);
mainInstruction.layerInstructions = [NSArray arrayWithObject:videolayerInstruction];
videoComposition.instructions = [NSArray arrayWithObject:mainInstruction];
NSLog(@"Width: %f Height: %f", videoTrack.naturalSize.width, videoTrack.naturalSize.height);
// Setup animation layer
UIImage* image = [UIImage imageNamed:@"dogge_icon.png"];
CALayer *animationLayer = [CALayer layer];
animationLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[animationLayer setMasksToBounds:YES];
[animationLayer setContents: (id)image.CGImage];
// Add animation
CABasicAnimation *animation =
[CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration=5.0;
animation.autoreverses=YES;
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:2.0f];
animation.repeatCount=10;
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
[animationLayer addAnimation:animation forKey:@"scale"];
NSLog(@"animationLayer animations: %@", [animationLayer animationKeys]);
// Build layer hierarchy
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height);
videoLayer.frame = CGRectMake(0, 0, videoTrack.naturalSize.width, videoTrack.naturalSize.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:animationLayer];
videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
// Export
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality];
NSString *exportVideoPath = [STPFileUtilities getPathToFileIn: NSDocumentDirectory WithName: @"composition.mov"];
[STPFileUtilities deleteFileIfExists:exportVideoPath];
NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath];
exportSession.outputURL = exportURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.videoComposition = videoComposition;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch (exportSession.status) {
case AVAssetExportSessionStatusFailed:{
NSLog(@"FAIL: %@", exportSession.error);
break;
}
case AVAssetExportSessionStatusCompleted: {
NSLog (@"SUCCESS");
}
};
}];
}
如果我想在兩秒後添加動畫,該怎麼辦?我可以在AVCoreAnimationBeginTimeAtZero上添加幾秒鐘嗎?我試過flipAnimation.beginTime = CACurrentMediaTime()+ 1;但沒有運氣。 – Nil
您需要自己計算時間並將其添加到動畫中。我不認爲你可以在合成視頻的情況下使用CACurrentMediaTime。我會在你的答案中加入一些細節。 –