// 合成 我的视频大小都是一样的 所以没有对他们进行缩放裁剪等操作
- (void)mergeAndExportVideos {
if (videoArray.count==0) {
return;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
/// 从mixComposition取出音视频 轨道
AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime totalDuration = kCMTimeZero;
NSError *error = nil;
for (int i = 0; i < [videoArray count]; i++) {
AVAsset *asset = [AVAsset assetWithURL:[[NSURL alloc] initFileURLWithPath:videoArray[i]]];
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,asset.duration)
ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
atTime:totalDuration
error:nil];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,asset.duration)
ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
atTime:totalDuration
error:&error];
totalDuration = CMTimeAdd(totalDuration, asset.duration);
}
/// 合成视频文件存储的路径
NSString *path = [self getVideoMergeFilePathString];
NSURL *mergeFileURL = [NSURL fileURLWithPath:path];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];
exporter.outputURL = mergeFileURL;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
for (NSString *path in videoArray) {
[self delFromLocal:path];
}
[videoArray removeAllObjects];
// [self savePhotoCmare:path];//写入相册
}];
});
}