iOS从.mov文件中提取音频

Gri*_*neh 8 audio extract avfoundation mov ios

我一直试图从.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
Run Code Online (Sandbox Code Playgroud)

我不确定上述方法是否有效,但我可能会认为我只是做错了.但是,如果有人知道另一种方法来实现我想要实现的目标,那么任何帮助都会非常感激.

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

谢谢您的帮助!

Raj*_*rya 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);
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)


Jin*_*ing 6

因为outputFileType是错误的.对于.mov文件,它通常是@"com.apple.quicktime-movie".并且提取的音频是.mov格式.要确保,您可以使用此方法获取支持的输出类型:

NSArray *supportedTypeArray=exportSession.supportedFileTypes;    

for (NSString *str in supportedTypeArray)    
    NSLog(@"%@",str);
Run Code Online (Sandbox Code Playgroud)

您可以使用以下方法以音频格式(.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");
    }
}];
Run Code Online (Sandbox Code Playgroud)