【问题标题】:AVAssetWriter convert aac 5.1 audio track from AVAsset fail on appendSampleBufferAVAssetWriter 从 AVAsset 转换 aac 5.1 音轨在 appendSampleBuffer 上失败
【发布时间】:2017-10-10 14:26:34
【问题描述】:

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

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

我还尝试使用kAudioChannelLayoutTag_MPEG_5_1_D 设置channelLayout.mChannelLayoutTag,并使用6 值更新AVNumberOfChannelsKey,但这对我不起作用。

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

【问题讨论】:

标签: ios objective-c avfoundation avassetwriter avassetwriterinput


【解决方案1】:

我还没有尝试过使用 5.1 音频,但是当从视频中提取音频时,我喜欢在纯音频 AVMutableComposition 上使用直通 AVAssetExportSession,因为这样可以避免转码速度变慢并降低音频质量。比如:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多