iOS的提取音频从.mov文件 [英] iOS Extracting Audio from .mov file

查看:241
本文介绍了iOS的提取音频从.mov文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力,现在提取一会儿一个.mov文件的音频和我刚才似乎无法得到它的工作。具体来说,我需要提取音频,并保存为一个.AIF或.AIFF文件。

我使用的是AVMutableComposition,并加载MOV文件作为AVAsset尝试。最后用AVAssetExportSession(设置输出文件类型AVFileTypeAIFF,这是我需要的格式),将文件写入到一个AIF前只增加音轨到AVMutableComposition。

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

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

  AVAssetExportSession *出口国;
出口= [[AVAssetExportSession页头] initWithAsset:组合式presetName:AVAssetExport presetHighestQuality]。exporter.audioMix = audioMix;
exporter.outputURL = [NSURL fileURLWithPath:文件路径]
exporter.outputFileType = AVFileTypeAIFF; //发生在这一行错误

我不知道,如果上面的方法是有效的,但即时通讯持开放的态度,我只是在做一些错误的可能性。但是如果有谁知道另一种方式来完成我想要实现的,比任何帮助将不胜AP preciated。

我可以发布更详细的code。如果需要的话,但此刻我想一些其他的办法,以便它有点乱,现在。

感谢您的帮助!


解决方案

   - (无效){getAudioFromVideo
    浮STARTTIME = 0;
    浮ENDTIME = 10;
    [超级viewDidLoad中];
    NSArray的*路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    的NSString * documentsDirectory = [路径objectAtIndex:0];
    * NSString的audioPath = [documentsDirectory stringByAppendingPathComponent:@soundOneNew.m4a];    AVAsset * myasset = [AVAsset assetWithURL:[一个NSBundle mainBundle] URLForResource:@VideoNamewithExtension:@MP4]];    AVAssetExportSession * exportSession = [AVAssetExportSession exportSessionWithAsset:myasset presetName:AVAssetExport presetAppleM4A]。    exportSession.outputURL = [NSURL fileURLWithPath:audioPath];
    exportSession.outputFileType = AVFileTypeAppleM4A;    CMTime vocalStartMarker = CMTimeMake((int)的(地板(的startTime * 100)),100);
    CMTime vocalEndMarker = CMTimeMake((int)的(CEIL(结束时间* 100)),100);    CMTimeRange exportTimeRange = CMTimeRangeFromTimeToTime(vocalStartMarker,vocalEndMarker);
    exportSession.timeRange = exportTimeRange;
    如果([[的NSFileManager defaultManager] fileExistsAtPath:audioPath]){
        [的NSFileManager defaultManager] removeItemAtPath:audioPath错误:无];
    }    [exportSession exportAsynchronouslyWithCompletionHandler:^ {
        如果(exportSession.status == AVAssetExportSessionStatusFailed){
            的NSLog(@失败);
        }
        其他{
            的NSLog(@AudioLocation:%@,audioPath);
        }
    }];
}

I've been trying to extract audio from a .mov file for a while now and I just can't seem to get it working. Specifically, I need to extract the audio and save it as an .aif or .aiff file .

I've tried using an AVMutableComposition, and loading the mov file as a AVAsset. Adding only the audio track to the AVMutableComposition before finally using an AVAssetExportSession (setting the output file type to AVFileTypeAIFF, which is the format I need it in), to write the file to an aif.

I get an error saying that this output file type is invalid, I'm unsure why:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid output file type'

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

I'm not sure if the above approach would work, but im open to the possibility that I'm just doing something wrong. However if anyone knows another way to accomplish what I'm trying to achieve, than any help would be greatly appreciated.

I can post more detailed code if it is needed, but at the moment I'm trying a few other approaches so its a bit messy right now.

Thanks for the help!

解决方案

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

这篇关于iOS的提取音频从.mov文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆