video-encoder.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import depthai as dai
  2. # Create pipeline
  3. pipeline = dai.Pipeline()
  4. # Define sources and output
  5. camRgb = pipeline.create(dai.node.ColorCamera)
  6. videoEnc = pipeline.create(dai.node.VideoEncoder)
  7. xout = pipeline.create(dai.node.XLinkOut)
  8. xout.setStreamName('h265')
  9. # Properties
  10. camRgb.setBoardSocket(dai.CameraBoardSocket.RGB)
  11. camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K)
  12. videoEnc.setDefaultProfilePreset(3840, 2160, 30, dai.VideoEncoderProperties.Profile.H265_MAIN)
  13. # Linking
  14. camRgb.video.link(videoEnc.input)
  15. videoEnc.bitstream.link(xout.input)
  16. # Connect to device and start pipeline
  17. with dai.Device(pipeline) as device:
  18. # Output queue will be used to get the encoded data from the output defined above
  19. q = device.getOutputQueue(name="h265", maxSize=30, blocking=True)
  20. # The .h265 file is a raw stream file (not playable yet)
  21. with open('video.h265', 'wb') as videoFile:
  22. print("Press Ctrl+C to stop encoding...")
  23. try:
  24. while True:
  25. h265Packet = q.get() # Blocking call, will wait until a new data has arrived
  26. h265Packet.getData().tofile(videoFile) # Appends the packet data to the opened file
  27. except KeyboardInterrupt:
  28. # Keyboard interrupt (Ctrl + C) detected
  29. pass
  30. print("To view the encoded data, convert the stream file (.h265) into a video file (.mp4) using a command below:")
  31. print("ffmpeg -framerate 30 -i video.h265 -c copy video.mp4")