gpt4 book ai didi

ios - 如何修剪音频数据?

转载 作者:行者123 更新时间:2023-12-01 19:25:45 24 4
gpt4 key购买 nike

我想删除最后5秒钟的音频数据并将其保存为新音频。我试图通过使用ExtAudioFile服务的以下代码来完成它,但是在这里,我的音频输出大小从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);

最佳答案

您正在获取压缩音频(M4A)并将其解压缩,这是修剪音频内容所需的操作。如果您想回到2.5 MB的范围,则需要在完成后重新压缩音频。

请记住,反复的有损解压缩,编辑,再压缩循环会降低音频质量。如果要执行许多音频编辑操作,则应将音频从压缩状态转换为未压缩状态,然后运行转换,最后最后重新压缩。

关于ios - 如何修剪音频数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7849801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com