두개의 비디오 합치기
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> @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; } |
'컴퓨터 > 아이폰' 카테고리의 다른 글
아이폰 파일 매니져 (0) | 2014.02.18 |
---|---|
mov 파일 mp4로 변환하기 (0) | 2013.11.20 |
아이폰 자체 api를 사용해서 동영상에 이펙트 주는 사이트 (0) | 2013.11.20 |
ffmpeg 모바일 기기에서의 동영상 재생 - 다우 기술 홈페이지 (0) | 2013.11.20 |
아이폰 이미지들을 모아서 동영상으로 만드는 api (0) | 2013.11.20 |