Maxime Dupré
Maxime Dupré

Reputation: 5737

Video playback is a lot faster than expected

I know that the VideoWriter object's FPS should match the FPS of my webcam. My webcam is a Logitech HD Pro Webcam C920 and as you can see, the spec says it has a FPS of 30. Still instead of hardcoding the value, I use cap.get(CV_CAP_PROP_FPS) (which returns 30.0) to be sure to match the FPS.

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, cap.get(CV_CAP_PROP_FPS), (640, 480))

print(cap.get(CV_CAP_PROP_FPS)) # 30.0

This is how I capture each frame of the video:

while(cap.isOpened()):
        # Capture frame-by-frame
        ret, frame = cap.read()
        if ret == True:
                out.write(frame)

My problem is that, even with matching FPSs, the output video (output.avi) has a playback speed of about x3 (according to my non-scientific estimation), which is obviously not what I want. I have tried with different codecs, but the same problem occurs.

There are a couple questions on SO that are addressing this same problem:

OpenCV, captured video runs faster than original camera video!
OpenCV Video capture and fps problem
OpenCV: Video Recording is too fast
http://answers.opencv.org/question/16522/video-recording-is-too-fast/

Most of which suggest to match the FPS of the writer, with the FPS of the webcam, which I did. If I hardcode the FPS to 10, the video playback seems normal, but I don't want to do that, as it has to work generically with many cameras.

I don't really know what to do at this point, but I'll keep searching until I find the solution. Any insight is appreciated!!

Upvotes: 1

Views: 7928

Answers (2)

Maxime Dupré
Maxime Dupré

Reputation: 5737

@MBo correctly answered why the video playback is faster than expected, but I want more information for the people that have the same problem.

First of all, this has a lot too do with the Raspberry Pi, so people using a more performant machine might not have this problem.

Even when simply reading the video stream from the camera and not writing a video file, a maximum of 18-20 FPS at 640 x 480 is achievable. At 1920 x 1080, the maximum FPS is 1-2. The reason is that the webcam frames are read sequentially, thus only one core of the CPU is used for the processing.

When reading the frames in different threads, 24 FPS can be achieved at a 640 x 480 resolution. Still, if you are interested, see this question for more information on reading the webcam frames in parallel.

The only real solution I have found for video applications with the Raspberry Pi is to use the official camera module, which is connected directly to the GPU and offers 1080p with 30 FPS. See this question as to why the Raspberry Pi camera module is a way better alternative to a USB webcam.

Upvotes: 3

MBo
MBo

Reputation: 80107

Check real camera FPS without video writing - for example, count a number of captured frames for 10 seconds
If FPS is close to 30, check the same with video recording. If FPS becomes worse, then you miss some frames, because your system probably is not capable to treat this video stream with given codec (MJPG) - weak processor an so on...

Addition: I think, you would search what codec is the most effective for Raspberry and check whether additional libraries like ffmpeg are used by opencv on your platform.

Upvotes: 2

Related Questions