gold715
Posts: 3
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.
https://picamera.readthedocs.io/en/rele ... -mmal-tour

Code: Select all

 vc.ril.camera [0]
   encoding    I420
      buf      1x7589376
    bitrate    0bps
     frame     [email protected]
     
 vc.ril.camera [1]                                 [0] vc.ril.resize [0]                                 [0] vc.ril.video_encode [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]         1920x1088[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)
    output1.write(buf.data)
    return buf.flags & mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END

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

def main():
    global output1 
    output1 = io.open('high.yuv', 'wb')
    global output2 
    output2 = io.open('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[0].commit()

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

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

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

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

    encoder.outputs[0].framesize = (1920, 1080)
    encoder.outputs[0].framerate = 15
    encoder.outputs[0].format = mmal.MMAL_ENCODING_H264
    encoder.outputs[0].bitrate = 1000000
    encoder.outputs[0].commit()
    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

    resizer.inputs[0].connect(camera.outputs[1])
    encoder.inputs[0].connect(resizer.outputs[0])

    camera.outputs[0].enable(highres_callback)
    encoder.outputs[0].enable(lowres_callback)
    
    encoder.connection.enable()
    resizer.connection.enable()

    encoder.enable()
    resizer.enable()
    camera.enable()

    try:
        sleep(3)
        
    finally:
        encoder.disable()
        resizer.disable()
        camera.disable()
        
        # mo.print_pipeline(camera.outputs[0])
        # mo.print_pipeline(encoder.outputs[0])
        
        encoder.inputs[0].disconnect()
        resizer.inputs[0].disconnect()
        output1.close()
        output2.close()

if __name__ == '__main__':
    main()
        
        
thank you.

Return to “Camera board”