gpt4 book ai didi

ios - 如何将背景图像设置为 AVMutableVideoCompositionInstruction

转载 作者:行者123 更新时间:2023-12-01 16:28:53 25 4
gpt4 key购买 nike

我设置了backgroundColor它工作正常,但我无法设置背景图像。

AVMutableVideoCompositionInstruction * mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,assetWithMaxTime.duration);
mainInstruction.enablePostProcessing = NO;

mainInstruction.backgroundColor =[UIColor yellowColor].CGColor;

最佳答案

在尝试了几件事之后,我终于找到了一种工作方法。要使其正常工作,需要使用空白的视频/音频轨道。然后将背景图像叠加到这个空白视频层。十 导出它并结合原始 Assets (视频)和导出的 Assets ( Assets )并导出最终 Assets (视频)。

添加叠加

- (void)addOverlayImage:(UIImage *)overlayImage ToVideo:(AVMutableVideoComposition *)composition inSize:(CGSize)size {
// 1 - set up the overlay
CALayer *overlayLayer = [CALayer layer];

[overlayLayer setContents:(id)[overlayImage CGImage]];
overlayLayer.frame = CGRectMake(0, 0, size.width, size.height);
[overlayLayer setMasksToBounds:YES];

// 2 - set up the parent layer
CALayer *parentLayer = [CALayer layer];
CALayer *videoLayer = [CALayer layer];
parentLayer.frame = CGRectMake(0, 0, size.width, size.height);
videoLayer.frame = CGRectMake(0, 0, size.width, size.height);
[parentLayer addSublayer:videoLayer];
[parentLayer addSublayer:overlayLayer];

// 3 - apply magic
composition.animationTool = [AVVideoCompositionCoreAnimationTool
videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];
}

- (void)getBackgroundVideoAssetWithcompletion:(void (^)(AVAsset *bgAsset))completionBlock {

NSString *path = [[NSBundle mainBundle] pathForResource:@"blank_video" ofType:@"mp4"];
NSURL *trackUrl = [NSURL fileURLWithPath:path];
AVAsset *asset = [AVAsset assetWithURL:trackUrl];
AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

CMTimeRange range = CMTimeRangeMake(kCMTimeZero, [asset duration]);
AVMutableComposition* mixComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:range ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];


CGAffineTransform videoTransform = track.preferredTransform;
CGSize naturalSize = CGSizeApplyAffineTransform(track.naturalSize, videoTransform);
naturalSize = CGSizeMake(fabs(naturalSize.width), fabs(naturalSize.height));


AVMutableVideoComposition *composition = [AVMutableVideoComposition videoCompositionWithPropertiesOfAsset:asset];
UIImage *img = [self imageWithImage:[UIImage imageNamed:@"white_image"] convertToSize:naturalSize];
[self addOverlayImage:img ToVideo:composition inSize:naturalSize];


AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = range;
composition.instructions = @[instruction];


AVAssetExportSession *_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
_assetExport.videoComposition = composition;



NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"exported-%d.mov", arc4random() % 100000]];
unlink([exportPath UTF8String]);
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];

_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
_assetExport.outputURL = exportUrl;
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler:^{

switch (_assetExport.status) {

case AVAssetExportSessionStatusFailed:
break;

case AVAssetExportSessionStatusExporting:
break;

case AVAssetExportSessionStatusCompleted:{

dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Successful!!!");
AVAsset *finalAsset = [AVAsset assetWithURL:_assetExport.outputURL];
completionBlock(finalAsset);
});
}
break;

default:
break;
}
}];
}

现在有一个带有叠加图像的视频 Assets 。唯一要做的就是将原始视频和导出的视频 Assets 结合起来。导出的 Assets 应该是底层,原始 Assets 应该是顶层。

关于ios - 如何将背景图像设置为 AVMutableVideoCompositionInstruction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33368060/

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