使用AVAssetWriter在iPhone上创建MPEG-2或AAC ADTS

时间:2011-07-31 23:16:28

标签: iphone ios core-audio aac mpeg2-ts

是否可以使用AVAssetWriter在ios上编写MPEG2 ADTS或AAC ADTS?

首先我制作一个AVAssetWriter

assetWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:filePath]
                                                                fileType:AVFileTypeCoreAudioFormat
                                                                   error:&er];

然后我使用我怀疑是正确的设置进行输入并尝试将其添加到编写器中,但无济于事:

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

assetWriterInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio
                                                  outputSettings:outputSettings];
assetWriterInput.expectsMediaDataInRealTime = NO;

if ([assetWriter canAddInput:assetWriterInput]) {
    [assetWriter addInput:assetWriterInput];
} else {
    NSLog (@"can't add asset writer input.");
}

1 个答案:

答案 0 :(得分:1)

这是我对aac的工作设置

AudioChannelLayout channelLayout;
    memset(&channelLayout, 0, sizeof(AudioChannelLayout));
    channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;

    AVAssetWriter *assetWriter = [[AVAssetWriter assetWriterWithURL:exportURL
                                                           fileType:AVFileTypeMPEG4

                                                              error:&assetError] retain];

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