7

我正在尝试从视频文件中提取音轨并使用此类.mp4转换为.m4a音频文件outputSettingsAVAssetWriter

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值,但它对我不起作用。AVNumberOfChannelsKey6

谁能帮我理解我做错了什么?可能没有解决方案可以仅使用 iOS AVFoundation 框架来执行此任务吗?我应该outputParams为 5.1 aac 6 通道的音轨使用不同的音轨吗?

4

1 回答 1

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

ps 2012年的这段代码“刚刚工作”,这很好。

于 2017-10-16T09:06:43.523 回答