ios 视频拼接

//

//  CVViewController.m

//  combineVideo

//

//  Created by xiaopeng_wang on 13-11-15.

//  Copyright (c) 2013 xiaopeng_wang. All rights reserved.

//


#import "CVViewController.h"


@interface CVViewController ()


@end


@implementation CVViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent(); //Debug purpose - used to calculate the total time taken


    AVMutableComposition *saveComposition = [AVMutableComposition composition];

    

    AVMutableCompositionTrack *compositionVideoTrack = [saveComposition addMutableTrackWithMediaType:AVMediaTypeVideo 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];

    

    float time = 0;

    

    

    for (int i = 0; i<2; i++) {

        

        

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

        

        AVURLAsset *sourceAsset = nil;

        

        if(i == 0)

        {

            //Here where load our movie Assets using AVURLAsset

            sourceAsset= [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"gizmo" ofType: @"mp4"]] options:nil];

        }

        else

        {

             sourceAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource: @"Miniature" ofType: @"mp4"]] options:nil];

        }

        

        

        NSError *error = nil;

        

        BOOL ok = NO;

        AVAssetTrack *sourceVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] 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 new = CGAffineTransformConcat(transform, CGAffineTransformMakeScale(s,s));

            

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

            

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

            

            [layerInstruction setTransform:newer atTime:CMTimeMakeWithSeconds(time, 30)];

        }

        

        

        ok = [compositionVideoTrack insertTimeRange:sourceVideoTrack.timeRange ofTrack:sourceVideoTrack atTime:[saveComposition duration] error:&error];


        if (!ok) {

            // Deal with the error.

            NSLog(@"something went wrong");

        }

        

        NSLog(@"\n source asset duration is %f \n source vid track timerange is %f %f \n composition duration is %f \n composition vid track time range is %f %f",CMTimeGetSeconds([sourceAsset duration]), CMTimeGetSeconds(sourceVideoTrack.timeRange.start),CMTimeGetSeconds(sourceVideoTrack.timeRange.duration),CMTimeGetSeconds([saveComposition duration]), CMTimeGetSeconds(compositionVideoTrack.timeRange.start),CMTimeGetSeconds(compositionVideoTrack.timeRange.duration));

        

        time += CMTimeGetSeconds(sourceVideoTrack.timeRange.duration);

        

    }

    

    instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction];

    instruction.timeRange = compositionVideoTrack.timeRange;

    videoComposition.instructions = [NSArray arrayWithObject:instruction];



    NSString *tempPath = NSTemporaryDirectory();


    

  //  7. Get file path for of the final video.

        NSString *path = [tempPath stringByAppendingPathComponent:@"mergedvideo.mov"];

    if([[NSFileManager defaultManager] fileExistsAtPath:path])

    {

        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];

    }

    

    NSURL *url = [[NSURL alloc] initFileURLWithPath: path];

    

//    Create the AVAssetExportSession and set the preset to it.

//    The completion handler will be called upon the completion of the export.

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetHighestQuality];

    exporter.outputURL=url;

    

    exporter.outputFileType=[[exporter supportedFileTypes] objectAtIndex:0];

    

    [exporter exportAsynchronouslyWithCompletionHandler:^{

        NSLog(@"export completed : %lf",CFAbsoluteTimeGetCurrent()-currentTime);

    }];


}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值