.wav转换为AVAssetWritter ios的压缩格式

那么我现在面临的问题是尺寸问题。 我允许用户从他们的库中select一首歌曲,然后将其分割成多个部分,然后可以使用启用了文件共享的计算机上的.wav或.mp3文件。 基本上我使用以下AVAssetWritter选项,我不断得到一个巨大的巨大的audio文件。 EG 5分钟的音乐大概是50MB。 我正在试图把这个问题削减到一个标准大小的行业。 我有一种感觉,它与我正在使用LinearPCM作为我的选项之一有关,但是当我更改这个或任何audio选项时,作者不能写入。 我也正在更改AVAssetWritter上的FileType参数。 这是我的代码,它工作得很好,我只需要find压缩或缩小文件。

AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportURL fileType:AVFileTypeWAVE error:&assetError] NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, [NSNumber numberWithFloat:41000.0], AVSampleRateKey, [NSNumber numberWithInt:2], AVNumberOfChannelsKey, [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey, [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey, [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved, [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey, nil]; AVAssetWriterInput *assetWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:outputSettings] retain]; 

任何帮助压缩或使用我的AVAssetWritter写入不同的编解码器,而不是WAVE或CoreAudioFormat将不胜感激。

编辑:如果有一种方法,直接从音乐库到.m4a而不是.wav,这将是我想要的理想方法!

谢谢,尼克

回答

这是.m4a的正确audio选项,如果您希望用户将其视为.mp3或.wav文件,请将其附加到文件名的末尾!

 AudioChannelLayout channelLayout; memset(&channelLayout, 0, sizeof(AudioChannelLayout)); channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, [NSNumber numberWithInt:2], AVNumberOfChannelsKey, [NSNumber numberWithInt:128000], AVEncoderBitRateKey, [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey, nil]; 

回答

这是.m4a的正确audio选项,如果您希望用户将其视为.mp3或.wav文件,请将其附加到文件名的末尾!

 AudioChannelLayout channelLayout; memset(&channelLayout, 0, sizeof(AudioChannelLayout)); channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo; NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, [NSNumber numberWithInt:2], AVNumberOfChannelsKey, [NSNumber numberWithInt:128000], AVEncoderBitRateKey, [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey, nil]; 

怎么样(抱歉不要自己尝试,但它应该让你更接近):

 AVAssetWriter *assetWriter = [AVAssetWriter assetWriterWithURL:exportURL fileType:AVFileTypeAppleM4A error:&assetError]; NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithFloat:44100.0], AVSampleRateKey, [NSNumber numberWithInt:2], AVNumberOfChannelsKey, [NSNumber numberWithInt:128000], AVEncoderBitRateKey, [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey, [NSNumber numberWithInt: AVAudioQualityHigh], AVEncoderAudioQualityKey, nil];