我想删除最后5秒的音频数据,并将其保存到不同的位置作为新的音频。我试图通过使用ExtAudioFile服务的以下代码来完成此操作,但这里我的音频输出大小从2.5MB增加到26.5MB MB..where我错了。
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);发布于 2011-10-21 21:10:53
您正在压缩音频(M4A)并解压缩它,这是您需要对音频内容进行裁剪的操作。如果你想回到2.5MB的范围,你需要在完成后重新压缩你的音频。
请记住,重复有损的解压缩-编辑-再压缩循环会降低音频的质量。如果您要执行大量音频编辑操作,则应将音频从压缩转换为未压缩,然后运行转换,最后重新压缩。
https://stackoverflow.com/questions/7849801
复制相似问题