I'm trying to record and convert a stream with python using arecord.
In the shell this is a command more or less like:
arecord -B 5000 -f dat | lame -m j -q 5 -V 2 - test.mp3 &
In Python I tried this with subprocess.Popen:
reccmd = ["arecord", "-B", "5000", "-f", "dat"]
mp3cmd = ["lame", "-m", "j", "-q", "5", "-V", "2", "-", "test.mp3"]
p = subprocess.Popen(reccmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(mp3cmd, stdin=p.stdout)
p2.communicate()
But I don't know how to include the "&" or something similar in the code to be able to stop the recording with a
killall arecord
command
To use a pipe with the subprocess module, you have to pass shell=True . In your particular case, however, the simple solution is to call subprocess. check_output(('ps', '-A')) and then str. find on the output.
The popen() function executes the command specified by the string command. It creates a pipe between the calling program and the executed command, and returns a pointer to a stream that can be used to either read from or write to the pipe.
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions: os.
If you don't want the main Python process to block while executing the subprocesses, simply don't call p2.communicate()
. The calls to subprocess.Popen()
don't block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With