如何修剪audio数据?

我想删除最后5秒的audio数据,并将其保存到不同的位置作为新的audio。我试图通过下面的代码使用ExtAudioFile服务来完成它,但是这里我的audio输出大小从2.5 MB增加到26.5 MB ..我错了吗。

UInt32 size; NSString *docsDir; NSArray *dirPaths; dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; NSString *destinationURL = [docsDir stringByAppendingPathComponent:@"Audio3.m4a"]; NSURL * soundFilePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Audio1" ofType:@"m4a"]]; ExtAudioFileRef inputFile= NULL; ExtAudioFileRef outputFile= NULL; ExtAudioFileOpenURL((CFURLRef)soundFilePath, &inputFile); AudioStreamBasicDescription destFormat; destFormat.mFormatID = kAudioFormatMPEG4AAC; destFormat.mFormatFlags = kAudioFormatFlagsCanonical; destFormat.mSampleRate = 441000; destFormat.mBytesPerPacket = 2; destFormat.mFramesPerPacket = 1; destFormat.mBytesPerFrame = 2; destFormat.mChannelsPerFrame = 2; destFormat.mBitsPerChannel = 16; destFormat.mReserved = 0; OSStatus createStatus =ExtAudioFileCreateWithURL((CFURLRef)[NSURL fileURLWithPath:destinationURL],kAudioFileM4AType,&destFormat,NULL,kAudioFileFlags_EraseFile,&outputFile); //ExtAudioFileDispose(outputFile); NSLog(@"createStatus: %i", createStatus); //this is not needed as file url is already opened. ExtAudioFileOpenURL((CFURLRef)soundFilePath, &inputFile); //ExtAudioFileOpenURL((CFURLRef)[NSURL fileURLWithPath:destinationURL], &outputFile); //find out how many frames long this file is SInt64 length = 0; UInt32 dataSize2 = (UInt32)sizeof(length); ExtAudioFileGetProperty(inputFile, kExtAudioFileProperty_FileLengthFrames, &dataSize2, &length); AudioStreamBasicDescription clientFormat; clientFormat.mFormatID = kAudioFormatMPEG4AAC; clientFormat.mSampleRate = 441000; clientFormat.mFormatFlags = kAudioFormatFlagsCanonical; clientFormat.mBitsPerChannel = 16; clientFormat.mChannelsPerFrame = 2; clientFormat.mFramesPerPacket = 1; clientFormat.mBytesPerPacket = 2; clientFormat.mBytesPerFrame = 2; destFormat.mReserved = 0; size = sizeof(clientFormat); //set the intermediate format to canonical on the source file for conversion (?) ExtAudioFileSetProperty(inputFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat); ExtAudioFileSetProperty(outputFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat); OSStatus seekStatus = ExtAudioFileSeek(outputFile, 0);  NSLog(@"seekstatus %i", seekStatus); SInt64 newLength = length - (5); //shorten by 5 seconds worth of frames NSLog(@"length: %i frames", length); UInt8 *buffer = malloc(64*1024); //64K UInt32 totalFramecount = 0; while(true) { AudioBufferList bufferList; bufferList.mNumberBuffers = 1; bufferList.mBuffers[0].mNumberChannels = 2; bufferList.mBuffers[0].mData = buffer; //pointer to buffer of audio data bufferList.mBuffers[0].mDataByteSize =64*1024; //number of bytes in the buffer UInt32 frameCount = 64*1024 / 2; //2 bytes per frame // Read a chunk of input SInt64 outFrameOffset; ExtAudioFileTell(inputFile, &outFrameOffset) ; NSLog(@"head status %i", outFrameOffset); OSStatus status = ExtAudioFileRead(inputFile, &frameCount, &bufferList); totalFramecount += frameCount; NSLog(@"read status %i", status); NSLog(@"loaded %i frames and stopping at %i", totalFramecount, newLength); if (!frameCount ||(totalFramecount >= newLength)) { //termination condition break; } OSStatus writeStatus = ExtAudioFileWrite(outputFile, frameCount, &bufferList); NSLog(@"ws: %i", writeStatus); } free(buffer); ExtAudioFileDispose(inputFile); ExtAudioFileDispose(outputFile); 

您正在压缩audio(M4A)并将其解压缩,这是您需要做的以削减audio内容。 如果你想回到2.5 MB的范围,你需要重新压缩你的audio完成后。

请记住,重复的有损解压缩 – 编辑 – 重新压缩循环会降低audio的质量。 如果您要执行大量的audio编辑操作,则应将audio从压缩转换为未压缩,然后运行转换,最后重新压缩。