iOS多個mov視頻合成

/**

*? 多個視頻合成為一個視頻輸出到指定路徑,注意區(qū)分是否3D視頻

*

*? @param tArray? ? ? 視頻文件NSURL地址

*? @param storePath? ? 沙盒目錄下的文件夾

*? @param storeName? ? 合成的文件名字

*? @param tbool? ? ? ? 是否3D視頻,YES表示是3D視頻

*? @param successBlock 成功block

*? @param failureBlcok 失敗block

*/

-(void)mergeVideoToOneVideo:(NSArray *)tArray toStorePath:(NSString *)storePath WithStoreName:(NSString *)storeName andIf3D:(BOOL)tbool success:(void (^)(void))successBlock failure:(void (^)(void))failureBlcok

{

? ? AVMutableComposition *mixComposition = [self mergeVideostoOnevideo:tArray];

? ? NSURL *outputFileUrl = [self joinStorePaht:storePath togetherStoreName:storeName];

? ? [self storeAVMutableComposition:mixComposition withStoreUrl:outputFileUrl andVideoUrl:[tArray objectAtIndex:0] WihtName:storeName andIf3D:tbool success:successBlock failure:failureBlcok];

}

/**

*? 多個視頻合成為一個

*

*? @param array 多個視頻的NSURL地址

*

*? @return 返回AVMutableComposition

*/

-(AVMutableComposition *)mergeVideostoOnevideo:(NSArray*)array

{

? ? AVMutableComposition* mixComposition = [AVMutableComposition composition];

? ? AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

? ? Float64 tmpDuration =0.0f;


? ? for (NSInteger i=0; i<array.count; i++)

? ? {


? ? ? ? AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:array[i] options:nil];


? ? ? ? CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);


? ? ? ? /**

? ? ? ? *? 依次加入每個asset

? ? ? ? *

? ? ? ? *? @param TimeRange 加入的asset持續(xù)時間

? ? ? ? *? @param Track? ? 加入的asset類型,這里都是video

? ? ? ? *? @param Time? ? ? 從哪個時間點(diǎn)加入asset,這里用了CMTime下面的CMTimeMakeWithSeconds(tmpDuration, 0),timesacle為0

? ? ? ? *

? ? ? ? */

? ? ? ? NSError *error;

? ? ? ? BOOL tbool = [a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:CMTimeMakeWithSeconds(tmpDuration, 0) error:&error];

? ? ? ? tmpDuration += CMTimeGetSeconds(videoAsset.duration);


? ? }

? ? return mixComposition;

}


//拼接url地址

-(NSURL *)joinStorePaht:(NSString *)sPath togetherStoreName:(NSString *)sName

{

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

? ? NSString *documentPath = [paths objectAtIndex:0];

? ? NSFileManager *fileManager = [NSFileManager defaultManager];


? ? NSString *storePath = [documentPath stringByAppendingPathComponent:sPath];

? ? BOOL isExist = [fileManager fileExistsAtPath:storePath];

? ? if(!isExist){

? ? ? ? [fileManager createDirectoryAtPath:storePath withIntermediateDirectories:YES attributes:nil error:nil];

? ? }

? ? NSString *realName = [NSString stringWithFormat:@"%@.mp4", sName];

? ? storePath = [storePath stringByAppendingPathComponent:realName];

? ? NSURL *outputFileUrl = [NSURL fileURLWithPath:storePath];

? ? return outputFileUrl;

}

/**

*? 存儲合成的視頻

*

*? @param mixComposition mixComposition參數(shù)

*? @param storeUrl? ? ? 存儲的路徑

*? @param successBlock? successBlock

*? @param failureBlcok? failureBlcok

*/

-(void)storeAVMutableComposition:(AVMutableComposition*)mixComposition withStoreUrl:(NSURL *)storeUrl andVideoUrl:(NSURL *)videoUrl WihtName:(NSString *)aName andIf3D:(BOOL)tbool success:(void (^)(void))successBlock failure:(void (^)(void))failureBlcok

{

? ? __weak typeof(self) welf = self;

? ? AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];

? ? _assetExport.outputFileType = @"com.apple.quicktime-movie";

? ? //? ? _assetExport.outputFileType = @"public.mpeg-4";

? ? _assetExport.outputURL = storeUrl;

? ? [_assetExport exportAsynchronouslyWithCompletionHandler:^{

? ? ? ? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

? ? ? ? ? ? //在系統(tǒng)相冊存儲一份

? ? ? ? ? ? UISaveVideoAtPathToSavedPhotosAlbum([storeUrl path], nil, nil, nil);

? ? ? ? ? ? //? ? ? ? ? ? 在本地沙盒中存儲一份,存儲成功返回YES,如果不想存到沙盒,直接返回調(diào)用<span style="font-family: Arial, Helvetica, sans-serif;">successBlock();</span>

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? BOOL successful = [welf.photoStore storeVideoImageScale:videoUrl WihtName:aName andIf3D:tbool];

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? if (successful) {

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? successBlock();

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? }else

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? failureBlcok();

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? //? ? ? ? ? ? ? ? ? ? ? ? });

? ? ? ? ? ? successBlock();

? ? ? ? });

? ? }];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容