我正在尝试从.mp4
视频文件中提取音轨,并使用.m4a
类
outputSettings
转换为AVAssetWriter
音频文件
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];
案例1: .mp4带有音轨参数的视频文件(使用CMFormatDescriptionRef
打印):
mediaType:'soun'
mediaSubType:'aac '
mSampleRate: 44100.000000
mFormatID: 'aac '
mChannelsPerFrame: 2
ACL: {Stereo (L R)}
结果:使用已定义的输出参数成功创建.m4a
输出文件
案例2: .mp4带有音轨参数的视频文件(使用CMFormatDescriptionRef
打印):
mediaType:'soun'
mediaSubType:'aac '
mSampleRate: 48000.000000
mFormatID: 'aac '
mChannelsPerFrame: 6
ACL: {5.1 (C L R Ls Rs LFE)}
结果:转换失败,在添加未知[AVAssetWriter appendSampleBuffer: ...]
的示例缓冲区error
时:
Error Domain: NSOSStatusErrorDomain
code: -12780
description: The operation could not be completed
要将视频转换为音频,我使用与其中描述的算法相同的算法:https://github.com/rs/SDAVAssetExportSession/blob/master/SDAVAssetExportSession.m
此外,我尝试使用channelLayout.mChannelLayoutTag
设置kAudioChannelLayoutTag_MPEG_5_1_D
,并使用AVNumberOfChannelsKey
值更新6
,但它对我无效。
任何人都可以帮我理解我做错了什么吗?可能没有解决方案只使用iOS AVFoundation框架来执行此任务?我应该使用不同的outputParams
音频轨道和5.1 aac 6频道吗?
答案 0 :(得分:0)
我还没试过5.1音频,但是当从视频中提取音频时,我喜欢在仅音频AVAssetExportSession
上使用直通AVMutableComposition
,因为这样可以避免转码速度慢并抛弃音质。类似的东西:
AVMutableComposition* newAudioAsset = [AVMutableComposition composition];
AVMutableCompositionTrack* dstCompositionTrack;
dstCompositionTrack = [newAudioAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
AVAsset* srcAsset = [AVURLAsset URLAssetWithURL:srcURL options:nil];
AVAssetTrack* srcTrack = [[srcAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
CMTimeRange timeRange = srcTrack.timeRange;//CMTimeRangeMake(kCMTimeZero, srcAsset.duration);
NSError* error;
if(NO == [dstCompositionTrack insertTimeRange:timeRange ofTrack:srcTrack atTime:kCMTimeZero error:&error]) {
NSLog(@"track insert failed: %@\n", error);
return 1;
}
__block AVAssetExportSession* exportSesh = [[AVAssetExportSession alloc] initWithAsset:newAudioAsset presetName:AVAssetExportPresetPassthrough];
exportSesh.outputFileType = AVFileTypeAppleM4A;
exportSesh.outputURL = dstURL;
[exportSesh exportAsynchronouslyWithCompletionHandler:^{
AVAssetExportSessionStatus status = exportSesh.status;
NSLog(@"exportAsynchronouslyWithCompletionHandler: %i\n", status);
if(AVAssetExportSessionStatusFailed == status) {
NSLog(@"FAILURE: %@\n", exportSesh.error);
} else if(AVAssetExportSessionStatusCompleted == status) {
NSLog(@"SUCCESS!\n");
}
}];
我手边没有5.1文件,所以如果不起作用,您可能需要仔细查看该行
AVAssetTrack* srcTrack = [[srcAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
P.S。这段代码来自2012"刚刚工作",这很不错。