Pyaudio:如何压缩音频流 [英] Pyaudio : how to compress audio stream

查看:52
本文介绍了Pyaudio:如何压缩音频流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用python作为客户端服务器来开发VOIP工具,如下所示:

I'm currently developping a VOIP tool in python working as a client-server as follows :

CHUNK = 1024
p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.paInt16,
        channels = 1,
        rate = 44100,
        input = True,
        frames_per_buffer = CHUNK)

while 1:
    connection.sendVoice(stream.read(CHUNK))

我该如何继续将发送的数据压缩为备用连接,也许可以提高速度,...

How could I proceed to compress the sent data to spare connection, maybe increase speed, ...

推荐答案

import time, sys,io
import pymedia.audio.sound as sound
import pymedia.audio.acodec as acodec
import pymedia.muxer as muxer


def voiceRecorder( secs, name ):
  f = open(name,'wb')
  secs = secs*5
  dm= muxer.Demuxer('mp3')
  snds= sound.getODevices()
  rt = 44100
  cparams= { 'id': acodec.getCodecID( 'mp3' ),
             'bitrate': 128000/4,
             'sample_rate': rt,
             'channels': 2 } 
  ac= acodec.Encoder( cparams )
  snd= sound.Input( rt, 2, sound.AFMT_S16_LE )
  snd.start()
  start_time = time.time()

  while snd.getPosition()<= secs:

   s= snd.getData()
   if s and len( s ):

     for fr in ac.encode( s ):
       f.write( fr)

    else:
      time.sleep(.25)



  snd.stop()


if __name__ == "__main__":
  if len( sys.argv )!= 3:
    print 'Usage: voice_recorder <seconds> <file_name>'
  else:
    voiceRecorder( int( sys.argv[ 1 ] ), sys.argv[ 2 ]  )

这篇关于Pyaudio:如何压缩音频流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆