xcode – AvMutableComposition在最后发出黑框
发布时间:2020-12-14 18:00:04 所属栏目:百科 来源:网络整理
导读:我在iOS应用程序中使用AVCaptureConnection捕获视频.之后,我在视频中添加一些图像作为CALayers.一切都运行正常,但在添加图像后,在结果视频的最后得到一个黑框.在此没有影响实际音频/视频的帧.对于音频我正在提取它并改变它的音高然后使用AVMutableCompositio
我在iOS应用程序中使用AVCaptureConnection捕获视频.之后,我在视频中添加一些图像作为CALayers.一切都运行正常,但在添加图像后,在结果视频的最后得到一个黑框.在此没有影响实际音频/视频的帧.对于音频我正在提取它并改变它的音高然后使用AVMutableComposition添加它.这是我正在使用的代码.请帮我解决我做错了什么,或者我需要添加其他东西.
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 = [[audioAsset 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,320); animComp.frameDuration = CMTimeMake(1,30); 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:5 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]; 这是我导出视频的方法 -(IBAction) exportMovie:(id)sender{ //successCheck = NO; NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *tempPath = [docPaths objectAtIndex:0]; //NSLog(@"Temp Path: %@",tempPath); NSString *fileName = [NSString stringWithFormat:@"%@/Final.MP4",tempPath]; NSFileManager *fileManager = [NSFileManager defaultManager] ; if([fileManager fileExistsAtPath:fileName ]){ NSError *ferror = nil ; [fileManager removeItemAtPath:fileName error:&ferror]; } NSURL *exportURL = [NSURL fileURLWithPath:fileName]; AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:cmp presetName:AVAssetExportPresetMediumQuality] ; exporter.outputURL = exportURL; exporter.videoComposition = animComp; //exporter.audioMix = audioMix; exporter.outputFileType = AVFileTypeQuickTimeMovie; [exporter exportAsynchronouslyWithCompletionHandler:^(void){ switch (exporter.status) { case AVAssetExportSessionStatusFailed:{ NSLog(@"Fail"); break; } case AVAssetExportSessionStatusCompleted:{ NSLog(@"Success video"); }); break; } default: break; } }]; NSLog(@"outside"); } 解决方法
有一个出口的属性给出时间范围,尝试给时间范围少于实际时间(少几纳秒)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |