pyVoIP
pyVoIP copied to clipboard
Example for receive a call and record it
I try to record the call. For this I use this part in the callback function answer
print("+++++++++++++++++ Get Audio from caller and write file")
w = wave.open('test_wave.wav', 'wb')
w.setparams((1, 16 // 8, 8000, 0, 'NONE', 'NONE'))
while call.state == CallState.ANSWERED:
data = call.read_audio()
#print(data)
w.writeframesraw(data)
w.close()
This writes a wav file, but I only get is very noisy. I think the parameters in setparams a not right. But I don't know what to set here.
Are you sure that you receive 2 Bytes = 16 Bit? Normally you will get 8 Bit = 1 Byte. It worked for me, and the saved audio-file is totally okay.
My Code:
w = wave.open('test_wave.wav', 'wb')
w.setnchannels(1)
w.setsampwidth(8 // 8) # 8 Bit = 1 Byte
w.setframerate(8000)
w.close
That's correct, PCMU/A is 8 bit
Thanks, sometimes the right solution is so easy. It works for me too.
This code works, recording is permanently. How can i stop recording by time or (will be better) by silent in channel?
w = wave.open('test_wave.wav', 'wb')
w.setnchannels(1)
w.setsampwidth(8 // 8)
w.setframerate(8000)
while call.state == CallState.ANSWERED:
data = call.read_audio()
w.writeframesraw(data)
w.close()
call.hangup()
You could probably do something like:
if data != b"\x80" * len(data):
w.writeframes(data)
I'm trying to record a few seconds of a certain part in a call. However, I can't even get a simple recording to work properly...
I want to save a recording as a .wav file.
If I'm using: data = call.read_audio(blocking=False)
it starts to produce an multiple hour long .wav file that's pretty large (+ 200Mb) and does not even contain any audio...
When I'm using data = call.read_audio(blocking=True)
it spits out a 365kb file with 0 seconds...
What am I missing here?
from pyVoIP.VoIP import VoIPPhone, InvalidStateError, CallState
import time
import wave
# ACCOUNT DATA:
SIP_IP = 'pbx.easybell.de'
SIP_Port = 5060
SIP_Username = 'placeholder'
SIP_Password = 'placeholder'
myIP = 'placeholder'
def answer(call):
w = wave.open('test_wave.wav', 'wb')
w.setnchannels(1)
w.setsampwidth(2)
w.setsampwidth(8 // 8)
w.setframerate(8000)
call.answer()
while call.state == CallState.ANSWERED:
print("recording start")
data = call.read_audio(blocking=False)
w.writeframesraw(data)
w.close()
print("recording saved")
if __name__ == "__main__":
phone = VoIPPhone(SIP_IP, SIP_Port, SIP_Username, SIP_Password, myIP=myIP, callCallback=answer)
phone.start()
input('Press enter to disable the phone')
phone.stop()
I'm trying to record a few seconds of a certain part in a call. ...
> while call.state == CallState.ANSWERED:
> print("recording start")
> data = call.read_audio(blocking=False)
Don't use print() in the function answer(). If you need logging then use recording into a file. Parameter "blocking" better left by default.
@TipsTricksMore, definitely heed what @Koshkodav said about blocking. But I think your problem is you don't want to be using writeframesraw
, if I recall correctly, that does not increase the frame counter in the wav file so it thinks you haven't added any data when you have. But yeah, if you set blocking to false it's going to add a TON of empty sound in between packets.
w = wave.open('test_wave.wav', 'wb')
w.setnchannels(1)
w.setsampwidth(2)
w.setsampwidth(8 // 8)
w.setframerate(8000)
call.answer()
while call.state == CallState.ANSWERED:
data = call.read_audio(blocking=False)
if data != b"\x80" * len(data):
w.writeframesraw(data)
w.close()
call.hangup()
I am getting only a 0-sec file what am I doing wrong? I am not receiving any audio from another side I don't know why. Any suggestions
I believe the issue you are having is the writeframesraw
call. Per the Python documentation writeframesraw
does not increment the frames counter in the wav file. You should use writeframes
instead.