gpt4 book ai didi

ios - 使用 AVMutableVideoComposition 导出 CALayer

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:36 25 4
gpt4 key购买 nike

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

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

我是漏掉了什么还是做错了什么。

最佳答案

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

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

错了,有点搞笑,反正改成这个

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

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

关于ios - 使用 AVMutableVideoComposition 导出 CALayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371678/

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