自己遇到的坑:
1.視頻和音頻需要分開合成隆豹;
2.豎屏拍攝的視頻帆竹,合成后改變了方向沟使, 只好自己更改方向
實(shí)現(xiàn)步驟:
在.h中添加合成時(shí)需要調(diào)用的方法
//多個(gè)分段視頻合成
/*
array為本地保存的短視頻數(shù)組,元素為NSURL類型 比如:
file:///var/mobile/Containers/Data/Application/BBF38CEF-BB5D-40BA-82D5-4A9508BEA4F9/Library/Caches/我的短視頻/video_0.mov,
file:///var/mobile/Containers/Data/Application/BBF38CEF-BB5D-40BA-82D5-4A9508BEA4F9/Library/Caches/我的短視頻/video_1.mov
*/
- (void)synthesisMedia:(NSMutableArray *)array;
在.m中添加
#import <AVFoundation/AVFoundation.h>
#define ShootVideoPath @"錄制視頻"
#define ShootVideoName @"我的短視頻"
- 1.創(chuàng)建一個(gè)AVComposition的媒體數(shù)據(jù)
-(AVMutableComposition *)mergeVideostoOnevideo:(NSArray*)array{
AVMutableComposition* mixComposition = [AVMutableComposition composition];
//視頻通道
AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
//音頻通道
AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
Float64 tmpDuration =0.0f;
//合成視頻
for (NSInteger i=0; i<array.count; i++){
AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:array[i] options:nil];
CMTime time = [videoAsset duration];
NSInteger seconds = ceil(time.value/time.timescale);
NSLog(@"第%ld個(gè)視頻時(shí)長(zhǎng) = %ld",i,seconds);
//視頻采集
CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
NSError *error;
//合成視頻
[compositionVideoTrack insertTimeRange:timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:CMTimeMakeWithSeconds(tmpDuration, 0) error:&error];
//合成音頻
[compositionAudioTrack insertTimeRange:timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMakeWithSeconds(tmpDuration, 0) error:&error];
tmpDuration = CMTimeGetSeconds(videoAsset.duration) + tmpDuration;
}
//豎屏錄制的視頻愚争,合成后會(huì)改變方向。所以手動(dòng)轉(zhuǎn)了一下
compositionVideoTrack.preferredTransform = CGAffineTransformMakeRotation(M_PI/2);
return mixComposition;
- 2.創(chuàng)建一個(gè)AVMutableComposition并調(diào)用上一步的方法,以此合成視頻
//分段多個(gè)視頻 進(jìn)行合成
- (void)synthesisMedia:(NSMutableArray *)array{
AVMutableComposition *mixComposition = [self mergeVideostoOnevideo:array];
AVAssetExportSession* assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
assetExport.outputFileType = AVFileTypeMPEG4;
NSURL *filUrl = [self getVideosURLPath:ShootVideoName];//自定義的輸出路徑
assetExport.outputURL = filUrl;//視頻的輸出路徑
assetExport.shouldOptimizeForNetworkUse = YES;
//視頻合成
[assetExport exportAsynchronouslyWithCompletionHandler:^{
//回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:filUrl options:nil];
CMTime time = [videoAsset duration];
NSInteger seconds = ceil(time.value/time.timescale);
//在系統(tǒng)相冊(cè)存儲(chǔ)一份(需要開啟相冊(cè)的權(quán)限,不然閃退)
//UISaveVideoAtPathToSavedPhotosAlbum([filUrl path], nil, nil, nil);
//7玫小凉敲!filUrl為視頻的輸出路徑!捐顷!
});
}];
}
- 3.自定義輸出路徑
//拼接輸出路徑
- (NSURL *)getVideosURLPath:(NSString *)videoName{
NSString *documents = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
documents = [documents stringByAppendingPathComponent:ShootVideoPath];
documents = [self action_addFiles:documents];//新建文件并返回地址
NSString *failPath = [documents stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",videoName]];
NSURL *filUrl = [NSURL fileURLWithPath:failPath];
return filUrl;
}
//新建文件并返回地址
- (NSString *)action_addFiles:(NSString *)path{
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
//文件夾已存在
} else {
//創(chuàng)建文件夾
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
return path;
}