Created
December 17, 2024 11:18
DepthAI OAK video encoding 12Mp with H264/H265
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import depthai as dai | |
# Create pipeline | |
pipeline = dai.Pipeline() | |
# Define sources and output | |
camRgb = pipeline.create(dai.node.ColorCamera) | |
videoEnc = pipeline.create(dai.node.VideoEncoder) | |
xout = pipeline.create(dai.node.XLinkOut) | |
xout.setStreamName('h265') | |
# Properties | |
camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A) | |
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_12_MP) | |
# Max H26x encoding is 248.8 MPix/s - so 4K@30 or 12MP@20 | |
camRgb.setFps(20) | |
videoEnc.setDefaultProfilePreset(20, dai.VideoEncoderProperties.Profile.H265_MAIN) | |
convert_manip = pipeline.create(dai.node.ImageManip) | |
# Video encoder input frames need to be in NV12 input | |
convert_manip.initialConfig.setFrameType(dai.ImgFrame.Type.NV12) | |
# H26x needs width to be multiple of 32, height to be multiple of 8 | |
# So we crop from 4056x3040 to 4032x3040 | |
convert_manip.initialConfig.setResize(4032, 3040) | |
# 18.5MB per 12MP NV12 frame | |
convert_manip.setMaxOutputFrameSize(18495360) | |
camRgb.isp.link(convert_manip.inputImage) | |
# Linking | |
convert_manip.out.link(videoEnc.input) | |
videoEnc.bitstream.link(xout.input) | |
# Connect to device and start pipeline | |
with dai.Device(pipeline) as device: | |
# Output queue will be used to get the encoded data from the output defined above | |
q = device.getOutputQueue(name="h265", maxSize=30, blocking=True) | |
# The .h265 file is a raw stream file (not playable yet) | |
with open('video.h265', 'wb') as videoFile: | |
print("Press Ctrl+C to stop encoding...") | |
try: | |
while True: | |
h265Packet = q.get() # Blocking call, will wait until a new data has arrived | |
h265Packet.getData().tofile(videoFile) # Appends the packet data to the opened file | |
except KeyboardInterrupt: | |
# Keyboard interrupt (Ctrl + C) detected | |
pass | |
print("To view the encoded data, convert the stream file (.h265) into a video file (.mp4) using a command below:") | |
print("ffmpeg -framerate 30 -i video.h265 -c copy video.mp4") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment