Running a script when recording starts and stops

I’d like to run a shell command before recording starts and after recording stops. Is there a way to do this?

In my case, the script would turn off the computer fan before recording and turn it back on after recording, to minimize background noise.

If macOS shell supports “named pipes” then you could start recording from shell and then send any other commands from shell. See: