1

I have a circular buffer that is monitored for motion and saves clips to disk. Following the everything is a file philosophy, I should be able to write to a Unix pipe like I write to a file and make the gstreamer end work. The problem is I can't figure out how to write my video to a pipe.

How do I write to a pipe? Will writing to a pipe recreate the raspivid portion of this pipelint to create an RTSP stream that allows me to watch my dog at work?

raspivid -t 0 -w 1080 -h 720 -fps 25 -hf -b 2000000 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=VIDSERVERIP port=5000

My code

Ryan McGrath
  • 113
  • 1
  • 3

1 Answers1

1

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.

Dave Jones
  • 3,988
  • 16
  • 22