iOS音频推流格式转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS音频推流格式转换相关的知识,希望对你有一定的参考价值。

参考技术A 音频采集会得到格式为CMSampleBufferRef的音频包,音频推流一般把音频流转换成flv格式

(1)将音频流CMSampleBufferRef转换成PCM data

- (NSData *)convertAudiosmapleBufferToPcmData:(CMSampleBufferRef) audioSample

//获取数据大小

NSInteger audioDataSize =CMSampleBufferGetTotalSampleSize(audioSample);

//分配数据空间

int8_t * audio_data =aw_alloc((int32_t)audioDataSize);

//创建CMBlockBufferRef,获取PCM数据

CMBlockBufferRef dataBuffer =CMSampleBufferGetDataBuffer(audioSample);

//将数据copy至分配的内存中

CMBlockBufferCopyDataBytes(dataBuffer,0, audioDataSize, audio_data);

//返回PCM data

return [NSData dataWithBytesNoCopy:audio_data length:audioDataSize];



(2)将音频PCM data编码成acc格式并合成为flv

- (aw_flv_audio_tag*)encodePCMDataToFlvTag:(NSData*)pcmData

self.curFramePcmData= pcmData;

AudioBufferList outAudioBufferList = 0;

outAudioBufferList.mNumberBuffers=1;

outAudioBufferList.mBuffers[0].mNumberChannels = (uint32_t)self.audioConfig.channelCount;

outAudioBufferList.mBuffers[0].mDataByteSize = self.aMaxOutputFrameSize;

outAudioBufferList.mBuffers[0].mData = malloc(self.aMaxOutputFrameSize);

uint32_t outputDataPacketSize =1;

OSStatus status =AudioConverterFillComplexBuffer(_aConverter,aacEncodeInputDataProc, (__bridgevoid*_Nullable)(self), &outputDataPacketSize, &outAudioBufferList,NULL);

if(status == noErr)

NSData * rawAAC = [NSDatadataWithBytesNoCopy: outAudioBufferList.mBuffers[0].mDatalength:outAudioBufferList.mBuffers[0].mDataByteSize];

self.manager.timestamp += 1024*1000/self.audioConfig.sampleRate;

return aw_encoder_create_audio_tag((int8_t*)rawAAC.bytes, rawAAC.length, (uint32_t)self.manager.timestamp, &_faacConfig);

else

NSLog(@"aac编码错误");



return NULL;

Amr音频转换Mp3格式

最近研发部门有一个需求andriod手机端的音频格式amr或wav格式的音频文件需要统一在线转换,为了保证快速并行完成我使用的goroutine和chan,我已经封装好了docker镜像,大家有兴趣的可以下载直接使用:

docker pull docker.io/liyinda/alpine_amrtomp3

github地址如下:

https://github.com/liyinda/AmrToMp3

部分逻辑代码如下:

  //将filelist的文件列表传入ch管道 
  ch :=make(chan string)

  //创建goroutine获取文件列表并将列表传递给管道另一端进行音频转换
  go func(mine []os.FileInfo) {
    for _, item := range mine {
      ch <- item.Name()
    }
  }(filelist)

  go func() {
    for i :=0; i<= cpuNum; i++ {
      file := <-ch

      //获取文件的内容
      wfilepath := WORKDIR + file
      bfilepath := BAKDIR + today + "/" + file
      pfilepath := PRODUCTDIR + today + "/" + file

      var rs bool
      var fileExt string
      //获取文件名后缀
      fileExt = path.Ext(file) 
      newfilepath := ""
      audiofilepath := ""

      //操作日志记录
      logFile, logErr := os.OpenFile(*logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
      if logErr != nil {
        fmt.Println("Fail to find", *logFile, "AmrToMp3 start Failed")
        os.Exit(1)
      }
      defer logFile.Close()
      log.SetOutput(logFile)
      log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

      switch {
        case fileExt == ".amr" :
          //进行amr到mp3的转换
          newfilepath = strings.Replace(wfilepath, ".amr", ".mp3", -1)
          comeAndTo := [...]string{wfilepath, newfilepath}
          rs = models.Arm2mp3(comeAndTo)
          log.Printf("%v Amr format conversion success 
", newfilepath)
        case fileExt == ".wav" :
          //进行wav到mp3的转换
          newfilepath = strings.Replace(wfilepath, ".wav", ".mp3", -1)
          comeAndTo := [...]string{wfilepath, newfilepath}
          rs = models.Arm2mp3(comeAndTo)
          log.Printf("%v Wav format conversion success 
", newfilepath)
        default:
          //os.Remove(wfilepath)
          os.Rename(wfilepath, bfilepath)
          log.Printf("%v conversion fail and backup 
", bfilepath)
      }

      if rs {
        //转换成功之后,将原始文件移动到备份目录
        os.Rename(wfilepath, bfilepath)
        //生产mp3文件路径
        audiofilepath = strings.Replace(pfilepath, ".amr", ".mp3", -1)
        //转换成功之后,将生成文件移动到生产目录
        os.Rename(newfilepath, audiofilepath)
      }
    }
  }()

以上是关于iOS音频推流格式转换的主要内容,如果未能解决你的问题,请参考以下文章

iOS-音频格式转换-b

FFMPEG常用命令行

Amr音频转换Mp3格式

格式工厂怎么将MPEG4格式的音频转换成mp3

iOS快捷转音视频格式,有它就够了

MP3转AAC音频格式怎么使用音频转换器转换?