- (void)joinVideoAndMusic {
NSURL *videoURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"IMG_4400" ofType:@"m4v"]];
NSURL *videoURL2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"IMG_5170" ofType:@"mp4"]];
NSURL *musicURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"littlelucky" ofType:@"mp3"]];
AVAsset *asset1 = [AVAsset assetWithURL:videoURL1];
AVAsset *asset2 = [AVAsset assetWithURL:videoURL2];
AVAsset *asset3 = [AVAsset assetWithURL:musicURL];
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset1.duration) ofTrack:[asset1 tracksWithMediaType:AVMediaTypeVideo].firstObject atTime:kCMTimeZero error:nil];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset2.duration) ofTrack:[asset2 tracksWithMediaType:AVMediaTypeVideo].firstObject atTime:asset1.duration error:nil];
if (asset3 != nil) {
AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioTrack insertTimeRange:CMTimeRangeMake(CMTimeAdd(asset1.duration, asset2.duration), CMTimeAdd(asset1.duration, asset2.duration)) ofTrack:[asset3 tracksWithMediaType:AVMediaTypeAudio].firstObject atTime:kCMTimeZero error:nil];
_audioMix = [AVMutableAudioMix audioMix];
if (asset3) {
AVMutableAudioMixInputParameters *audioParam = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTrack];
[audioParam setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:CMTimeRangeMake(kCMTimeZero,asset1.duration)];
[audioParam setVolumeRampFromStartVolume:1 toEndVolume:0 timeRange:CMTimeRangeMake(asset1.duration, asset2.duration)];
[audioParam setTrackID:audioTrack.trackID];
_audioMix.inputParameters = @[audioParam];
}
}
NSString *outputPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/CustomMovie.mp4"];
unlink([outputPath UTF8String]); // 刪除當(dāng)前該路徑下的文件
NSURL *outputURL = [NSURL fileURLWithPath:outputPath];
_exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_exporter.outputURL = outputURL;
_exporter.outputFileType = AVFileTypeQuickTimeMovie;
_exporter.shouldOptimizeForNetworkUse = YES;
_exporter.audioMix = _audioMix;
__weak typeof(self) weakSelf = self;
[_exporter exportAsynchronouslyWithCompletionHandler:^{
// 視頻輸出完成
switch (weakSelf.exporter.status) {
case AVAssetExportSessionStatusCompleted:
[weakSelf saveVideoToLibrary:outputPath];
break;
case AVAssetExportSessionStatusFailed:
kShowPopTip(nil, @"導(dǎo)出失敗", nil);
break;
case AVAssetExportSessionStatusCancelled:
kShowPopTip(nil, @"導(dǎo)出取消", nil);
break;
default:
break;
}
}];
}
- (void)saveVideoToLibrary:(NSString *)outputPath {
if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {
__block PHObjectPlaceholder *placeholder;
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(outputPath)) {
NSError *error;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:[NSURL URLWithString:outputPath]];
placeholder = [createAssetRequest placeholderForCreatedAsset];
} error:&error];
if (error) {
kShowPopTip(self.view, error.description, nil);
}else {
kShowPopTip(self.view, @"視頻已保存到相冊", nil);
}
}
}
}
iOS 視頻拼接以及添加背景音樂
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枫匾,“玉大人架诞,你說我怎么就攤上這事「绍裕” “怎么了谴忧?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我沾谓,道長委造,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任均驶,我火速辦了婚禮昏兆,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘妇穴。我一直安慰自己爬虱,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布腾它。 她就那樣靜靜地躺著跑筝,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瞒滴。 梳的紋絲不亂的頭發(fā)上曲梗,一...
- 文/蒼蘭香墨 我猛地睜開眼控乾,長吁一口氣:“原來是場噩夢啊……” “哼谴蔑!你這毒婦竟也來了惨远?” 一聲冷哼從身側(cè)響起躺枕,我...
- 正文 年R本政府宣布秩彤,位于F島的核電站叔扼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏漫雷。R本人自食惡果不足惜瓜富,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望珊拼。 院中可真熱鬧食呻,春花似錦、人聲如沸澎现。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽剑辫。三九已至干旧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間妹蔽,已是汗流浹背椎眯。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 最近突發(fā)奇想想要了解點視頻處理的東西屎即,通過 google庙睡,github 等資源自給整理了這些資料,網(wǎng)上資源很多技俐,代...
- 分析:這是一個仿秒拍音視頻合成的demo饺谬。打開秒拍并拍攝一段視頻捂刺,然后進(jìn)入編輯界面谣拣,給拍攝的視頻添加背景音樂,可以...
- 這段時間由于工作需要族展,了解了一些關(guān)于iOS中視頻處理功能森缠,發(fā)現(xiàn)AVFoundation功能強(qiáng)大,今天聊一聊視頻截取...
- 這段時間由于工作需要仪缸,了解了一些關(guān)于iOS中視頻處理功能贵涵,發(fā)現(xiàn)AVFoundation功能強(qiáng)大,今天聊一聊視頻截取...
- 原理及步驟:1恰画、傳入視頻路徑宾茂、音樂路徑、視頻的時長參數(shù)等2拴还、用AVURLAsset分別讀取視頻跨晴、音樂的內(nèi)容3、新建...