Sending data down a pipe to another process in Python is quite simple, especially if your script spawns that other process. First, you to import want the subprocess module. Then you want to use the Popen
class in there to spawn the other process, specifying subprocess.PIPE
as the stdin
value. As you might expect, that'll cause a pipe to be spawned for communication to the other process via its stdin file.
Then it's just a matter of using the process' stdin as the recording output. The one thing to bear in mind is that you need to close the stdin pipe once recording is finished; picamera won't do it for you because it didn't open the pipe (you did via Popen), so it won't presume to close it for you:
import picamera
import subprocess
# start the gstreamer process with a pipe for stdin
gstreamer = subprocess.Popen([
'gst-launch-1.0', '-v',
'fdsrc',
'!', 'h264parse',
'!', 'rtph264pay', 'config-interval=1', 'pt=96',
'!', 'gdppay',
'!', 'tcpserversink', 'host=YOUR-PI-IP', 'port=5000'
], stdin=subprocess.PIPE)
# initialize the camera
camera = picamera.PiCamera(resolution=(1280, 720), framerate=25)
camera.hflip = True
# start recording to gstreamer's stdin
camera.start_recording(gstreamer.stdin, format='h264', bitrate=2000000)
camera.wait_recording(10)
camera.stop_recording()
# signal to gstreamer that the data's finished then wait for it to terminate
gstreamer.stdin.close()
gstreamer.wait()
As to the rest of your code - you've got some good stuff there, but some can be simplified with a few new toys introduced in picamera 1.11. Specifically, have a look at the new PiCameraCircularIO.copy_to method, and the clear method (just above it in the docs). Those can be used to greatly simplify (or even eliminate) your write_video
function.
Another approach I was thinking of was creating an named pipe that I would open in python and write my video file to continuously. Then I could do
cat named_pipe | gst-launch-1.0 .............
This would allow me to use the regular gst documentation/tutorials rather than pygst as it seems less people use gst.
Thanks for your help.
– Ryan McGrath Jul 16 '16 at 21:55