gpt4 book ai didi

avfoundation - 向视频添加摆动图标

转载 作者:行者123 更新时间:2023-12-01 01:13:12 30 4
gpt4 key购买 nike

有人可以帮我将一个图标合并到一个视频中,该视频应该在视频播放时摆动。我已将动画添加到摆动图标,但只有静态图标会合并到视频中。如果有人可以帮助我,我将不胜感激。

我给你我正在使用的代码

//- (void)exportDidFinish:(AVAssetExportSession*)session{


NSLog(@"baseView subviews : %@", [baseView subviews]);

UIGraphicsBeginImageContext(baseView.bounds.size);
[baseView.layer renderInContext:UIGraphicsGetCurrentContext()];
//[self wobbleVideo:baseView];

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

aLayer = [CALayer layer];
aLayer.frame = CGRectMake(0, 0, 320, 380);
aLayer.bounds = CGRectMake(0, 0, 320, 380);
aLayer.contents = (id) resultImage.CGImage;
aLayer.opacity = 1;
aLayer.backgroundColor = [UIColor clearColor].CGColor;
aLayer.geometryFlipped = YES;
[aLayer addAnimation:[self getShakeAnimation] forKey:@"transform"];


AVURLAsset *asset = [AVURLAsset URLAssetWithURL:rotatedVideoUrl options:nil];
cmp = [AVMutableComposition composition];

AVMutableCompositionTrack *videoComposition = [cmp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioComposition = [cmp addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *sourceAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
[videoComposition insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:sourceVideoTrack atTime:kCMTimeZero error:nil] ;
[audioComposition insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:sourceAudioTrack atTime:kCMTimeZero error:nil] ;


animComp = [AVMutableVideoComposition videoComposition];
animComp.renderSize = CGSizeMake(320, 360);
animComp.frameDuration = CMTimeMake(1,30);
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, 320, 360);
videoLayer.frame = CGRectMake(0, 0, 320, 360);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:aLayer];
animComp.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];


// to gather the audio part of the video
NSArray *tracksToDuck = [cmp tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *trackMixArray = [NSMutableArray array];
for (NSInteger i = 0; i < [tracksToDuck count]; i++) {
AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:[tracksToDuck objectAtIndex:i]];
[trackMix setVolume:10 atTime:kCMTimeZero];
[trackMixArray addObject:trackMix];
}
audioMix = [AVMutableAudioMix audioMix];
audioMix.inputParameters = trackMixArray;

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [asset duration]);
AVMutableVideoCompositionLayerInstruction *layerVideoInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoComposition];
[layerVideoInstruction setOpacity:1.0 atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject:layerVideoInstruction] ;
animComp.instructions = [NSArray arrayWithObject:instruction];
[self exportMovie:self];


}



//WOBBLE ANIMATION-------------------------------------
- (CAKeyframeAnimation*)getShakeAnimation {

CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

CGFloat wobbleAngle = 0.06f;

NSValue* valLeft = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];
NSValue* valRight = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];
animation.values = [NSArray arrayWithObjects:valLeft, valRight, nil];

animation.autoreverses = YES;
animation.duration = 0.125;
animation.repeatCount = HUGE_VALF;

return animation;
}

最佳答案

使用 CABasicAnimation 来实现你想要的。我在这里粘贴了一个样本。请看看并告诉我您是否仍然面临问题。

CALayer *parentLayer = [CALayer 层];
CALayer *videoLayer = [CALayer 层];

parentLayer.sublayerTransform = CATransform3DMakeRotation(0.0f, 1.0f, -1.0f, 1.0f);

parentLayer.frame = CGRectMake(0, 0, 320, (window.bounds.size.height - 50));
videoLayer.frame = CGRectMake(0, 0, 320, (window.bounds.size.height - 50));

parentLayer.geometryFlipped = NO;
[parentLayer addSublayer:videoLayer];


//---- adding wobble image to the video ----//
for (UIImageView * individualImageViews in [baseView subviews])
{
aLayer = [CALayer layer];

float newY = ((window.bounds.size.height - 50) - (individualImageViews.frame.origin.y + individualImageViews.frame.size.height)); // frame is taken like this because the layer was getting flipped

aLayer.frame = individualImageViews.frame;
aLayer.bounds = individualImageViews.frame;
aLayer.frame = CGRectMake(individualImageViews.frame.origin.x, newY , individualImageViews.frame.size.width, individualImageViews.frame.size.height) ;
aLayer.bounds = CGRectMake(individualImageViews.frame.origin.x, newY , individualImageViews.frame.size.width, individualImageViews.frame.size.height);



aLayer.contents = (id) individualImageViews.image.CGImage;
aLayer.opacity = 1;
aLayer.backgroundColor = [UIColor clearColor].CGColor;
aLayer.geometryFlipped = NO;

CABasicAnimation *trans = [CABasicAnimation animationWithKeyPath:@"transform"];

CGFloat wobbleAngle = 0.3f;

NSValue* valLeft;
NSValue* valRight;


valLeft = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];
valRight = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];

trans.fillMode = kCAFillModeForwards;
trans.duration = 1;
trans.fromValue = valLeft;
trans.toValue = valRight;
trans.cumulative = NO;
trans.repeatDuration = 10;
trans.autoreverses = YES;

//trans.autoreverses = YES;
//trans.repeatCount = HUGE_VALF;
//trans.fromValue = [NSNumber numberWithFloat:1.0];
//trans.toValue = [NSNumber numberWithFloat:0.0];;

// need this for export
trans.beginTime = 1e-100;
trans.removedOnCompletion = NO;

[aLayer addAnimation:trans forKey:@"transform"];

[parentLayer addSublayer:aLayer];
}

亲爱的一切都好。

关于avfoundation - 向视频添加摆动图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13890926/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com