I am trying to extract audio track from .mp4
video file and convert to .m4a
audio file with this outputSettings
using AVAssetWriter
class
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, // 128 kbps
[NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,
nil];
Case 1: .mp4 video file with audio track params (print with CMFormatDescriptionRef
):
mediaType:'soun'
mediaSubType:'aac '
mSampleRate: 44100.000000
mFormatID: 'aac '
mChannelsPerFrame: 2
ACL: {Stereo (L R)}
result: successfully create .m4a
output file with defined output params
Case 2: .mp4 video file with audio track params(print with CMFormatDescriptionRef
):
mediaType:'soun'
mediaSubType:'aac '
mSampleRate: 48000.000000
mFormatID: 'aac '
mChannelsPerFrame: 6
ACL: {5.1 (C L R Ls Rs LFE)}
result: convert fails, when adding sample buffer [AVAssetWriter appendSampleBuffer: ...]
with unknown error
:
Error Domain: NSOSStatusErrorDomain
code: -12780
description: The operation could not be completed
To convert video to audio I use the same algorithm as described there: https://github.com/rs/SDAVAssetExportSession/blob/master/SDAVAssetExportSession.m
Also I tried to setup channelLayout.mChannelLayoutTag
with kAudioChannelLayoutTag_MPEG_5_1_D
, and update AVNumberOfChannelsKey
with 6
value, but it doesn't work for me.
Can anyone help me to understand what I am doing wrong? May be there are no solution to perform this task using only iOS AVFoundation framework? Should I use different outputParams
for audio track with 5.1 aac 6 channels?