iphone ios 音频添加,合成,替换到视频(里的音轨)

来源:互联网 发布:三大运营商的网络制式 编辑:程序博客网 时间:2024/04/28 06:43

如果对av foundation 不熟悉得话,建议先看看wwdc 2010 关于av foundation得讲座。http://blog.csdn.net/linzhiji/article/details/6752722


I have a video file and an audio file. Is it possible to merge it to one video with with sound file. I think AVMutableComposition should help me, but I still dont understand how. any advices?


AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];AVMutableComposition* mixComposition = [AVMutableComposition composition];AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio                                                                                     preferredTrackID:kCMPersistentTrackID_Invalid];[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)                                     ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]                                      atTime:kCMTimeZero error:nil];AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo                                                                                     preferredTrackID:kCMPersistentTrackID_Invalid];[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)                                ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]                                 atTime:kCMTimeZero error:nil];AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition                                                                       presetName:AVAssetExportPresetPassthrough];   NSString* videoName = @"export.mov";NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];NSURL    *exportUrl = [NSURL fileURLWithPath:exportPath];if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) {    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];}_assetExport.outputFileType = @"com.apple.quicktime-movie";DLog(@"file type %@",_assetExport.outputFileType);_assetExport.outputURL = exportUrl;_assetExport.shouldOptimizeForNetworkUse = YES;[_assetExport exportAsynchronouslyWithCompletionHandler: ^(void ) {                  // your completion code here     }        } ];


原创粉丝点击