0

As my first RPi project I'm making a surveillance camera. I would like to be able to remotely see the HD stream, but at the same time, save everything being streamed in lower quality, (space considerations) on the SD card. I got the stream going, but how can I produce another stream with a different quality going into multiple files?

Currently for streaming I'm using

raspivid -o - -t 0 -hf -w 1280 -h 720 -b 1000000 -fps 25|cvlc stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8554}' :demux=h264

But then I cannot use raspivid again... any ideas?

2 Answers 2

0

You can use the video splitter build into picamera to achieve this. We'll set up one stream which pipes output to your cvlc process, and another which writes to disk with a resizer in the pipeline to reduce its resolution.

from subprocess import Popen, PIPE
from picamera import PiCamera

# Start the cvlc process first so we've got something to output to
cvlc = Popen([
    'cvlc',
    'stream:///dev/stdin',
    '--sout',
    '#standard{access=http,mux=ts,dst=:8554}',
    ':demux=h264',
    ], stdin=PIPE)
try:
    with PiCamera(resolution='720p', framerate=25) as camera:
        camera.start_recording(cvlc.stdin, 'h264', bitrate=1000000, splitter_port=1)
        camera.start_recording('output.h264', 'h264', resize=(640, 360), splitter_port=2)
        camera.wait_recording(10)
        camera.stop_recording(splitter_port=1)
        camera.stop_recording(splitter_port=2)
finally:
    # Close the pipe to cvlc so it knows no more data is coming
    cvlc.stdin.close()
    # Wait for cvlc to terminate
    cvlc.terminate()
    cvlc.wait()

The splitter has 4 ports and the resizer runs in the GPU, so you can have up to four simultaneous recordings at different resolutions going to different outputs, including subprocesses via pipes. However, the GPU only has so much "ooomph" - you can max it out without using all 4 ports depending on resolution and other factors. This recipe from picamera's examples shows the basic theory used above.

3
  • Wow, I was not aware of this splitter thing, that will makes things so much easier, I was considering doing a re-encoding already :) . I will test this. Sep 10, 2016 at 10:18
  • @ŁukaszZaroda the resizer is doing the equivalent of re-encoding the stream as my answer has mentioned that the camera is only capable of a single stream. Sep 12, 2016 at 12:55
  • @MohammadAli actually the resizer isn't re-encoding the stream; the camera's video port feeds the splitter, the splitter's first port feeds one H.264 encoder, and the second port feeds a resizer followed by another H.264 encoder. Via the MMAL splitter, the camera can produce a number of streams - more info in the camera hardware section of the picamera docs
    – Dave Jones
    Sep 12, 2016 at 12:58
0

At best you can split the stream and have it save a copy as well, although I know this is not what you asked for the Pi camera is only capable of producing a single video stream at a time so for you to be able to save a copy of lower quality you would have to downsample the video which is not only complex but is also too cpu intensive for your pi to be realistically capable of. Furthermore multiple files would require programming which I think is relatively out of the scope of this question.

To split the raspivid into multiple streams and to save a copy we are going to be using the tee command:

raspivid -o - -t 0 -hf -w 1280 -h 720 -b 1000000 -fps 25|tee /Mohammad.h264|cvlc stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8554}' :demux=h264

Don't forget to use your own file path to save to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.