1

I copied a script from adaftut which allows to show the camera stream on the TFT LCD. However using that stream I cannot save the camera recording.

The script opens io.BytesIO, than gets the camera captures into that stream and than closes the stream in while loop. And I cannot save the stream as a video. This is part of the code:

while(True):
    stream = io.BytesIO() # Capture into in-memory stream
    camera.capture(stream, use_video_port=True, format='raw')
    stream.seek(0)
    stream.readinto(yuv)  # stream -> YUV buffer
    stream.close()
    yuv2rgb.convert(yuv, rgb, sizeData[sizeMode][1][0],
      sizeData[sizeMode][1][1])
    img = pygame.image.frombuffer(rgb[...], 'RGB')

Can you please help to save the stream as a recording? I need both to save camera recording and to preview camera data on LCD TFT.

haykp
  • 435
  • 12
  • 29

1 Answers1

2

Hayk jan,

Most probably you need to use some libraries which work with video files. For example you could use OpenCV to save your stream into video file. Example of it can be found in answer. For your code it should be something like:

import cv2

# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))

while(True):
    stream = io.BytesIO() # Capture into in-memory stream
    camera.capture(stream, use_video_port=True, format='raw')
    stream.seek(0)
    stream.readinto(yuv)  # stream -> YUV buffer
    stream.close()
    yuv2rgb.convert(yuv, rgb, sizeData[sizeMode][1][0],
      sizeData[sizeMode][1][1])
    img = pygame.image.frombuffer(rgb[...], 'RGB')

    out.write(img)

out.release()
Gagik Sukiasyan
  • 846
  • 6
  • 17