AVAssetWriter如何编写降采样/压缩的m4a/mp3文件

10

我试图压缩/降低本地m4a或mp3文件的质量以使其更小。

最初,我使用AVAssetExportSession将AVAsset导出到临时目录,但我无法控制压缩/降低质量(您只能使用预设,其中只有.wav文件格式支持质量下降)。

然后,在Stack Overflow上遵循了几个示例,我尝试使用AVAssetReader / AVAssetWriter来执行此“导出”操作。

我创建我的读取器/编写器如下:

NSString *exportPath = [NSHomeDirectory() stringByAppendingPathComponent:@"out.m4a"];

NSURL *exportURL = [NSURL fileURLWithPath:outPath];

// reader
NSError *readerError = nil;
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset
                                                       error:&readerError];

AVAssetTrack *track = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];    
AVAssetReaderTrackOutput *readerOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:track
                                                                          outputSettings:nil];
[reader addOutput:readerOutput];

// writer
NSError *writerError = nil;
AVAssetWriter *writer = [[AVAssetWriter alloc] initWithURL:exportURL
                                                  fileType:AVFileTypeAppleM4A
                                                     error:&writerError];

AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;

// use different values to affect the downsampling/compression
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
                                [NSNumber numberWithInt:2], AVNumberOfChannelsKey,
                                [NSNumber numberWithInt:128000], AVEncoderBitRateKey,
                                [NSData dataWithBytes:&channelLayout length:sizeof(AudioChannelLayout)], AVChannelLayoutKey,
                                nil];

AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeAudio
                                                                 outputSettings:outputSettings];
[writerInput setExpectsMediaDataInRealTime:NO];
[writer addInput:writerInput];

然后我开始写作...

[writer startWriting];
[writer startSessionAtSourceTime:kCMTimeZero];

[reader startReading];
dispatch_queue_t mediaInputQueue = dispatch_queue_create("mediaInputQueue", NULL);
[writerInput requestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^{

    NSLog(@"Asset Writer ready : %d", writerInput.readyForMoreMediaData);
    while (writerInput.readyForMoreMediaData) {
        CMSampleBufferRef nextBuffer;
        if ([reader status] == AVAssetReaderStatusReading && (nextBuffer = [readerOutput copyNextSampleBuffer])) {
            if (nextBuffer) {
                NSLog(@"Adding buffer");
                [writerInput appendSampleBuffer:nextBuffer];
            }
        } else {
            [writerInput markAsFinished];

            switch ([reader status]) {
                case AVAssetReaderStatusReading:
                    break;
                case AVAssetReaderStatusFailed:
                    [writer cancelWriting];
                    break;
                case AVAssetReaderStatusCompleted:
                    NSLog(@"Writer completed");
                    [writer endSessionAtSourceTime:asset.duration];
                    [writer finishWriting];

                    NSData *data = [NSData dataWithContentsOfFile:exportPath];
                    NSLog(@"Data: %@", data);
                    break;
            }
            break;
        }
    }
}];

我写完后,据说已经写入exportURL的数据是null,并且写入器报告成功完成。有什么想法可能出了什么问题吗?

更新 调用appendSampleBuffer:后,写入器状态为AVAssetWriterStatusFailed,尽管读取器状态成功,并且似乎可以读取整个文件。


它是否曾尝试写入任何内容?第一次尝试附加样本缓冲区时,读取器状态是什么? - BlueVoodoo
啊哈,每次调用[writerInput appendSampleBuffer:]时,写入器状态都为3,根据枚举值看起来像是AVAssetWriterStatusFailed。看起来读取器正在复制我的资产中的所有样本缓冲区 - appendSampleBuffer被调用了很多次。 - Dfowj
可能需要添加以下代码以避免内存泄漏:CMSampleBufferInvalidate(nextBuffer); CFRelease(nextBuffer); nextBuffer = NULL; - guozqzzu
1个回答

6
我发现解决办法:
NSString *exportPath = [NSHomeDirectory() stringByAppendingPathComponent:@"out.m4a"]中使用NSHomeDirectory()会导致写入程序无法创建文件。不太清楚为什么,或者我需要做什么才能让它正常工作,但是暂时将NSHomeDirectiory()更改为NSTemporaryDirectory()解决了我的问题。

1
你不能在iOS中写入主目录。临时目录可以,文档目录也可以。 - Oded Ben Dov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接