2

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

+ (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);
 }       
 ];


}

但是在为“NSRangeException”记录“c”后出现错误,原因:

* -[__NSArrayM objectAtIndex:]: 索引 0 超出空数组的范围。

音频文件位于

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

有小费吗?谢谢。

4

2 回答 2

11

首先我应该说你给出的音频格式不正确,

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

那个 NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"1.aiff"];

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

    -(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 !");
 }
 ];


 }

传递最终文件的videoURLaudioURL保存路径

于 2013-02-20T08:09:07.953 回答
0

发生这种情况是因为音频或视频文件尚未完全写入,您正在尝试合并它们。确保您使用完成处理程序来确保完整写入特定路径的音频和视频文件。

于 2021-01-21T19:46:54.393 回答