0

Is it possible to use AVAssetWriter to write an MPEG2 ADTS or AAC ADTS on ios?

First I make an AVAssetWriter

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

Then I make an input using what I suspect to be the correct settings and attempt to add it to the writer, but to no avail:

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 Answer 1

1

here is my working settings for 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];
1
  • 1
    Please note that it's MPEG4, not MPEG2. Jul 28, 2015 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.