Posts: 6
Joined: Mon Jul 08, 2019 2:32 am

How can I record a video and data using PiCamera mmal?

Fri Jul 12, 2019 6:37 am

I want to get yuv image while recording video.

So I followed the description of the PiCamera mmalobj. ... -mmal-tour

Code: Select all [0]
   encoding    I420
      buf      1x7589376
    bitrate    0bps
     frame     [email protected] [1]                                 [0] [0]                                 [0] [0]
   encoding    I420                               I420   encoding    I420                               I420      encoding       H264
      buf      1x7589376         -->         1x7589376      buf      1x3133440         -->         1x3133440         buf         1x65536
    bitrate    0bps                               0bps    bitrate    0bps                               0bps       bitrate       1000000bps
     frame     [email protected]         [email protected]     frame     [email protected]         1920x108[email protected]        frame        [email protected]
then, I built a pipeline that looks like this.

However, videos and images are not saved properly and only empty files are created.

Can I know what I did wrong?

Below is my code.

Code: Select all

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

def highres_callback(port, buf):
    # print('highres: ')
    # print(buf)
    return buf.flags & mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END

def lowres_callback(port, buf):
    # print('lowres: ')
    # print(buf)
    return buf.flags & mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END

def main():
    global output1 
    output1 ='high.yuv', 'wb')
    global output2 
    output2 ='low.h264', 'wb')

    camera = mo.MMALCamera()
    resizer = mo.MMALResizer()
    encoder = mo.MMALVideoEncoder()

    camera.outputs[0].framesize = (2592, 1944)
    camera.outputs[0].framerate = 0
    camera.outputs[0].bitrate = 0
    camera.outputs[0].format = mmal.MMAL_ENCODING_I420

    camera.outputs[1].framesize = (2592, 1944)
    camera.outputs[1].framerate = 15
    camera.outputs[1].format = mmal.MMAL_ENCODING_OPAQUE

    resizer.inputs[0].framesize = (2592, 1944)
    resizer.inputs[0].framerate = 15
    resizer.inputs[0].format = mmal.MMAL_ENCODING_OPAQUE

    resizer.outputs[0].framesize = (1920, 1080)
    resizer.outputs[0].framerate = 15
    resizer.outputs[0].format = mmal.MMAL_ENCODING_I420

    encoder.inputs[0].framesize = (1920, 1080)
    encoder.inputs[0].framerate = 15
    encoder.inputs[0].format = mmal.MMAL_ENCODING_I420

    encoder.outputs[0].framesize = (1920, 1080)
    encoder.outputs[0].framerate = 15
    encoder.outputs[0].format = mmal.MMAL_ENCODING_H264
    encoder.outputs[0].bitrate = 1000000
    p = 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
    encoder.outputs[0].params[mmal.MMAL_PARAMETER_PROFILE] = p
    encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER] = True
    encoder.outputs[0].params[mmal.MMAL_PARAMETER_INTRAPERIOD] = 30
    encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT] = 22
    encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT] = 22
    encoder.outputs[0].params[mmal.MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT] = 22




        # mo.print_pipeline(camera.outputs[0])
        # mo.print_pipeline(encoder.outputs[0])

if __name__ == '__main__':
thank you.

Posts: 3
Joined: Mon Jul 22, 2019 6:39 pm

Re: How can I record a video and data using PiCamera mmal?

Mon Jul 22, 2019 6:55 pm

I just came across your post while struggling to put a fairly similar pipeline together - ideally using both the preview and video outputs simultaneously to give two streams with different resolution and coding.

You look to be a bit further ahead than me but your experience is similar - the documentation is a little sparse so quite a lot of trial and error.

One thing you might want to try is explicitly enabling camera.outputs[1] - camera.outputs[0] is already enabled because it provides feedback for auto exposure etc. I was trying to connect camera[1] to the splitter and saw no problems until I enabled the output - then it refused to find a compatible format. I am now thinking of adding the resizer in the pipeline since they mention it can also do format conversions.

Posts: 6
Joined: Mon Jul 08, 2019 2:32 am

Re: How can I record a video and data using PiCamera mmal?

Thu Jul 25, 2019 2:08 am

Thank you for your reply.

But I gave up on Python.

At first, I thought it would be easy to implement with Python, but I couldn't find any source to refer to or learn from.

So I moved on to the C language and referred to raspivid and raspistill, copied most of them, changed the shape of the pipeline a little, and eventually managed to save both video and images at the same time.

Using the preview port looks good, but I felt I couldn't control it and put a splitter on the video port to use it in two ways.

Writing may be strange using a translator.

Anyway, thank you.

Posts: 3
Joined: Mon Jul 22, 2019 6:39 pm

Re: How can I record a video and data using PiCamera mmal?

Thu Jul 25, 2019 9:28 am

Thanks for the reply. I had also started exploring alternatives to python - in my case because the streaming support is very ad-hoc. The best streaming library seems to be written in C++ and I am not sure I could face trying to interface python and c++.

I hadn't considered hacking raspivid so I think I will give that a try. C is also a lot closer to C++ if I do end up needing that streaming library.

Thanks again


Posts: 3
Joined: Mon Jul 22, 2019 6:39 pm

Re: How can I record a video and data using PiCamera mmal?

Sun Aug 04, 2019 4:44 pm

To close this out and to help anyone else landing here with the same problem...

I came across a working example at ... p?t=207341. The main difference to your code is the import of the Event class from the 'threading' library - then used to synchronise activity in the callback routines.

The other thing to note is that camera ports 1 and 2 require the 'capture' property to be set to true/false before/after you try and collect frames.

(I also got a frustrating silent failure using an invalid resolution when configuring the camera!)


Return to “Camera board”