ios avfoundation 合并音频到一个视频文件 avurlassettrack
2016-05-27 12:01
387 查看
下面这段代码可以将一组音频合并到一段视频中, 并输出到沙盒的Documents文件夹中,
其中有部分代码用到了自定义的类方法,比较简单,请大家自行改动运行
其中有部分代码用到了自定义的类方法,比较简单,请大家自行改动运行
NSError * error = nil; AVMutableComposition * composition = [AVMutableComposition composition]; AVURLAsset * videoAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:filePath] options:nil]; AVAssetTrack * videoAssetTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID: kCMPersistentTrackID_Invalid]; [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,videoAsset.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:&error]; CMTime audioStartTime = kCMTimeZero; for (int i = 1; i < 2 ; i++) { AVURLAsset * asset = [GG assetForBundle:[NSString stringWithFormat:@"%zd", i] type:@"mp3"]; AVAssetTrack * audioAssetTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]; AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID: kCMPersistentTrackID_Invalid]; [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,asset.duration) ofTrack:audioAssetTrack atTime:audioStartTime error:&error]; audioStartTime = CMTimeAdd(audioStartTime, asset.duration); } AVAssetExportSession* assetExport = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality]; assetExport.outputFileType =AVFileTypeQuickTimeMovie;// @"com.apple.quicktime-movie"; assetExport.outputURL = [NSURL fileURLWithPath:outFilePath]; [assetExport exportAsynchronouslyWithCompletionHandler: ^(void ) { switch (assetExport.status) { case AVAssetExportSessionStatusCompleted: // export complete NSLog(@"Export Complete"); break; case AVAssetExportSessionStatusFailed: NSLog(@"Export Failed"); NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]); // export error (see exportSession.error) break; case AVAssetExportSessionStatusCancelled: NSLog(@"Export Failed"); NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]); // export cancelled break; } }];
相关文章推荐
- iOS 自定义二维码条形码扫描识别
- iOS程序中的内存分配 栈区堆区全局区
- iOS 对afnetworking简单封装
- iOS版本控制工具(SVN,GIT,SourceTree)
- iOS Quartz2D使用(绘图路径)
- iOS 工具类
- IOS 自定义 无限图片轮播器开源库
- iOS 自定义照相机
- iOS知识---针对多个显示内容空间不定内容长度的显示问题Content Hugging 和 Content Compression Resistance
- iOS AVCaptureSession实现获取摄像头图像,并识别图片中身份证号码
- iOS 画字,画线,矩形,画图,动画
- ios 对FMDB 简单封装
- 禁用ios7 手势滑动返回功能
- iPhone上看到好的动效设计,如何保存成GIF
- iOS 点击通知栏消息跳转到指定的页面
- Nagios配置和命令介绍(二 )
- iOS 应用支持 IPv6
- 闲来无事,整理一下xcode快捷键全集!
- github上很好的iOS资源集锦
- iOS 项目真机调试