Видео треснуло при объединении видео с помощью AVMutableComposition

Я объединяю видео с помощью AVMutableComposition с приведенным ниже кодом:

- (void)MergeAndSave_internal{

    AVMutableComposition *composition = [AVMutableComposition composition];
    AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.frameDuration = CMTimeMake(1,30);
    videoComposition.renderScale = 1.0;

    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];

    NSLog(@"%@",videoPathArray);

    float time = 0;
    CMTime startTime = kCMTimeZero;

    for (int i = 0; i<videoPathArray.count; i++) {

        AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[videoPathArray objectAtIndex:i]] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]];

        NSError *error = nil;

        BOOL ok = NO;
        AVAssetTrack *sourceVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        AVAssetTrack *sourceAudioTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

        CGSize temp = CGSizeApplyAffineTransform(sourceVideoTrack.naturalSize, sourceVideoTrack.preferredTransform);
        CGSize size = CGSizeMake(fabsf(temp.width), fabsf(temp.height));
        CGAffineTransform transform = sourceVideoTrack.preferredTransform;

        videoComposition.renderSize = sourceVideoTrack.naturalSize;
        if (size.width > size.height) {

            [layerInstruction setTransform:transform atTime:CMTimeMakeWithSeconds(time, 30)];
        } else {


            float s = size.width/size.height;


            CGAffineTransform newe = CGAffineTransformConcat(transform, CGAffineTransformMakeScale(s,s));

            float x = (size.height - size.width*s)/2;

            CGAffineTransform newer = CGAffineTransformConcat(newe, CGAffineTransformMakeTranslation(x, 0));

            [layerInstruction setTransform:newer atTime:CMTimeMakeWithSeconds(time, 30)];
        }
        if(i==0){
             [compositionVideoTrack setPreferredTransform:sourceVideoTrack.preferredTransform];
        }
        ok = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]) ofTrack:sourceVideoTrack atTime:startTime error:&error];


        ok = [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [sourceAsset duration]) ofTrack:sourceAudioTrack atTime:startTime error:nil];

        if (!ok) {
            {
                [radialView4 setHidden:YES];
                NSLog(@"Export failed: %@", [[self.exportSession error] localizedDescription]);
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Something Went Wrong :("  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
                [alert show];
                [radialView4 setHidden:YES];
                break;
            }

        }

        startTime = CMTimeAdd(startTime, [sourceAsset duration]);

    }


    instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];
    instruction.timeRange = compositionVideoTrack.timeRange;

    videoComposition.instructions = [NSArray arrayWithObject:instruction];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"RampMergedVideo.mov"]];
    unlink([myPathDocs UTF8String]);
    NSURL *url = [NSURL fileURLWithPath:myPathDocs];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:composition
                                                                      presetName:AVAssetExportPreset1280x720];
    exporter.outputURL=url;
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    exporter.shouldOptimizeForNetworkUse = YES;
    [exporter exportAsynchronouslyWithCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{

            switch ([exporter status]) {
                case AVAssetExportSessionStatusFailed:
                    NSLog(@"Export failed: %@", [exporter error]);
                    break;
                case AVAssetExportSessionStatusCancelled:
                    NSLog(@"Export canceled");
                    break;
                case AVAssetExportSessionStatusCompleted:{
                    NSLog(@"Export successfully");

                }
                default:
                    break;
            }
            if (exporter.status != AVAssetExportSessionStatusCompleted){
                NSLog(@"Retry export");

            }

        });
    }];

}

Но видео выглядит треснувшим при сохранении в систему и воспроизведении в Quick Time Player. Я думаю что проблема в CFAffline transform. Кто-нибудь может посоветовать?

Вот треснувший экран в середине видео: введите здесь описание изображениявведите здесь описание изображения


person Manoj Arun S    schedule 16.02.2015    source источник
comment
вы можете использовать параметр timeRange в AVAssetExportSession и вырезать нужный вам диапазон из основного актива. Это может помочь. Я столкнулся с той же проблемой, и следующая ссылка поможет мне решить проблему. вот ссылка stackoverflow.com/a/8562104/3400991   -  person Shobhakar Tiwari    schedule 25.02.2015


Ответы (1)


Вы не установили для видеокомпозиции значение AVAssetExportSession. Попробуйте сделать это exporter.videoComposition = videoComposition;. Не пробовал это, но должно работать.

person Sumeet    schedule 26.02.2015