无法正确反转 AVAsset 音频。唯一的结果是白噪声

Posted

技术标签:

【中文标题】无法正确反转 AVAsset 音频。唯一的结果是白噪声【英文标题】:Can't reverse AVAsset audio properly. The only result is white noise 【发布时间】:2016-02-23 15:28:55 【问题描述】:

我正在尝试反转 AVAsset 音频并将其保存到文件中。为了清楚起见,我用问题https://github.com/ksenia-lyagusha/AudioReverse.git做了简单的应用程序

应用程序从包中获取mp4 视频文件,将其作为单个m4a 文件导出到沙箱中的临时文件夹,然后尝试从那里读取它,反转并保存结果文件。 临时的m4a 文件没问题。

我反向部分的唯一结果是Sandbox 中带有白噪声的音频文件。

下面有一段代码,负责反转AVAsset。它基于相关问题

How to reverse an audio file? ios audio manipulation - play local .caf file backwards

但是,它对我不起作用。

OSStatus theErr = noErr;
UInt64 fileDataSize = 0;
AudioFileID inputAudioFile;
AudioStreamBasicDescription theFileFormat;
UInt32 thePropertySize = sizeof(theFileFormat);

theErr = AudioFileOpenURL((__bridge CFURLRef)[NSURL URLWithString:inputPath], kAudioFileReadPermission, 0, &inputAudioFile);

thePropertySize = sizeof(fileDataSize);
theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize);

UInt32 ps = sizeof(AudioStreamBasicDescription) ;
AudioFileGetProperty(inputAudioFile, kAudioFilePropertyDataFormat, &ps, &theFileFormat);

UInt64 dataSize = fileDataSize;
void *theData = malloc(dataSize);

// set up output file
AudioFileID outputAudioFile;

AudioStreamBasicDescription myPCMFormat;

myPCMFormat.mSampleRate       = 44100;
myPCMFormat.mFormatID         = kAudioFormatLinearPCM;
// kAudioFormatFlagsCanonical is deprecated
myPCMFormat.mFormatFlags      = kAudioFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved;
myPCMFormat.mChannelsPerFrame = 1;
myPCMFormat.mFramesPerPacket  = 1;
myPCMFormat.mBitsPerChannel   = 32;
myPCMFormat.mBytesPerPacket   = (myPCMFormat.mBitsPerChannel / 8) * myPCMFormat.mChannelsPerFrame;
myPCMFormat.mBytesPerFrame    = myPCMFormat.mBytesPerPacket;



NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"ReverseAudio.caf"];
NSURL *outputURL = [NSURL fileURLWithPath:exportPath];

theErr = AudioFileCreateWithURL((__bridge CFURLRef)outputURL,
                       kAudioFileCAFType,
                       &myPCMFormat,
                       kAudioFileFlags_EraseFile,
                       &outputAudioFile);

//Read data into buffer
//if readPoint  = dataSize, then bytesToRead = 0 in while loop and
//it is endless
SInt64 readPoint  = dataSize-1;
UInt64 writePoint = 0;

while(readPoint > 0)

    UInt32 bytesToRead = 2;
    AudioFileReadBytes(inputAudioFile, false, readPoint, &bytesToRead, theData);
    // bytesToRead is now the amount of data actually read

    UInt32 bytesToWrite = bytesToRead;
    AudioFileWriteBytes(outputAudioFile, false, writePoint, &bytesToWrite, theData);
    // bytesToWrite is now the amount of data actually written

    writePoint += bytesToWrite;
    readPoint -= bytesToRead;


free(theData);
AudioFileClose(inputAudioFile);
AudioFileClose(outputAudioFile);

如果我将AudioFileCreateWithURL 中的文件类型从kAudioFileCAFType 更改为另一个,则根本不会在Sandbox 中创建结果文件。

感谢您的帮助。

【问题讨论】:

奇怪的噪音是什么样的?白噪声? @yun.cloud 问题已编辑,现在可以更清楚了。 YES,最终输出文件中有白噪声。 只是想确认一下,但采样/比特率与原始文件是否相同?从我看来它看起来是正确的 - 但是当你只写文件而不反转它时会发生什么(本质上是复制数据) 【参考方案1】:

您会收到白噪音,因为您的输入和输出文件格式不兼容。您有不同的采样率和通道,可能还有其他差异。要完成这项工作,您需要在读取和写入之间使用一种通用 (PCM) 格式。对于新的(ish)AVAudio 框架来说,这是一项合理的工作。我们从文件读取到 PCM,洗牌缓冲区,然后从 PCM 写入文件。这种方法并未针对大文件进行优化,因为所有数据都一次性读入缓冲区,但足以让您开始使用。

您可以从您的 getAudioFromVideo 完成块中调用此方法。为了清楚起见,忽略了错误处理。

- (void)readAudioFromURL:(NSURL*)inURL reverseToURL:(NSURL*)outURL 

//prepare the in and outfiles

  AVAudioFile* inFile = 
     [[AVAudioFile alloc] initForReading:inURL error:nil];

  AVAudioFormat* format = inFile.processingFormat;
  AVAudioFrameCount frameCount =(UInt32)inFile.length;
  NSDictionary* outSettings = @
             AVNumberOfChannelsKey:@(format.channelCount)
            ,AVSampleRateKey:@(format.sampleRate);

  AVAudioFile* outFile = 
    [[AVAudioFile alloc] initForWriting:outURL
                               settings:outSettings
                                  error:nil];

//prepare the forward and reverse buffers
  self.forwaredBuffer = 
    [[AVAudioPCMBuffer alloc] initWithPCMFormat:format 
                                  frameCapacity:frameCount];
  self.reverseBuffer = 
    [[AVAudioPCMBuffer alloc] initWithPCMFormat:format 
                                  frameCapacity:frameCount];

//read file into forwardBuffer
    [inFile readIntoBuffer:self.forwaredBuffer error:&error];

//set frameLength of reverseBuffer to forwardBuffer framelength
    AVAudioFrameCount frameLength = self.forwaredBuffer.frameLength; 
    self.reverseBuffer.frameLength = frameLength;

//iterate over channels 

     //stride is 1 or 2 depending on interleave format
     NSInteger stride = self.forwaredBuffer.stride;  

    for (AVAudioChannelCount channelIdx = 0;                    
          channelIdx < self.forwaredBuffer.format.channelCount;
          channelIdx++) 
      float* forwaredChannelData = 
          self.forwaredBuffer.floatChannelData[channelIdx];
      float* reverseChannelData = 
          self.reverseBuffer.floatChannelData[channelIdx];
      int32_t reverseIdx = 0;

     //iterate over samples, allocate to reverseBuffer in reverse order   
      for (AVAudioFrameCount frameIdx = frameLength; 
                    frameIdx >0; 
                    frameIdx--) 
           float sample = forwaredChannelData[frameIdx*stride];
           reverseChannelData[reverseIdx*stride] = sample;
           reverseIdx++;
            
        

//write reverseBuffer to outFile
        [outFile writeFromBuffer:self.reverseBuffer error:nil];
    

【讨论】:

谢谢,太棒了。我花了一大笔钱做这件事。顺便问一下,您能推荐任何类似的倒车视频方法吗? @Kseny,看看github上的this project【参考方案2】:

我无法在您的代码中找到问题,但是我建议您使用 AVAssetWriter 反转 AVAsset。以下代码基于iOS reverse audio through AVAssetWritet。我在那里添加了其他方法以使其正常工作。最后我得到了反向文件。

static NSMutableArray *samples;

static OSStatus sampler(CMSampleBufferRef sampleBuffer, CMItemCount index, void *refcon)

    [samples addObject:(__bridge id _Nonnull)(sampleBuffer)];
    return noErr;


- (void)reversePlayAudio:(NSURL *)inputURL

    AVAsset *asset = [AVAsset assetWithURL:inputURL];

    AVAssetReader* reader = [[AVAssetReader alloc] initWithAsset:asset error:nil];

    AVAssetTrack* audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    NSMutableDictionary* audioReadSettings = [NSMutableDictionary dictionary];
    [audioReadSettings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM]
                         forKey:AVFormatIDKey];

    AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:audioReadSettings];
    [reader addOutput:readerOutput];
    [reader startReading];

    NSDictionary *outputSettings = @AVFormatIDKey     : @(kAudioFormatMPEG4AAC),
                                 AVSampleRateKey       : @(44100.0),
                                 AVNumberOfChannelsKey : @(1),
                                 AVEncoderBitRateKey   : @(128000),
                                 AVChannelLayoutKey    : [NSData data];

    AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio
                                                                     outputSettings:outputSettings];

    NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"reverseAudio.m4a"];

    NSURL *exportURL = [NSURL fileURLWithPath:exportPath];
    NSError *writerError = nil;
    AVAssetWriter *writer = [[AVAssetWriter alloc] initWithURL:exportURL
                                                      fileType:AVFileTypeAppleM4A
                                                         error:&writerError];
    [writerInput setExpectsMediaDataInRealTime:NO];
    writer.shouldOptimizeForNetworkUse = NO;
    [writer addInput:writerInput];
    [writer startWriting];
    [writer startSessionAtSourceTime:kCMTimeZero];

    CMSampleBufferRef sample;// = [readerOutput copyNextSampleBuffer];
    samples = [[NSMutableArray alloc] init];

    while (sample != NULL) 
        sample = [readerOutput copyNextSampleBuffer];

        if (sample == NULL)
            continue;

        CMSampleBufferCallForEachSample(sample, &sampler, NULL);

        CFRelease(sample);
    

    NSArray* reversedSamples = [[samples reverseObjectEnumerator] allObjects];

    for (id reversedSample in reversedSamples) 
        if (writerInput.readyForMoreMediaData)  
            [writerInput appendSampleBuffer:(__bridge CMSampleBufferRef)(reversedSample)];
        
        else 
            [NSThread sleepForTimeInterval:0.05];
        
    

    [samples removeAllObjects];
    [writerInput markAsFinished];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_async(queue, ^

        [writer finishWritingWithCompletionHandler:^

            //  writing is finished
            // reversed audio file in TemporaryDirectory in the Sandbox
        ];
    );

代码的已知问题。

    如果音频过长,内存可能会出现问题。 音频文件的持续时间比原始文件长。 (作为一种快速修复,您可以像往常一样将其削减为 AVAsset)。

【讨论】:

它总是增加输出时间。最后没有音频。 @AsifHabib 最终在已知问题中指出该方法的缺点

以上是关于无法正确反转 AVAsset 音频。唯一的结果是白噪声的主要内容,如果未能解决你的问题,请参考以下文章

更改 AVAsset 的首选数量

将AVAsset视频文件拆分为块

MPMediaItem 到 AVAsset 到 .MP3

从 AVAsset 中查找通道数

VB.Net - 音频文件专辑艺术

是否可以使用 HTML5 音频 API 反转音频?