- (void)viewDidLoad
{ [superviewDidLoad];
self.array 里面放的是两个视频的地址 ,类型为NSurl
path 是你合并后的存放的地址路径
[self mergeAndExportVideos:self.array withOutPath:path];
}
- (void)mergeAndExportVideos:(NSMutableArray*)videosPathArraywithOutPath:(NSString*)outpath
{
AVMutableComposition*mixComposition =[[AVMutableComposition alloc]init];
//音频轨道
AVMutableCompositionTrack*audioTrack = [mixCompositionaddMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
//视频轨道
AVMutableCompositionTrack*videoTrack = [mixCompositionaddMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime totalDuration =kCMTimeZero;
for(inti =0; i < videosPathArray.count; i++) {
//AVURLAsset:AVAsset的子类,此类主要用于获取多媒体的信息,包括视频、音频的类型、时长、每秒帧数,其实还可以用来获取视频的指定位置的缩略图。
AVURLAsset*asset = [AVURLAssetassetWithURL:videosPathArray[i]];
NSError*erroraudio =nil;
//获取AVAsset中的音频
AVAssetTrack*assetAudioTrack = [[assettracksWithMediaType:AVMediaTypeAudio]firstObject];
//向通道内加入音频
BOOLba = [audioTrackinsertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
ofTrack:assetAudioTrack
atTime:totalDuration
error:&erroraudio];
NSLog(@"erroraudio:%@%d",erroraudio,ba);
NSError*errorVideo =nil;
//获取AVAsset中的视频
AVAssetTrack*assetVideoTrack = [[assettracksWithMediaType:AVMediaTypeVideo]firstObject];
//向通道内加入视频
BOOLbl = [videoTrackinsertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
ofTrack:assetVideoTrack
atTime:totalDuration
error:&errorVideo];
NSLog(@"errorVideo:%@%d",errorVideo,bl);
totalDuration =CMTimeAdd(totalDuration, asset.duration);
}
//创建合成后写入的路劲
NSURL*mergeFileURL = [NSURLfileURLWithPath:outpath];
if([[NSFileManagerdefaultManager]fileExistsAtPath:outpath])
{
NSLog(@"有文件");
return;
}
//这里开始导出合成后的视频
AVAssetExportSession*exporter = [[AVAssetExportSessionalloc]initWithAsset:mixComposition
presetName:AVAssetExportPreset640x480];
exporter.outputURL= mergeFileURL;
NSLog(@"%@",exporter.supportedFileTypes);
if([self.typeisEqualToString:@"mp4"]) {
exporter.outputFileType=AVFileTypeMPEG4;
}else
{
exporter.outputFileType=AVFileTypeQuickTimeMovie;
}
exporter.shouldOptimizeForNetworkUse=YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
//导出的状态
switch(exporter.status) {
caseAVAssetExportSessionStatusUnknown:
NSLog(@"exporter Unknow");
break;
caseAVAssetExportSessionStatusCancelled:
NSLog(@"exporter Canceled");
break;
caseAVAssetExportSessionStatusFailed:
//导出失败
NSLog(@"exporter Failed");
break;
caseAVAssetExportSessionStatusWaiting:
NSLog(@"exporter Waiting");
break;
caseAVAssetExportSessionStatusExporting:
NSLog(@"exporter Exporting");
break;
caseAVAssetExportSessionStatusCompleted:
//导出成功
NSLog(@"exporter Completed");
dispatch_async(dispatch_get_main_queue(), ^{
//这里是回到你的主线程做一些事情
});
break;
}
}];
}
}
}
- (void)clearCache
{
if([[NSFileManagerdefaultManager]fileExistsAtPath:self.outPath])
{
//这里是你可以删除你合成后的视频文件
//self.outPath 是你保存视频的路劲,当然你也可以通过该路劲删除它
NSLog(@"删除成功");
[[NSFileManager defaultManager] removeItemAtPath:self.outPath error:nil];
}
}