我怎样才能附加到录制MPEG4 AAC文件? [英] How can I append to a recorded MPEG4 AAC file?

查看:174
本文介绍了我怎样才能附加到录制MPEG4 AAC文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记录在iPhone上的音频,使用 AVAudioRecorder 通过以下设置:

I'm recording audio on an iPhone, using an AVAudioRecorder with the following settings:

NSMutableDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
       [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
       [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
       [NSNumber numberWithInt:1], AVNumberOfChannelsKey,
       [NSNumber numberWithInt:12800], AVEncoderBitRateKey,
       [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
       [NSNumber numberWithInt: AVAudioQualityHigh],  AVEncoderAudioQualityKey,
       nil];

(我可以在大多数的这些设置灵活,但是我必须使用MPEG4 AAC)。

(I can be flexible on most of these settings, but I have to use MPEG4 AAC.)

我的音频保存到文件中。

I save the audio to a file.

的用户需要能够回来在稍后的日期和持续记录到相同的文件中。似乎没有要直接与 AVAudioRecorder 做一个选择,所以不是我录制到一个新的文件和连接它们。

The user needs to be able to come back at a later date and continue recording to the same file. There doesn't seem to be an option to do this directly with AVAudioRecorder, so instead I'm recording to a new file and concatenating them.

目前我使用的是 AVMutableComposition AVMutableCompositionTrack here ,但它是更长的录音很慢,所以这是不是真的可行。

At the moment I'm appending the files using an AVMutableComposition and an AVMutableCompositionTrack as here, but it's really slow for longer recordings so this isn't really feasible.

我想这将是更快,如果我可以从第二个文件剥去头,音频数据追加到第一个文件,然后修改合并文件的标题,以反映新的持续时间。据我所知这两个文件,​​用完全相同的设置创建的,我想在标题中的其他细节应该是相同的。

I'm thinking it would be much quicker if I could strip the header from the second file, append the audio data to the first file, then alter the header of the combined file to reflect the new duration. As I know both files were created with exactly the same settings, I figure the other details in the headers should be identical.

可惜我不能找到什么格式的标头,或者是否有可能以这种方式合并文件的任何信息。

Unfortunately I can't find any information about what format the headers are in, or if it's possible to combine files in this way.

所以我的问题是:


  • 什么是MPEG-4 AAC文件头的格式,在iPhone上?
  • 创建时
  • 我可以用这样的标题搞乱组合两个音频文件?

  • 是否有追加两个MPEG-4 AAC音频更好的办法的文件几乎在瞬间?

推荐答案

虽然我们要求 AVAudioRecorder 在MPEG4-AAC格式录制,它总是产生一个.caf(核心音频格式)文件。这恰恰是一个包装格式,然而,它包含的实际音频数据是在AAC格式

Though we ask the AVAudioRecorder to record in MPEG4-AAC format, it always produces a .caf (Core Audio Format) file. This is just a wrapper format, however, and the actual audio data it contains is in AAC format.

在结束时,追加文件下来到操纵.caf文件逐字节。的Core Audio格式文件的规范是<一个href=\"http://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/CAFSpec/CAF_spec/CAF_spec.html\"相对=nofollow>此处。消化这个文件,并相应地处理这些文件是有点倒胃口在第一,但事实证明,该规范是非常明确的,完整的,所以这是不是太苛刻。

In the end, appending files came down to manipulating the .caf files byte-by-byte. The spec for Core Audio Format files is here. Digesting this document and processing the files accordingly was a little off-putting at first, but it turns out the spec is very clear and complete, so it wasn't too onerous.

由于规格说明,.caf文件包括与年初四字节名称块。对于AAC文件,总有一个说明块和久喜块。因为我们知道我们的两个原始文件相同的格式,我们可以把这些大块不变复制到输出文件。

As the spec explains, .caf files consist of chunks with four-byte names at the beginning. For AAC files, there's always a desc chunk and a kuki chunk. As we know our two original files are in the same format, we can copy these chunks unchanged to the output file.

还有一个的pAKT 块和数据块。我们不能保证这些都将输入文件内是顺序。有可能会或可能不会是一个免费块 - 但是这仅仅包含填充为0x00的,所以我们不必将此复制到输出文件

There's also a pakt chunk and a data chunk. We can't guarantee which order these will be in within the input files. There may or may not be a free chunk - but this just contains padding 0x00's, so we needn't copy this to the output file.

要结合的pAKT 块,我们需要考察块头部,并产生一个新的的pAKT 块的 mNumberPackets mNumberValidFrames 字段是那些在输入文件的总和。在 mPrimingFrames mRemainderFrames 始终为零 - 这是仅适用于流媒体相关的。在的pAKT 块的大部分(即实际数据包数据表)可以只是连接在一起。

To combine the pakt chunks, we need to examine the chunk headers and produce a new pakt chunk whose mNumberPackets and mNumberValidFrames fields are the sums of those in the input files. The mPrimingFrames and mRemainderFrames are always zero - these are only relevant for streaming media. The bulk of the pakt chunks (ie. the actual packet table data) can just be concatenated.

同样,对于数据块:在 mChunkSize 字段需要总结,然后将数据的大容量可并置。

Similarly for the data chunks: the mChunkSize fields need to be summed and then the bulk of the data can be concatenated.

从这些文件中的所有二进制数字字段读取数据时要小心:这些文件都是大端但iPhone是小端

Be careful when reading data from all the binary numeric fields within these files: the files are big-endian but the iPhone is little-endian.

有关额外的信用,你也不妨考虑删除音频片段从一个文件中,或插入一个音频文件到另一个中间。这是有点麻烦,因为你必须解析的pAKT 块的内容。同样是按照规范的情况下:还有的数据包大小如何存储在可变长度整数一个很好的说明,所以你必须分析这些发现每个数据包中的数据块,并据此计算出它们的位置。

For extra credit, you might also like to consider deleting segments of audio from within a file, or inserting one audio file into the middle of another. This is a little trickier as you have to parse the contents of the pakt chunk. Again it's a case of following the spec: there's a good description of how the packet sizes are stored in variable-length integers, so you'll have to parse these to find how many bytes each packet takes up in the data chunk, and calculate their positions accordingly.

这所有的一切是相当更多的麻烦,比我所期待的。也许有一个开源库,将做到这一切给你,但我无法找到一个。

All in all this is rather more hassle than I was hoping for. Maybe there's an open source library that will do all this for you, but I couldn't find one.

然而,处理的原始文件这样被致盲的快速相比,使用 AVMutableComposition AVMutableCompositionTrack 作为原来的问题 - 插入一个小时的长时间录音到另一个相同长度的花费大约两秒钟

However, handling raw files like this is blinding fast compared to using AVMutableComposition and AVMutableCompositionTrack as in the original question - inserting an hour-long recording into another of the same length takes about two seconds.

祝你好运!

这篇关于我怎样才能附加到录制MPEG4 AAC文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆