-
-
Save mantor3000/5127139 to your computer and use it in GitHub Desktop.
merge video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (firstAsset !=nil && secondAsset!=nil) { | |
[activityView startAnimating]; | |
// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances. | |
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init]; | |
// 2 - Video track | |
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo | |
preferredTrackID:kCMPersistentTrackID_Invalid]; | |
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, firstAsset.duration) | |
ofTrack:[[firstAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; | |
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, secondAsset.duration) | |
ofTrack:[[secondAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:firstAsset.duration error:nil]; | |
// 3 - Audio track | |
if (audioAsset!=nil){ | |
AVMutableCompositionTrack *AudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio | |
preferredTrackID:kCMPersistentTrackID_Invalid]; | |
[AudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration)) | |
ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; | |
} | |
// 4 - Get path | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent: | |
[NSString stringWithFormat:@"mergeVideo-%d.mov",arc4random() % 1000]]; | |
NSURL *url = [NSURL fileURLWithPath:myPathDocs]; | |
// 5 - Create exporter | |
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition | |
presetName:AVAssetExportPresetHighestQuality]; | |
exporter.outputURL=url; | |
exporter.outputFileType = AVFileTypeQuickTimeMovie; | |
exporter.shouldOptimizeForNetworkUse = YES; | |
[exporter exportAsynchronouslyWithCompletionHandler:^{ | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[self exportDidFinish:exporter]; | |
}); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment