Skip to content

Instantly share code, notes, and snippets.

@gbrls
Created October 10, 2021 02:28
Show Gist options
  • Save gbrls/3257bfa16bf66f92ca27b870e2386aa4 to your computer and use it in GitHub Desktop.
Save gbrls/3257bfa16bf66f92ca27b870e2386aa4 to your computer and use it in GitHub Desktop.
Record audio and copy it to clipboard
#!/bin/python3
import os, subprocess
source = 'alsa_output.pci-0000_00_1f.3.analog-stereo' # Got this from pw-cli
path = '/tmp/record.wav'
timeout = 60
trim = '00.500'
record_cmd = f'timeout {timeout} parecord --channels=1 -d {source} {path}'
play_cmd = f'mpv {path}'
# This -t text/uri-list is our way to specify which target this data is going to
copy_cmd = f'echo file://{path} | xclip -selection c -t text/uri-list'
find_cmd = f'''ps -aux | grep {path} | grep timeout | grep parecord'''
notify_cmd = f'notify-send Recording done!'
def start():
os.system(record_cmd)
def end(pid):
os.system(f'kill {pid}')
os.system(f'ffmpeg -y -i {path} -ss 00:00:{trim} -c copy {path}')
os.system(copy_cmd)
os.system(notify_cmd)
def getpid():
#output = subprocess.check_output(find_cmd, shell=True).decode('utf-8')
p = subprocess.Popen(find_cmd, shell=True, stdout=subprocess.PIPE)
cur_pid = p.pid
out, err = p.communicate()
output = out.decode('utf-8')
print(output)
pid = output.split()[1]
#cur_pid = os.getpid()
print(pid, cur_pid)
if int(pid) != cur_pid:
return pid
else:
return None
pid = getpid()
if pid == None:
start()
else:
end(pid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment