从 .mov 文件中提取音频(iOS)

8
我一直在尝试从.mov文件中提取音频,但似乎无法成功。具体来说,我需要提取音频并将其保存为.aif或.aiff文件。
我尝试使用AVMutableComposition,并将mov文件作为AVAsset加载。然后只将音频轨道添加到AVMutableComposition中,最后使用AVAssetExportSession(将输出文件类型设置为我需要的AVFileTypeAIFF格式),将文件写入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

我不确定上述方法是否可行,但我认为我可能做错了些什么。如果有人知道达成我的目标的另一种方法,那么任何帮助都将不胜感激。

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

谢谢您的帮助!


有人有什么想法吗?我在这个问题上真的卡住了。 - Grinneh
我还想从视频文件中提取音频。你能帮我吗? - Harsh
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);
        }
    }];
}

它完美地运行...谢谢你啊伙计...@rajesh,我有一个问题,我必须创建反转音频,是否有任何解决方案可以获取任何音频文件的反转音频?请给出您的评论... - Mehul

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

0

给其他人一些小提示(我之前不知道): 如果你有视频文件(在我的情况下是mp4),你可以使用AVAudioPlayer仅播放音频。 你不需要从视频中提取(或转换)音频。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接