0

Using the documentation for picamera, I am trying to write my own video capturing pipeline on a Raspberry Pi Zero W. The reason for this is that i want to insert a processing step in the pipeline later. For now, I only want a working camera/video recording pipeline.

First, I tried running the code presented in chapter 16.1.12 for rendering a clock on the camera output and sending it to both preview and a video file. This only gave errors of the type:

Exception ignored on calling ctypes callback function: <function MMALPort.enable.<locals>.wrapper at 0xb5e36388>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1227, in wrapper
    self._pool.send_buffer(block=False)
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1931, in send_buffer
    super(MMALPortPool, self).send_buffer(port, block, timeout)
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 1881, in send_buffer
    raise PiCameraMMALError(mmal.MMAL_EAGAIN, 'no buffers available')
picamera.exc.PiCameraMMALError: no buffers available: Resource temporarily unavailable; try again later 

This also happened after a reboot of the Pi. raspistill -o image.jpg also worked without problem.

I decided to try and strip away the clock part, and only build the video pipeline. However, when running it, it invariably produces a file only 27 bytes in size.

Here is my code:

from picamera import mmal, mmalobj as mo
from time import sleep



class OwnCamera_H264():
    def __init__(self, resolution, framerate):
        # Instantiate members of camera pipeline
        self.camera = mo.MMALCamera()
        #self.rotator = Rotator.PiCam_rotator()
        self.encoder = mo.MMALVideoEncoder()
        self.target = None

        # Configure camera output 1 (Video port)
        self.camera.outputs[1].framesize = resolution
        self.camera.outputs[1].framerate = framerate
        self.camera.outputs[1].commit()

        # Configure H.264 encoder
        self.encoder.outputs[0].format = mmal.MMAL_ENCODING_H264
        self.encoder.outputs[0].bitrate = 2000000
        self.encoder.outputs[0].commit()
        p = self.encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE]
        p.profile[0].profile = mmal.MMAL_VIDEO_PROFILE_H264_HIGH
        p.profile[0].level = mmal.MMAL_VIDEO_LEVEL_H264_41
        self.encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] = p
        self.encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER] = True
        self.encoder.outputs[0].params[mmal.MMAL_PARAMETER_INTRAPERIOD] = 30
        self.encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT] = 22
        self.encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT] = 22
        self.encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT] = 22

        # Connect encoder to camera output
        self.encoder.inputs[0].connect(self.camera.outputs[1])

    def start_recording(self, filename):
        # Make target, and connect to encoder
        self.target = mo.MMALPythonTarget(filename)
        self.target.inputs[0].connect(self.encoder.outputs[0])

        # Enable all connections, and then enable the members themselves.
        self.target.connection.enable()
        self.encoder.connection.enable()
        self.target.enable()
        self.encoder.enable()
        self.camera.enable()



    def stop_recording(self):
        self.camera.disable()
        self.target.disable()
        self.encoder.disable()
        self.target.inputs[0].disconnect()
        self.encoder.inputs[0].disconnect()
        self.target = None

def main():
    och = OwnCamera_H264((640, 480), 30)
    sleep(2)
    och.start_recording('test.h264')
    sleep(10)
    och.stop_recording()


if __name__ == '__main__':
    main()

I'm running Raspberry Pi OS Bullseye

3
  • If you're using Bullseye surely you should be using the new picamera2 ? raspberrypi.com/news/a-preview-release-of-the-picamera2-library
    – CoderMike
    Jun 29 at 13:56
  • @CoderMike Ideally yes, but as it is (as far as I can see) very much a work in progress, I prefer using the legacy picamera and migrating when picamera2 is officially released.
    – mandeala
    Jun 30 at 6:36
  • @CoderMike but maybe it's worth degrading to buster in this case?
    – mandeala
    Jun 30 at 13:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.