iOS从.mov文件中提取audio

我一直试图从一个.mov文件中提取audio一段时间,我似乎无法得到它的工作。 具体来说,我需要提取audio并将其保存为.aif或.aiff文件。

我已经尝试使用AVMutableComposition,并将AVS文件加载为AVAsset。 在最终使用AVAssetExportSession(将输出文件types设置为AVFileTypeAIFF,这是我需要的格式)之前,仅将audio轨道添加到AVMutableComposition,以便将文件写入aif。

我得到一个错误,说这个输出文件types是无效的,我不确定为什么:

*由于未捕获exception“NSInvalidArgumentException”而终止应用程序,原因:“输出文件types无效”

AVAssetExportSession *exporter; exporter = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetHighestQuality] ; exporter.audioMix = audioMix; exporter.outputURL=[NSURL fileURLWithPath:filePath]; exporter.outputFileType=AVFileTypeAIFF; //Error occurs on this line 

我不确定上述方法是否可行,但我可能会打开我只是做错事的可能性。 但是,如果有人知道另一种方法来实现我想要实现的目标,那么任何帮助都将不胜感激。

如果需要的话,我可以发布更详细的代码,但是现在我正在尝试一些其他的方法,所以现在有点杂乱。

谢谢您的帮助!

 -(void)getAudioFromVideo { float startTime = 0; float endTime = 10; [super viewDidLoad]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *audioPath = [documentsDirectory stringByAppendingPathComponent:@"soundOneNew.m4a"]; AVAsset *myasset = [AVAsset assetWithURL:[[NSBundle mainBundle] URLForResource:@"VideoName" withExtension:@"mp4"]]; AVAssetExportSession *exportSession=[AVAssetExportSession exportSessionWithAsset:myasset presetName:AVAssetExportPresetAppleM4A]; exportSession.outputURL=[NSURL fileURLWithPath:audioPath]; exportSession.outputFileType=AVFileTypeAppleM4A; CMTime vocalStartMarker = CMTimeMake((int)(floor(startTime * 100)), 100); CMTime vocalEndMarker = CMTimeMake((int)(ceil(endTime * 100)), 100); CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(vocalStartMarker, vocalEndMarker); exportSession.timeRange= exportTimeRange; if ([[NSFileManager defaultManager] fileExistsAtPath:audioPath]) { [[NSFileManager defaultManager] removeItemAtPath:audioPath error:nil]; } [exportSession exportAsynchronouslyWithCompletionHandler:^{ if (exportSession.status==AVAssetExportSessionStatusFailed) { NSLog(@"failed"); } else { NSLog(@"AudioLocation : %@",audioPath); } }]; } 

因为outputFileType是错误的。 对于.mov文件,它通常是@“com.apple.quicktime-movie”。 并且提取的audio是.mov格式。 为了确保,您可以使用此方法获取支持的输出types:

 NSArray *supportedTypeArray=exportSession.supportedFileTypes; for (NSString *str in supportedTypeArray) NSLog(@"%@",str); 

你可以用audio格式导出audio(.m4a,可以用AVAudioPlayer播放),方法如下:

 AVAssetExportSession *exportSession=[AVAssetExportSession exportSessionWithAsset:self.mAsset presetName:AVAssetExportPresetAppleM4A]; exportSession.outputURL=myUrl; exportSession.outputFileType=AVFileTypeAppleM4A; exportSession.timeRange=timeRange; [exportSession exportAsynchronouslyWithCompletionHandler:^{ if (exportSession.status==AVAssetExportSessionStatusFailed) { NSLog(@"failed"); } }]; 

只是对他人的小信息(我不知道那..)如果你有video文件(在我的情况下,它是MP4),你只能用AVAudioPlayer播放audio。 您无需从video中提取(或转换)audio。