【问题标题】:AvMutableComposition issues having black frame at the endAvMutableComposition 问题末尾有黑框
【发布时间】:2013-04-05 12:24:33
【问题描述】:

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

【问题讨论】:

  • 你在哪一部分改变了音调?我希望更改导出音频的音高,但遇到了一些麻烦...

标签: xcode avfoundation avcapturesession avmutablecomposition


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以从 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] ;
    

    对于 Swift 5

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

    那么你会避开最后一个黑框:)

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

    【讨论】:

    • 这绝对是我的情况。感谢您引起我的注意;-)
    【解决方案3】:

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

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

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

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

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      相关资源
      最近更新 更多