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


}

しかし、「c」が「NSRangeException」のログに記録された後にエラーが発生します。理由:

* -[__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"];

そのaiffを作ります 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 に答える