/**
音视频合成
@param assetURL 原始视频路径
@param BGMPath 背景音乐路径
@param needOriginalVoice 是否添加原视频的声音
@param videoVolume 视频音量
@param BGMVolume 背景音乐音量
@param completionHandle 合成后的回调
*/
(void)editVideoSynthesizeVieoPath:(NSURL *)assetURL BGMPath:(NSURL *)BGMPath needOriginalVoice:(BOOL)needOriginalVoice videoVolume:(CGFloat)videoVolume BGMVolume:(CGFloat)BGMVolume complition:(void (^)(NSURL *outputPath,BOOL isSucceed)) completionHandle{
// 素材
AVAsset *asset = [AVAsset assetWithURL:assetURL];
AVAsset *audioAsset = [AVAsset assetWithURL:BGMPath];
// 分离素材
AVAssetTrack *videoAssetTrack = [[asset tracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0];//视频素材
AVAssetTrack *audioAssetTrack = [[audioAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];//音频素材
// 编辑视频环境
AVMutableComposition *composition = [[AVMutableComposition alloc]init];
// 视频素材加入视频轨道
AVMutableCompositionTrack *videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:nil];
// 音频素材加入音频轨道
AVMutableCompositionTrack *audioCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration) ofTrack:audioAssetTrack atTime:kCMTimeZero error:nil];
// 是否加入视频原声
AVMutableCompositionTrack *originalAudioCompositionTrack = nil;
if (needOriginalVoice) {
AVAssetTrack *originalAudioAssetTrack = [[asset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];
originalAudioCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[originalAudioCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAssetTrack.timeRange.duration) ofTrack:originalAudioAssetTrack atTime:kCMTimeZero error:nil];
}
// 导出素材
AVAssetExportSession *exporter = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
// 音量控制
exporter.audioMix = [self buildAudioMixWithVideoTrack:originalAudioCompositionTrack VideoVolume:videoVolume BGMTrack:audioCompositionTrack BGMVolume:BGMVolume controlVolumeRange:kCMTimeZero];
// 设置输出路径
NSURL *outputPath = [self exporterPath];
exporter.outputURL = [self exporterPath];
exporter.outputFileType = AVFileTypeMPEG4;//指定输出格式
[exporter exportAsynchronouslyWithCompletionHandler:^{
switch ([exporter status]) {
case AVAssetExportSessionStatusFailed: {
NSLog(@"合成失败:%@",[[exporter error] description]);
completionHandle(outputPath,NO);
} break;
case AVAssetExportSessionStatusCancelled: {
completionHandle(outputPath,NO);
} break;
case AVAssetExportSessionStatusCompleted: {
completionHandle(outputPath,YES);
} break;
default: {
completionHandle(outputPath,NO);
} break;
}
}];
}
2018-01-30
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- #星際播報 2018/01/30 NS1.30.07.21 #Kin252超频黃人 #祈禱文: 今天我是超頻的黃人...
- 经济学家富兰克.H.奈特:决定一个人富有的三个条件,一是出身,二是运气,三是努力,而这三者之中,努力是最微不足道的...