8

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

我尝试使用 AVMutableComposition,并将 mov 文件加载为 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

我不确定上述方法是否可行,但我对我做错了什么的可能性持开放态度。但是,如果有人知道另一种方法来完成我想要实现的目标,那么任何帮助都将不胜感激。

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

谢谢您的帮助!

4

3 回答 3

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);
        }
    }];
}
于 2014-09-29T09:29:27.363 回答
6

因为 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");
    }
}];
于 2012-11-14T02:27:31.890 回答
0

对其他人来说只是小信息(我不知道..)如果您有视频文件(在我的情况下是 mp4),您只能使用 AVAudioPlayer 播放音频。您不需要从视频中提取(或转换)音频。

于 2016-10-27T12:06:04.403 回答