2011-08-04 127 views
9

我一直在尝试从.mov文件中提取音频一段时间,而我似乎无法使其工作。具体来说,我需要提取音频并将其保存为.aif或.aiff文件。iOS从.mov文件中提取音频

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

我得到一个错误说这个输出文件类型是无效的,我不能确定原因:

*终止应用程序由于未捕获的异常“NSInvalidArgumentException”,理由是:“无效的输出文件类型”

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 

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

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

感谢您的帮助!

+0

任何人有任何想法?我真的坚持这一点。 – Grinneh

+0

我也想从视频文件中提取音频。你能帮助我吗? – Harsh

回答

7

因为outputFileType是错误的。对于.mov文件,它通常是@“com.apple.quicktime-movie”。并且提取的音频是.mov格式。为了确保,你可以使用这个方法来获取支持的输出类型:

NSArray *supportedTypeArray=exportSession.supportedFileTypes;  

for (NSString *str in supportedTypeArray)  
    NSLog(@"%@",str); 

而且你可以在音频格式(.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"); 
    } 
}]; 
11
-(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); 
     } 
    }]; 
} 
+0

其工作熟练...谢谢你的人... @rajesh,我有一个问题,我必须创建获取音频反向,所以,有没有任何解决方案,以获得反向音频的任何音频文件? 提供您的评论.. – Mehul

0

别人只是小的信息(我不知道。) 如果您有视频文件(在我的情况下,它是MP4),你可以用AVAudioPlayer仅播放音频。 您无需从视频中提取(或转换)音频。