import sounddevice as sd
from scipy.io.wavfile import write
import time
import uuid #获取系统mac
import socket
#注意必须安装ffmpeg,并且添加 环境变量-在系统变量 Path,添加后 打开cmd,输入ffmpeg -version 正确才可以
from pydub import AudioSegment #wav转mp3
import os
def get_mac():
#mac
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
# 获取主机名
hostname = socket.gethostname()
# 获取IP
ip = socket.gethostbyname(hostname)
# 结果数据导出
pc_mac = {‘mac’:mac,‘hostname’:hostname,‘ip’:ip}
return pc_mac
def get_wav_write(file_name,seconds):
print(‘录音开始:’, file_name)
fs = 16000 # Sample rate
seconds = seconds # 时长秒数秒钟
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
sd.wait() # Wait until recording is finished
write(’./wav/’ + file_name + ‘.wav’, fs, myrecording) # Save as WAV file
wav_name = ‘./wav/’ + file_name + ‘.wav’
# 确认文件生成后进行转化并删除源文件
if (os.path.isfile(wav_name)):
# 同步转换成mp3
sourcefile = AudioSegment.from_wav(wav_name)
filename = wav_name.split(’/’)[-1].split(’.wav’)[0].replace(’ ‘, ‘_’) + ‘.mp3’
sourcefile.export(’./wav/’ + filename, format=“mp3”)
os.remove(wav_name)
return filename
#时间计算
def get_current_time(input_date =‘0’):
#如果时间传入为空
if input_date ==‘0’:
ct = time.time()-246060
local_time = time.localtime(ct)
data_head = time.strftime("%Y%m%d%H%M%S", local_time)
data_secs = abs(ct - round(ct)) * 1000
time_stamp = “%s%03d” % (data_head, data_secs)
else:
time_stamp = input_date +‘120000001’
return time_stamp
def getFileName(filepath):
file_list = []
for root,dirs,files in os.walk(filepath):
for filespath in files:
if ‘wav’ in filespath.split(’.’)[-1]:
file_list.append(os.path.join(root,filespath))
return file_list
if name == ‘main’:
#获取系统数据
mac_id = get_mac()
# 展现数据
print('计算机参数展示', mac_id)
#获取内容
mac = mac_id['mac']
hostname = mac_id['hostname']
ip = mac_id['ip']
# 程序执行前先确认./wav 是否存在,不存在新建
file_path_state = os.path.exists('./wav')
if file_path_state == False:
os.mkdir('./wav')
# 循环执行
while True:
#获取系统时间
in_time = get_current_time()[0:14]
#print('录音开始',in_time)
wav_write = get_wav_write(mac +'_'+ in_time,300)
# 因为这个进程会自动延后300秒,所以不需要时间延迟
print(in_time[0:12],'生成文件:',wav_write)