Cannot write video frame using OpenCV VideoWriter

Hi all,

I used OpenCV4Tegra(Version 2.4.13) and Python to record the video. However, I always get the blank result video. Does anyone face the same problem like me?

A piece of code below:

fourcc = cv2.cv.CV_FOURCC(‘X’, ‘V’, ‘I’, ‘D’)
videoRecorder = cv2.VideoWriter(‘pcs_video.avi’,fourcc,20.0,(640,480))

videoRecorder.write(frame)

HI,

Please try my code

import sys
import cv2

def read_cam():
    cap = cv2.VideoCapture(1)
    # Define the codec and create VideoWriter object
    fourcc = cv2.cv.CV_FOURCC(*'XVID')
    out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
    if cap.isOpened():
        while True:
            ret_val, img = cap.read();
            out.write(img)
            #cv2.imshow('demo',img) 
            #cv2.waitKey(10)

    else:
     print "camera open failed"

    out.release
    cv2.destroyAllWindows()


if __name__ == '__main__':
    read_cam()

Hi,

Thank you for your sample code but I’ve not yet done with this problem.
My purpose’s to record the color and depth frame from Kinect Xbox One using libfreenect2 & OpenCV4Tegra. However, I always get the blank video.

fourcc = cv2.cv.CV_FOURCC(*‘XVID’)
videoRecorder = cv2.VideoWriter(‘video_2x.avi’,fourcc,20.0,(int(1920 / 3), int(1080 / 3))) ## Resize color frame (1920,1080)

rgbFrame = cv2.resize(color.asarray(),(int(1920 / 3), int(1080 / 3)))
videoRecorder.write(rgbFrame)

Thank you.

Hi Kata,

So the frames from Kinect are okay for usecase like preview? The problem only happens when recording?

Hi WayneWWWW,

Thank you for your prompt rely.

So the frames from Kinect are okay for usecase like preview?The problem only happens when recording?
Yes, I can view the frame correctly by displaying the frame. The problem is come from recording.

To align our environment, you are using rel-28.1 from Jetpack with opencv4tegra, right? Do you install full package?

Could you try usb camera first? Because my code was using a web cam for testing.

Hi WayneWWW,

Thank you so much for your strong support!

To align our environment, you are using rel-28.1 from Jetpack with opencv4tegra, right? Do you install full >package?
Yes, it’s right. I installed full package.

Could you try usb camera first? Because my code was using a web cam for testing.
Yeath, your code’s been worked perfectly. That means the encoder also works correctly.

Any suggestion for my problem?

Hi,

Do you read depth data into cv::Mat?
If yes, which format do you use?

Usually, we use CV_8UC3 for camera input, which stands for [0-255] color image.
If you not in CV_8UC3, there is possibly an auto-truncate when encoding the video. This truncate may cause some information loss.

Thanks.

This might be very late for you but might help someone else. I also went through the same problem recently.
Found out that the video dimensions of the output file should match the dimension of the frame
you are trying to write.

out = cv2.VideoWriter(‘output.avi’,fourcc, 20.0, (W,H))

where W, H are width and height of video frames respectively.

If you are reading the input frame with VideoCapture object to read frames from another video stream as
cap = cv2.VideoCapture(1)

use
W = cap.get(3)
H = cap.get(4)

If you are reading the frame from another source or method,
use

W = input_frame.shape[0]
H = input_frame.shape[1]

assuming input_frame is an numpy array.

Hope this helps.

Nice! Thanks for sharing!