1

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!!

Community
  • 1
  • 1
Maxime Dupré
  • 5,319
  • 7
  • 38
  • 72

2 Answers2

3

@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.

Community
  • 1
  • 1
Maxime Dupré
  • 5,319
  • 7
  • 38
  • 72
2

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.

MBo
  • 77,366
  • 5
  • 53
  • 86
  • I'm using a raspberry pi, so it might be that the processor is too weak. How would you count the number of captured frames for 10 seconds? Increment a variable for each iteration of the while loop and using methods like time.clock() and/or time.time() to monitor the number of seconds that have passed? Anyway, thank you for the help, i'll definitely try that asap today. – Maxime Dupré Sep 22 '15 at 14:47
  • When testing manually as you suggested, the camera FPS is about 14 *without* video writing, so I guess the processor is weak. With the video writing it's about 8. However, I'm wondering how I'm suppose to write video files that have an accurate playback with this technique. This means that if something else is running on the computer or a random computing task occurs at the same time the video file is being written, the result won't be good. Clearly there must be another way of doing what I want to do, without being at the mercy of the processor. What do you think? – Maxime Dupré Sep 23 '15 at 00:48
  • Also ffmpeg is used on my platform, as when trying different codecs/file formats combinations, I get this type of error message: `OpenCV: FFMPEG: tag 0x43564144/'DAVC' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'` – Maxime Dupré Sep 23 '15 at 01:14
  • You added raspberry tag, and I hope that rasp guys could give more specific advices - hardware acceleration etc.. I worked with old Atom processor on Windows system and chose XVID to make video.There were performance problems with 1024x600 mode, but not with lesser resolution. – MBo Sep 23 '15 at 03:08