iOS分离立体声mp3到单声道aac

时间:2015-11-13 00:18:48

标签: ios split mp3 m4a

我使用以下代码在iOS上将mp3转换为m4a:iOS swift convert mp3 to aac

但是我需要将左右声道提取到单独的m4a文件中。

我有这个代码工作,它将我的音频分成nsdata:

let leftdata:NSMutableData! = NSMutableData()
let rightdata:NSMutableData! = NSMutableData()

let buff: CMBlockBufferRef = CMSampleBufferGetDataBuffer(sampleBuffer!)!

var lengthAtOffset: size_t = 0
var totalLength:Int = 0
var data: UnsafeMutablePointer<Int8> = nil

if( CMBlockBufferGetDataPointer( buff, 0, &lengthAtOffset, &totalLength, &data ) != noErr ) {
    print("some sort of error happened")
} else {

    for i in 0.stride(to: totalLength, by: 2) {

        if(i % 4 == 0) {
            leftdata.appendBytes(data+i, length: 2)
        } else {
            rightdata.appendBytes(data+i, length: 2)
        }

    }
}

data = nil

但是现在我需要将它转换为CMSampleBuffer,以便我可以附加到资产编写者。如何将nsdata转换为样本缓冲区?

11月24日更新 我现在得到了以下代码,试图将NSData转换为CMSampleBuffer。我无法解决失败的问题:

var dataPointer: UnsafeMutablePointer<Void> = UnsafeMutablePointer(leftdata.bytes)

var cmblockbufferref:CMBlockBufferRef?

var status = CMBlockBufferCreateWithMemoryBlock(nil, dataPointer, leftdata.length, kCFAllocatorNull, nil, 0, leftdata.length, 0, &cmblockbufferref)

var audioFormat:AudioStreamBasicDescription = AudioStreamBasicDescription()
audioFormat.mSampleRate = 44100
audioFormat.mFormatID = kAudioFormatLinearPCM
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian
audioFormat.mBytesPerPacket = 2
audioFormat.mFramesPerPacket = 1
audioFormat.mBytesPerFrame = 2
audioFormat.mChannelsPerFrame = 1
audioFormat.mBitsPerChannel = 16
audioFormat.mReserved = 0

var format:CMFormatDescriptionRef?

status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &audioFormat, 0, nil, 0, nil, nil, &format);

var timing:CMSampleTimingInfo = CMSampleTimingInfo(duration: CMTimeMake(1, 44100), presentationTimeStamp: kCMTimeZero, decodeTimeStamp: kCMTimeInvalid)

var leftSampleBuffer:CMSampleBufferRef?

status = CMSampleBufferCreate(kCFAllocatorDefault, cmblockbufferref, true, nil, nil, format, leftdata.length, 1, &timing, 0, nil, &leftSampleBuffer)

self.assetWriterAudioInput.appendSampleBuffer(leftSampleBuffer!)

1 个答案:

答案 0 :(得分:2)

我们终于有了这个工作!以下是我们用于将nsdata转换为samplebuffer的最终快速代码:

func NSDataToSample(data:NSData) -> CMSampleBufferRef? {

    var cmBlockBufferRef:CMBlockBufferRef?

    var status = CMBlockBufferCreateWithMemoryBlock(nil, nil, data.length, nil, nil, 0, data.length, 0, &cmBlockBufferRef)

    if(status != 0) {
        return nil
    }

    status = CMBlockBufferReplaceDataBytes(data.bytes, cmBlockBufferRef!, 0, data.length)

    if(status != 0) {
        return nil
    }

    var audioFormat:AudioStreamBasicDescription = AudioStreamBasicDescription()

    audioFormat.mSampleRate = 44100
    audioFormat.mFormatID = kAudioFormatLinearPCM
    audioFormat.mFormatFlags = 0xc
    audioFormat.mBytesPerPacket = 2
    audioFormat.mFramesPerPacket = 1
    audioFormat.mBytesPerFrame = 2
    audioFormat.mChannelsPerFrame = 1
    audioFormat.mBitsPerChannel = 16
    audioFormat.mReserved = 0

    var format:CMFormatDescriptionRef?

    status = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &audioFormat, 0, nil, 0, nil, nil, &format)

    if(status != 0) {
        return nil
    }

    var sampleBuffer:CMSampleBufferRef?

    status = CMSampleBufferCreate(kCFAllocatorDefault, cmBlockBufferRef!, true, nil, nil, format,  data.length/2, 0, nil, 0, nil, &sampleBuffer)

    if(status != 0) {
        return nil
    }

    return sampleBuffer
}