自动录音程序
连接了无线电台,如果频道上有人说话自动录下
所有录音保存在一天的一个录音文件里,会记录下开始时间,停止时间,通话时长,在录音中的开始位置,方便快速定位,无人说话的时候自动停止录音,节省磁盘,
多线程技术,即使程序崩了录音文件也不会损坏
==========
警告:
使用此程序请遵守《中华人民共和国无线电管理条例》 ->>中华人民共和国无线电管理条例
此程序仅限于学习交流使用,不得用于违法用途
import threading
import pyaudio
import copy
import math
import time
import numpy
import wave
localtime = time.localtime()
localtimestr = time.strftime("%Y-%m-%d-%H-%M-%S",localtime)
#ltime = time.time()
line = 0
class RecordThread(threading.Thread):
def __init__(self, audiofile="C:/Users/Public/RE/"+localtimestr+".wav"):
threading.Thread.__init__(self)
self.bRecord = True
self.rr = True
self.audiofile = audiofile
self.chunk = 1024
self.format = pyaudio.paInt16
self.channels = 1
self.rate = 16000
def run(self):
#print("RUN....")
audio = pyaudio.PyAudio()
wavfile = wave.open(self.audiofile, 'wb')
wavfile.setnchannels(self.channels)
wavfile.setsampwidth(audio.get_sample_size(self.format))
wavfile.setframerate(self.rate)
wavstream = audio.open(format=self.format,
channels=self.channels,
rate=self.rate,
input=True,
frames_per_buffer=self.chunk)
global xx
global yy
xx = 0
yy = 0
global line
alltime = 0
ntime1 = 0
ntime2 = 0
starttime = 0
stoptime = 0
timediff = 0
while self.bRecord:
data = wavstream.read(self.chunk)
wavdata = numpy.fromstring(data,dtype=numpy.short)
M = []
for i in range(0,len(wavdata),16000):
M.append(wavdata[i:i+16000]/10)
M=map(abslist,M)
sound = list(map(mean,M))
if sound[0] > 50:
#print("over")
#写入
xx = 1
wavfile.writeframes(data)
else:
xx = 0
if xx > yy:
yy = 1
#START
starttime = time.time()
alltime = round(alltime + timediff,3)
log("StartTime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(starttime))+" 开始时间: "+timestr(alltime))
print("StartTime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(starttime))+" 开始时间: "+timestr(alltime))
if xx < yy:
yy = 0
#STOP
stoptime = time.time()
timediff = round(stoptime - starttime,3)
srt(str(line)+"\n"+timestr(alltime)+" --> "+timestr(alltime+timediff)+"\n"+str(line)+"\n"+"<font color=#5F9F9F>"+time.strftime("%H:%M:%S",time.localtime(starttime))+" -> "+time.strftime("%H:%M:%S",time.localtime(stoptime))+"</font> "+"<font color=#4D4DFF>"+timestr(timediff)+"</font>"+"\n")
line = line + 1
log("StopTime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(stoptime))+" 结束时间: "+timestr(alltime+timediff)+"\n")
print("StopTime: "+time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(stoptime))+" 结束时间: "+timestr(alltime+timediff))
print("时长: "+timestr(timediff)+"\n")
wavstream.stop_stream()
wavstream.close()
audio.terminate()
def stoprecord(self):
print("stop")
self.bRecord = False
def pause(self):
print("pause")
self.rr = False
def next(self):
print("next")
self.rr = True
def abslist(a):
return list(map(abs,a))
def mean(a):
return numpy.longlong(sum(a)) / len(a)
def log(msg):
with open('C:/Users/Public/RE/'+localtimestr+ '.txt','a+') as file:
file.write(msg+"\n")
file.close()
def srt(msg):
with open('C:/Users/Public/RE/'+localtimestr+ '.srt','a+') as file:
file.write(msg+"\n")
file.close()
def timestr(sec):
m,s = divmod(sec,60)
h,m = divmod(m,60)
return str("%d:%02d:%.2f"%(h,m,s))
rt = RecordThread()
line = line + 1
#print(timestr(2.65))
log("RUN ...... Start At "+localtimestr+" SYS OK!"+" Frequency:91.1Mhz")
srt(str(line)+"\n"+"0:00:00.0 --> 0:00:30.0\n"+"{\\an8}"+"<font color=#FFFF00>"+ str(time.strftime("%Y/%m/%d %H:%M:%S",localtime))+"</font>"+" <font color=#00FFFF>(20:00-21:00)</font>"+"\n"+"<font color=#00FF00>438.025 -5 88.5 <i>QTH Suzhou Jiangsu China</i></font>\n<font color=#3299CC>苏州市业余无线电 472752158</font>\n\n1\n0:00:00.0 --> 0:00:30.0\n{\\an5}请遵守<font color=#FF0000><u><b>《中华人民共和国无线电管理条例》</b></u></font>\n")
print("RUN ...... Start At "+localtimestr+" SYS OK!"+" Frequency:91.1Mhz")
rt.start()