You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.8 KiB
Python

#!/usr/bin/env python3
import subprocess
import shutil
import shlex
import os
import mouse
from datetime import datetime
from time import sleep
home_path = os.path.dirname(os.path.abspath(__file__))
recordings_path = home_path + '/audios/'
archive_path='/media/pi/97A6-3AD6'
#recordings_path = 'audios/'
recording_tmp_file = recordings_path + '.temp.wav'
cmd = "arecord -f cd -D hw:1,0 {}".format(recording_tmp_file)
cmd = shlex.split(cmd)
start_rec=datetime.now()
is_recording = False
while True:
if mouse.is_pressed(button='left') and not is_recording:
start_rec=datetime.now()
is_recording = True
# start recording
print ('rec')
subprocess.Popen('espeak recording',shell=True)
rec = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=False)
sleep(1)
elif (mouse.is_pressed(button='left') and is_recording) or ((datetime.now()-start_rec).seconds > 60 and is_recording):
is_recording = False
print('stop')
# stop recording & rename soundfile
rec.kill()
now = datetime.now().strftime('%Y%m%d-%H%M%S')
recording_file = recordings_path + 'rec_' + now + '.wav'
subprocess.Popen('espeak stop recording',shell=True)
print(recording_tmp_file, recording_file)
os.rename(recording_tmp_file, recording_file)
#if sum([len(files) for r, d, files in os.walk(recordings_path)])>10:
audiofiles=os.listdir(path=recordings_path)
if len(audiofiles)>5:
for audiofile in audiofiles:
if audiofile.startswith("rec_"):
print (audiofile)
shutil.move(recordings_path + audiofile,(archive_path + '/archive_audios/'))
print ('recordings moved to archive_audios')
sleep(1)
sleep(0.01)