2011-11-08 41 views
2

我正在將我錄製的.m4a格式的音頻轉換爲.caf格式。錄製的音頻的設置中,如下所示:減小轉換後的音頻文件的大小

/* Record settings for recording the audio*/ 
recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey, 
       [NSNumber numberWithInt:44100.0],AVSampleRateKey, 
       [NSNumber numberWithInt: 2],AVNumberOfChannelsKey, 
       [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, 
       [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, 
       [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, 
       nil]; 

我將音頻轉換使用此功能的.caf:

-(NSString *)handleConvertToPCM:(NSURL *)convertUrl 
{ 
    [self performSelectorOnMainThread:@selector(showActivity) withObject:nil waitUntilDone:NO]; 

    DEBUG_LOG(@"DEBUGGING"); 
    DEBUG_LOG(@"handleConvertToPCM"); 
    // open an ExtAudioFile 
    NSLog (@"opening %@", convertUrl); 
    ExtAudioFileRef inputFile; 
    CheckResult (ExtAudioFileOpenURL((CFURLRef)convertUrl, &inputFile), 
       "ExtAudioFileOpenURL failed"); 
    // prepare to convert to a plain ol' PCM format 
    AudioStreamBasicDescription requiredPCMFormat; 
    requiredPCMFormat.mSampleRate = 44100; // todo: or use source rate? 
    requiredPCMFormat.mFormatID = kAudioFormatLinearPCM ; 
    requiredPCMFormat.mFormatFlags = kAudioFormatFlagsCanonical; 
    requiredPCMFormat.mChannelsPerFrame = 2; 
    requiredPCMFormat.mFramesPerPacket = 1; 
    requiredPCMFormat.mBitsPerChannel = 16; 
    requiredPCMFormat.mBytesPerPacket = 4; 
    requiredPCMFormat.mBytesPerFrame = 4; 
    CheckResult (ExtAudioFileSetProperty(inputFile, kExtAudioFileProperty_ClientDataFormat, 
             sizeof (requiredPCMFormat), &requiredPCMFormat), 
       "ExtAudioFileSetProperty failed"); 
    // allocate a big buffer. size can be arbitrary for ExtAudioFile. 
    UInt32 outputBufferSize = 0x10000; 
    void* ioBuf = malloc (outputBufferSize); 
    UInt32 sizePerPacket = requiredPCMFormat.mBytesPerPacket; 
    UInt32 packetsPerBuffer = outputBufferSize/sizePerPacket; 
    // set up output file 
    self.outputPath = [NSString stringWithFormat:@"%@/export-pcm.caf",DOCUMENTS_FOLDER]; 
    self.outputURL = [NSURL fileURLWithPath:self.outputPath]; 
    DEBUG_LOG(@"creating output file %@", self.outputURL); 
    AudioFileID outputFile; 
    CheckResult(AudioFileCreateWithURL((CFURLRef)outputURL, 
             kAudioFileCAFType, 
             &requiredPCMFormat, 
             kAudioFileFlags_EraseFile, 
             &outputFile), 
       "AudioFileCreateWithURL failed"); 
    // start convertin' 
    UInt32 outputFilePacketPosition = 0; //in bytes 
    while (true) 
    { 
     // wrap the destination buffer in an AudioBufferList 
     AudioBufferList convertedData; 
     convertedData.mNumberBuffers = 1; 
     convertedData.mBuffers[0].mNumberChannels = requiredPCMFormat.mChannelsPerFrame; 
     convertedData.mBuffers[0].mDataByteSize = outputBufferSize; 
     convertedData.mBuffers[0].mData = ioBuf; 
     UInt32 frameCount = packetsPerBuffer; 
     // read from the extaudiofile 
     CheckResult (ExtAudioFileRead(inputFile, 
             &frameCount, 
             &convertedData), 
        "Couldn't read from input file"); 
     if (frameCount == 0) 
     { 
      printf ("done reading from file"); 
      break; 
     } 
     // write the converted data to the output file 
     CheckResult (AudioFileWritePackets(outputFile, 
              false, 
              frameCount, 
              NULL, 
              outputFilePacketPosition/requiredPCMFormat.mBytesPerPacket, 
              &frameCount, 
              convertedData.mBuffers[0].mData), 
        "Couldn't write packets to file"); 

     DEBUG_LOG(@"Converted %ld bytes", outputFilePacketPosition); 
     // advance the output file write location 
     outputFilePacketPosition += (frameCount * requiredPCMFormat.mBytesPerPacket); 
    } 
    // clean up 
    ExtAudioFileDispose(inputFile); 
    AudioFileClose(outputFile); 
    return(self.outputPath);  

} 

我的問題是,相比轉換後的文件的大小是非常高的轉換爲文件。無論如何,通過更改轉換設置來減小大小。 我試圖壓縮獲得的文件,但它需要很多時間來壓縮。所以我想獲得一種方法來減少大小以及轉換。

+0

您正在將MP4文件解碼爲普通PCM。這肯定會導致規模的大幅增加。你爲什麼首先轉換文件? – Till

+0

它的要求是這樣的。任何方式來減小尺寸。將設置的任何改變工作?我嘗試了一些,但失敗了。 – Siddharth

+0

我並不熟悉在iOs中編寫音樂,但在使用工具轉換聲音時,我的遊戲將從立體聲轉換爲單聲道,因此尺寸減小了一半。也許mono對你來說已經足夠了? – TheEye

回答

0

解壓縮高度壓縮的音頻文件幾乎總是會導致更大的結果文件,除非您使用更均勻的壓縮格式進行重新壓縮。

+0

有沒有什麼方法可以重新壓縮比較不耗時的音頻? – Siddharth

+0

我需要caf格式 – Siddharth

+1

對於caf格式,文件大小將與採樣率(通常爲44.1kHz)乘以每採樣位數(通常爲16位或2字節)乘以通道數(1,2或5)成比例),以秒爲單位乘以音頻的長度。你想減少哪個? – hotpaw2