将音频与视频Objective-C合并 [英] Merging Audio with Video Objective-C

查看:81
本文介绍了将音频与视频Objective-C合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法将音频文件与视频文件合并:

I'm attempting to merge an audio file with a video file using:

+ (void)CompileFilesToMakeMovie:(NSString *) audioPath {
NSLog(@"a");
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSURL *audio_inputFileUrl = [[NSURL alloc] initFileURLWithPath:audioPath];



NSString *videoPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"input" ofType:@"mov"];
NSURL*    video_inputFileUrl = [NSURL fileURLWithPath:videoPath];

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *outputFilePath = [docsDir stringByAppendingPathComponent:@"OutputFile.mov"];
NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
NSLog(@"b");


CMTime nextClipStartTime = kCMTimeZero;

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

 NSLog(@"c");
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
 NSLog(@"d");


AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;

[_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
     UISaveVideoAtPathToSavedPhotosAlbum(outputFilePath, nil, nil, nil);
 }       
 ];


}

但我收到错误后 c记录为'NSRangeException',原因:

But I am getting an error after "c" is logged for 'NSRangeException', reason:


* - [__ NSArrayM objectAtIndex:]:index 0超出
数组的界限'。

* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'.

音频文件位于

  NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *docsDir = [dirPaths objectAtIndex:0];
  NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"%1.iaff"];

任何提示?
谢谢。

Any tips? Thanks.

推荐答案

首先,我要说你提供的音频格式不正确,

First of all I should say the audio format you have given is incorrect,

NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"%1.iaff"];

使 aiff NSString * soundFilePath = [ docsDir stringByAppendingPathComponent:@1.aiff];

如果你不确定你使用的代码,
此代码适用于我,

If you are not sure of the code you used, This code works for me,

    -(void) mergeAudio:(NSURL *) audioURL withVideo:(NSURL *) videoURL andSaveToPathUrl:(NSString *) savePath{

 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, audioAsset.duration)
 ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
 atTime:kCMTimeZero error:nil];

 AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
 presetName:AVAssetExportPresetPassthrough];

 NSURL    *savetUrl = [NSURL fileURLWithPath:savePath];

 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath])
 {
 [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
 }

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


 _assetExport.outputURL = savetUrl;
 _assetExport.shouldOptimizeForNetworkUse = YES;

 [_assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
 NSLog(@"fileSaved !");
 }
 ];


 }

通过 videoURL audioURL 保存路径

这篇关于将音频与视频Objective-C合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆