5

我在我的 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");
}
4

3 回答 3

4

exportsession 有一个属性可以给出时间范围,尝试给出的时间范围比实际时间少一点(少几纳秒)

于 2013-04-09T05:03:09.457 回答
3

您可以从 AVAssetTrack 获取真实的视频时长。AVAsset 的持续时间有时比 AVAssetTrack 长。

像这样检查持续时间。

print(asset.duration.seconds.description)
print(videoTrack.timeRange.duration.description)

所以你可以改变这条线。

[videoComposition insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:sourceVideoTrack atTime:kCMTimeZero error:nil] ;

喜欢这个。

 [videoComposition insertTimeRange:sourceVideoTrack.timeRange, ofTrack:sourceVideoTrack atTime:kCMTimeZero error:nil] ;

对于迅捷5

videoComposition.insertTimeRange(sourceVideoTrack.timeRange, of: sourceVideoTrack, at: CMTime.zero)

然后你会避免最后一个黑框:)

希望这可以帮助仍然受苦的人。

于 2020-07-15T07:16:56.023 回答
1

只是想为有我特定问题的人写这篇文章。

AVMutableComposition我正在拍摄视频并尝试通过拍摄并缩放音频和视频组件的时间范围来加快/减慢它scaleTimeRange

将时间范围缩放到 2 倍速度或 3 倍速度有时会导致视频的最后几帧变黑。幸运的是,@Khushboo 的回答也解决了我的问题。

但是,我没有将导出器的 timeRange 减少几纳秒,而是将其与组合的持续时间相同,最终完美运行。

exporter?.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: composition.duration)

希望这可以帮助!

于 2018-12-17T20:59:02.120 回答