首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

ios视频跟音频的合成

2013-12-20 
ios视频和音频的合成#pragma mark 下面是合成视频的方法- (void)vision:(PBJVision *)vis

ios视频和音频的合成

#pragma mark =====下面是合成视频的方法===========

- (void)vision:(PBJVision *)vision capturedVideo:(NSDictionary *)videoDict error:(NSError *)error

{

? ? _recording = NO; //这句可能是控制完成存储之后就不能再录的。。。

?

? ? if (error) {

? ? ? ? NSLog(@"encounted an error in video capture (%@)", error);

? ? ? ? return;

? ? }

?

? ? _currentVideo = videoDict;

? ? NSString *videoPath = [_currentVideo? objectForKey:PBJVisionVideoPathKey];

? ? NSLog(@"_currentVideo==%@",_currentVideo);

? ? NSLog(@"voideoPath==%@",videoPath);

?? ?

? ? //合成视频文件url的时候,必须在前面拼接一个file://因为刚才录制视频后存到沙盒里面的是以/private开头的。

? ? NSString *videoUrlPath = [NSString stringWithFormat:@"file://%@",videoPath];

? ? NSString *path = [[NSBundlemainBundle] pathForResource:@"angry"ofType:@"wav"];

? ? NSURL *audioUrl = [NSURL fileURLWithPath:path];

?? ?

? ? //下面两句是分别取得视频和声音文件的url,以供合成用。

? ? AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil];

? ? NSLog(@"audioAsset===%@==",audioAsset);

? ? AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:videoUrlPath] options:nil];

? ? NSLog(@"videoAsset===%@==",videoAsset);

?

? ? //下面就是合成的过程了。

? ? AVMutableComposition* mixComposition = [AVMutableCompositioncomposition];

?? ?

? ? 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"; //这里换成wmv格式的就不行了。

? ? NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];

? ? long long temp = [self fileSizeAtPath:exportPath];

? ? NSLog(@"temp===%lld",temp);

//? ? temp===381266

? ? NSLog(@"exportPaht === %@",exportPath);

? ? NSURL? ? *exportUrl = [NSURL fileURLWithPath:exportPath];

?? ?

? ? if ([[NSFileManagerdefaultManager] fileExistsAtPath:exportPath])

? ? {

? ? ? ? [[NSFileManagerdefaultManager] removeItemAtPath:exportPath error:nil];

? ? }

?? ?

? ? _assetExport.outputFileType = @"com.apple.quicktime-movie";

? ? NSLog(@"file type %@",_assetExport.outputFileType);

? ? _assetExport.outputURL = exportUrl;

? ? _assetExport.shouldOptimizeForNetworkUse = YES;

?? ?

? ? //下面是按照上面的要求合成视频的过程。

? ? [_assetExport exportAsynchronouslyWithCompletionHandler:

?? ? ^(void ) {

?? ? ? ? // 下面是把视频存到本地相册里面,存储完后弹出对话框。

?? ? ? ? [_assetLibrarywriteVideoAtPathToSavedPhotosAlbum:[NSURLURLWithString:exportPath] completionBlock:^(NSURL *assetURL, NSError *error1) {

?? ? ? ? ? ? UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"好的!" message: @"整合并保存成功!"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delegate:nil

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cancelButtonTitle:@"OK"

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? otherButtonTitles:nil];

?? ? ? ? ? ? [alert show];

? ? ? ? ? ? ?

?? ? ? ? }];

?? ? }

?? ? ];

?

?? ?

?

}

?

?

?

?

?

?

?

?

//单个文件的大小

- (long long) fileSizeAtPath:(NSString*) filePath{

?? ?

? ? //

? ? //? ? NSData* data = [NSData dataWithContentsOfFile:[VoiceRecorderBaseVC getPathByFileName:_convertAmr ofType:@"amr"]];

? ? //? ? NSLog(@"amrlength = %d",data.length);

? ? //? ? NSString * amr = [NSString stringWithFormat:@"amrlength = %d",data.length];

?? ?

? ? NSFileManager* manager = [NSFileManagerdefaultManager];

?? ?

? ? if ([manager fileExistsAtPath:filePath]){

?? ? ? ?

? ? ? ? return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

? ? }

? ? return 0;

?

}

?

?

热点排行