iOS视频添加背景音乐同时保留原音
发布时间 - 2026-01-11 00:26:34 点击率:次话不多说,请看代码:
//抽取原视频的音频与需要的音乐混合
-(void)addmusic:(id)sender
{
[MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];
AVMutableComposition *composition =[AVMutableCompositioncomposition];
audioMixParams =[[NSMutableArrayalloc]initWithObjects:nil];
//录制的视频
NSURL *video_inputFileUrl =[NSURLfileURLWithPath:self.videoPath];
AVURLAsset *songAsset =[AVURLAssetURLAssetWithURL:video_inputFileUrloptions:nil];
CMTime startTime =CMTimeMakeWithSeconds(0,songAsset.duration.timescale);
CMTime trackDuration =songAsset.duration;
//获取视频中的音频素材
[selfsetUpAndAddAudioAtPath:video_inputFileUrltoComposition:compositionstart:startTimedura:trackDurationoffset:CMTimeMake(14*44100,44100)];
//本地要插入的音乐
NSString *bundleDirectory =[[NSBundlemainBundle]bundlePath];
NSString *path = [bundleDirectorystringByAppendingPathComponent:@"30secs.mp3"];
NSURL *assetURL2 =[NSURLfileURLWithPath:path];
//获取设置完的本地音乐素材
[selfsetUpAndAddAudioAtPath:assetURL2toComposition:compositionstart:startTimedura:trackDurationoffset:CMTimeMake(0,44100)];
//创建一个可变的音频混合
AVMutableAudioMix *audioMix =[AVMutableAudioMixaudioMix];
audioMix.inputParameters =[NSArrayarrayWithArray:audioMixParams];//从数组里取出处理后的音频轨道参数
//创建一个输出
AVAssetExportSession *exporter =[[AVAssetExportSessionalloc]
initWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
exporter.audioMix = audioMix;
exporter.outputFileType=@"com.apple.m4a-audio";
NSString* fileName =[NSStringstringWithFormat:@"%@.mov",@"overMix"];
//输出路径
NSString *exportFile =[NSStringstringWithFormat:@"%@/%@",[selfgetLibarayPath], fileName];
if([[NSFileManagerdefaultManager]fileExistsAtPath:exportFile]) {
[[NSFileManagerdefaultManager]removeItemAtPath:exportFileerror:nil];
}
NSLog(@"是否在主线程1%d",[NSThreadisMainThread]);
NSLog(@"输出路径===%@",exportFile);
NSURL *exportURL =[NSURLfileURLWithPath:exportFile];
exporter.outputURL = exportURL;
self.mixURL =exportURL;
[exporterexportAsynchronouslyWithCompletionHandler:^{
int exportStatus =(int)exporter.status;
switch (exportStatus){
caseAVAssetExportSessionStatusFailed:{
NSError *exportError =exporter.error;
NSLog(@"错误,信息: %@", exportError);
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
break;
}
caseAVAssetExportSessionStatusCompleted:{
NSLog(@"是否在主线程2%d",[NSThreadisMainThread]);
NSLog(@"成功");
//最终混合
[selftheVideoWithMixMusic];
break;
}
}
}];
}
//最终音频和视频混合
-(void)theVideoWithMixMusic
{
NSError *error =nil;
NSFileManager *fileMgr =[NSFileManagerdefaultManager];
NSString *documentsDirectory =[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString *videoOutputPath =[documentsDirectorystringByAppendingPathComponent:@"test_output.mp4"];
if ([fileMgrremoveItemAtPath:videoOutputPatherror:&error]!=YES) {
NSLog(@"无法删除文件,错误信息:%@",[error localizedDescription]);
}
//声音来源路径(最终混合的音频)
NSURL *audio_inputFileUrl =self.mixURL;
//视频来源路径
NSURL *video_inputFileUrl = [NSURLfileURLWithPath:self.videoPath];
//最终合成输出路径
NSString *outputFilePath =[documentsDirectorystringByAppendingPathComponent:@"final_video.mp4"];
NSURL *outputFileUrl = [NSURLfileURLWithPath:outputFilePath];
if([[NSFileManagerdefaultManager]fileExistsAtPath:outputFilePath])
[[NSFileManagerdefaultManager]removeItemAtPath:outputFilePatherror:nil];
CMTime nextClipStartTime =kCMTimeZero;
//创建可变的音频视频组合
AVMutableComposition* mixComposition =[AVMutableCompositioncomposition];
//视频采集
AVURLAsset* videoAsset =[[AVURLAssetalloc]initWithURL:video_inputFileUrloptions:nil];
CMTimeRange video_timeRange =CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack*a_compositionVideoTrack = [mixCompositionaddMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrackinsertTimeRange:video_timeRangeofTrack:[[videoAssettracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0]atTime:nextClipStartTimeerror:nil];
//声音采集
AVURLAsset* audioAsset =[[AVURLAssetalloc]initWithURL:audio_inputFileUrloptions:nil];
CMTimeRange audio_timeRange =CMTimeRangeMake(kCMTimeZero,videoAsset.duration);//声音长度截取范围==视频长度
AVMutableCompositionTrack*b_compositionAudioTrack = [mixCompositionaddMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrackinsertTimeRange:audio_timeRangeofTrack:[[audioAssettracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]atTime:nextClipStartTimeerror:nil];
//创建一个输出
AVAssetExportSession* _assetExport =[[AVAssetExportSessionalloc]initWithAsset:mixCompositionpresetName:AVAssetExportPresetMediumQuality];
_assetExport.outputFileType =AVFileTypeQuickTimeMovie;
_assetExport.outputURL =outputFileUrl;
_assetExport.shouldOptimizeForNetworkUse=YES;
self.theEndVideoURL=outputFileUrl;
[_assetExportexportAsynchronouslyWithCompletionHandler:
^(void ) {
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
//播放
NSURL*url = [NSURLfileURLWithPath:outputFilePath];
MPMoviePlayerViewController *theMovie =[[MPMoviePlayerViewControlleralloc]initWithContentURL:url];
[selfpresentMoviePlayerViewControllerAnimated:theMovie];
theMovie.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
[theMovie.moviePlayerplay];
}
];
NSLog(@"完成!输出路径==%@",outputFilePath);
}
//通过文件路径建立和添加音频素材
- (void)setUpAndAddAudioAtPath:(NSURL*)assetURLtoComposition:(AVMutableComposition*)composition start:(CMTime)startdura:(CMTime)duraoffset:(CMTime)offset{
AVURLAsset *songAsset =[AVURLAssetURLAssetWithURL:assetURLoptions:nil];
AVMutableCompositionTrack *track =[compositionaddMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack *sourceAudioTrack =[[songAssettracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];
NSError *error =nil;
BOOL ok =NO;
CMTime startTime = start;
CMTime trackDuration = dura;
CMTimeRange tRange =CMTimeRangeMake(startTime,trackDuration);
//设置音量
//AVMutableAudioMixInputParameters(输入参数可变的音频混合)
//audioMixInputParametersWithTrack(音频混音输入参数与轨道)
AVMutableAudioMixInputParameters *trackMix =[AVMutableAudioMixInputParametersaudioMixInputParametersWithTrack:track];
[trackMixsetVolume:0.8fatTime:startTime];
//素材加入数组
[audioMixParamsaddObject:trackMix];
//Insert audio into track //offsetCMTimeMake(0, 44100)
ok = [trackinsertTimeRange:tRangeofTrack:sourceAudioTrackatTime:kCMTimeInvaliderror:&error];
}
#pragma mark - 保存路径
-(NSString*)getLibarayPath
{
NSFileManager *fileManager =[NSFileManagerdefaultManager];
NSArray* paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* path = [pathsobjectAtIndex:0];
NSString *movDirectory = [pathstringByAppendingPathComponent:@"tmpMovMix"];
[fileManagercreateDirectoryAtPath:movDirectorywithIntermediateDirectories:YESattributes:nilerror:nil];
return movDirectory;
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
# ios添加音乐
# ios视频添加音乐
# 视频添加音乐保留原声
# 讲解iOS开发中对音效和音乐播放的简单实现
# iOS App中实现播放音效和音乐功能的简单示例
# 实例解析iOS中音乐播放器应用开发的基本要点
# iOS开发中音频工具类的封装以及音乐播放器的细节控制
# iOS实现播放远程网络音乐的核心技术点总结
# ios开发:一个音乐播放器的设计与实现案例
# iOS利用AVPlayer播放网络音乐的方法教程
# iOS中关于音乐锁屏控制音乐(锁屏信息设置)的实例代码
# 运用iOS教你轻松制作音乐播放器
# iOS实现获取系统iTunes音乐的方法示例
# 创建一个
# 多说
# 错误信息
# 混音
# 里取出
# 视频采集
# NSStringstringWithFormat
# mov
# fileName
# NSFileManagerdefaultManager
# audio
# overMix
# fileExistsAtPath
# removeItemAtPath
# exportFileerror
# exportFile
# selfgetLibarayPath
# AVAssetExportSession
# exporter
# AVAssetExportSessionalloc
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
如何撰写建站申请书?关键要点有哪些?
如何在自有机房高效搭建专业网站?
如何续费美橙建站之星域名及服务?
php静态变量怎么调试_php静态变量作用域调试技巧【解答】
中山网站制作网页,中山新生登记系统登记流程?
Laravel Admin后台管理框架推荐_Laravel快速开发后台工具
js实现点击每个li节点,都弹出其文本值及修改
如何在IIS中新建站点并配置端口与物理路径?
PHP怎么接收前端传的文件路径_处理文件路径参数接收方法【汇总】
微信小程序 五星评分(包括半颗星评分)实例代码
Laravel如何生成URL和重定向?(路由助手函数)
Python文本处理实践_日志清洗解析【指导】
JS弹性运动实现方法分析
如何在万网自助建站平台快速创建网站?
Bootstrap CSS布局之列表
高防服务器:AI智能防御DDoS攻击与数据安全保障
如何快速辨别茅台真假?关键步骤解析
利用vue写todolist单页应用
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
深入理解Android中的xmlns:tools属性
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
Laravel Eloquent访问器与修改器是什么_Laravel Accessors & Mutators数据处理技巧
UC浏览器如何切换小说阅读源_UC浏览器阅读源切换【方法】
网站制作软件免费下载安装,有哪些免费下载的软件网站?
悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤
Python正则表达式进阶教程_复杂匹配与分组替换解析
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
Laravel如何处理CORS跨域问题_Laravel项目CORS配置与解决方案
Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】
实现点击下箭头变上箭头来回切换的两种方法【推荐】
用yum安装MySQLdb模块的步骤方法
新三国志曹操传主线渭水交兵攻略
Python数据仓库与ETL构建实战_Airflow调度流程详解
Bootstrap整体框架之JavaScript插件架构
JavaScript中的标签模板是什么_它如何扩展字符串功能
如何确认建站备案号应放置的具体位置?
Laravel观察者模式如何使用_Laravel Model Observer配置
个人摄影网站制作流程,摄影爱好者都去什么网站?
百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏
如何用腾讯建站主机快速创建免费网站?
Laravel如何实现用户密码重置功能?(完整流程代码)
如何用JavaScript实现文本编辑器_光标和选区怎么处理
Laravel怎么为数据库表字段添加索引以优化查询
长沙企业网站制作哪家好,长沙水业集团官方网站?
如何构建满足综合性能需求的优质建站方案?
JavaScript常见的五种数组去重的方式
android nfc常用标签读取总结
javascript中闭包概念与用法深入理解
做企业网站制作流程,企业网站制作基本流程有哪些?
html5的keygen标签为什么废弃_替代方案说明【解答】

