开发者

Using AVAssetWriter to create MPEG-2 or AAC ADTS on iPhone

开发者 https://www.devze.com 2023-03-23 23:36 出处:网络
Is it possible to use AVAssetWriter to write an MPEG2 ADTS or AAC ADTS on ios? First I make an AVAssetWriter

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 numberWi开发者_如何学JAVAthInt: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.");
}


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消