MaixCAM MaixPy Video Record
2024-05-20
Update history
Date | Version | Author | Update content |
---|---|---|---|
2024-05-20 | 1.0.0 | lxowalle | Initial document |
Introduction
This document provides instructions on how to use the video recording feature
Example 1
An example of recording a video in h265
format.
from maix import video, image, camera, app, time
cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
e = video.Encoder()
f = open('/root/output.h265', 'wb')
record_ms = 2000
start_ms = time.ticks_ms()
while not app.need_exit():
img = cam.read()
frame = e.encode(img)
print(frame.size())
f.write(frame.to_bytes())
if time.ticks_ms() - start_ms > record_ms:
app.set_exit_flag(True)
步骤:
import module and Initialize the camera
from maix import video, image, camera, app, time cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
camera.Camera()
is used to initialise the camera, here the camera resolution is initialised to640x480
, currently theEncoder
only supports theNV21
format, so set the image format toimage.Format.FMT_YVU420SP
.
Initialise the
Encoder
modulee = video.Encoder()
- The
video.Encoder()
module currently only supports processingimage.Format.FMT_YVU420SP
format images, which supportsh265
andh264
encoding, and defaults toh265
encoding. If you want to useh264
encoding, then you can change the initialisation parameter tovideo.Encoder(type=video.VideoType.VIDEO_H264_CBR)
. - Note that only one encoder can exist at the same time
- The
Encoding the camera image
img = cam.read() frame = e.encode(img)
img = cam.read()
read camera image and save toimg
frame = e.encode(img)
encodeimg
and save result toframe
Save the encoded result to file
f = open('/root/output.h265', 'wb') f.write(frame.to_bytes(False))
f = open(xxx)
opens and creates a filef.write(frame.to_bytes(False))
converts the encoding resultframe
to typebytes
and then callsf.write()
to write the data to the file
Timed 2s exit
record_ms = 2000 start_ms = time.ticks_ms() while not app.need_exit(): if time.ticks_ms() - start_ms > record_ms: app.set_exit_flag(True)
- Here is the application logic for the timed exit, see the code for yourself
Done
Example 2
An example of recording a video in h265
format.
from maix import video, time, image, camera, app
cam = camera.Camera(640, 480, image.Format.FMT_YVU420SP)
e = video.Encoder(capture = True)
e.bind_camera(cam)
f = open('/root/output.h265', 'wb')
record_ms = 2000
start_ms = time.ticks_ms()
while not app.need_exit():
frame = e.encode()
img = e.capture()
print(frame.size())
f.write(frame.to_bytes(True))
if time.ticks_ms() - start_ms > record_ms:
app.set_exit_flag(True)
Similar to example 1, the difference is that the Encoder
object's bind_camera
method is called, and the Encoder
takes the initiative to get the camera image, which has the advantage of using the hardware features to increase the encoding speed.
e = video.Encoder(capture = True)
e.bind_camera(cam)
frame = e.encode()
img = e.capture()
e = video.Encoder(capture = True)
enables thecapture
parameter to allow encoding to capture encoded images when encodinge.bind_camera(cam)
binds the camera to theEncoder
objectframe = e.encode()
Instead of passing inimg
when encoding, fetch the image from the camera internallyimg = e.capture()
captures the encoded image from theEncoder
object, which can be used for image processing
Convert to MP4 format
If you want to record video in mp4
format, you can record H265
video first, and then use the ffmpeg
tool in the system to convert to mp4
format.
import os
# Pack h265 to mp4
# /root/output.h265 is the h265 file path
# /root/output.mp4 is the mp4 file path
os.system('ffmpeg -loglevel quiet -i /root/output.h265 -c:v copy -c:a copy /root/output.mp4 -y')