http://www.mindfiresolutions.com/Merging-Two-Videos--iPhoneiPad-Tip--1396.php



Suppose you have two video files with name video1.mp4 and video2.mp4 and want to merge them into a single video programmatically this tip might help you. Follow the instructions given below-
 
First, we need to add following frameworks in our project:
a)AVFoundation  framework 
b)Asset Library framework 
c)Media Player Framework
d)Core Media frame work
 
Then we need to import following in the view controller class :
 

#import <MediaPlayer/MediaPlayer.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <AVFoundation/AVBase.h>

@implementation MyVideoViewController

 - (void) mergeTwoVideo

{

    AVMutableComposition* composition = [AVMutableCompositioncomposition];

NSString* path1 = [[NSBundlemainBundle] pathForResource:@"video1"ofType:@"mp4"];

NSString* path2 = [[NSBundlemainBundle] pathForResource:@"video2"ofType:@"mp4"];

 AVURLAsset* video1 = [[AVURLAssetalloc]initWithURL:[NSURLfileURLWithPath:path1] options:nil];

    AVURLAsset* video2 = [[AVURLAssetalloc]initWithURL:[NSURLfileURLWithPath:path2] options:nil];

    AVMutableCompositionTrack * composedTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo 

                                                                      preferredTrackID:kCMPersistentTrackID_Invalid]; 

        [composedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video1.duration)

                         ofTrack:[[video1 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]

                          atTime:kCMTimeZero

                           error:nil];

    [composedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video2.duration) 

                         ofTrack:[[video2 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 

                          atTime:video1.duration

                           error:nil];

 

 

NSString* documentsDirectory= [selfapplicationDocumentsDirectory];

NSString* myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"merge_video.mp4"];

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

     if([[NSFileManagerdefaultManager] fileExistsAtPath:myDocumentPath])

    {

        [[NSFileManagerdefaultManager] removeItemAtPath:myDocumentPath error:nil];

    }

VAssetExportSession *exporter = [[[AVAssetExportSessionalloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality] autorelease];

 

exporter.outputURL=url;

   exporter.outputFileType = @"com.apple.quicktime-movie";

    exporter.shouldOptimizeForNetworkUse = YES;

    [exporter exportAsynchronouslyWithCompletionHandler:^{

         switch ([exporter status]) {

             caseAVAssetExportSessionStatusFailed:

                 break;

             caseAVAssetExportSessionStatusCancelled:

                 break;

             caseAVAssetExportSessionStatusCompleted:

                break;

            default:

                break;

        }

     }];

 }

 // The import will be completed only when control reaches to  Handler block
 
 

- (NSString*) applicationDocumentsDirectory

{

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

    return basePath;

}


      
Posted by k_ben