OpenCV frame writing too slow
OpenCV writing video at 2x speed at 60 fps and writing correct speed at 30 fps
VideoWriter.write() very slow - Python - OpenCV
cv::VideoWriter does not support recording with variable frame rate
So, I was basically creating a script when gets the screenshots into numpy array inside while loop and adds those to cv.VideoWriter() this is kind of weird that when I set the fps to 60 it gave 2x speed in the resultant video and when it was 120 fps it gave 4x speed but it have correct speed at 30 fps. Did anyone understand why this might be happening ? my monitor has 59 Hz refresh rate btw pretty basic dell monitor.
I resolved my issue after a bit of debugging; it was an issue with VideoWriter being picky on the rate at which frames were fed to it.
You need to sync your read and write functions. Your code reads as fast as possible, and writes also as fast as possible. Your output video probably looks slow because writing the output happens faster than reading the input (since capture >> is waiting for your camera), and several identical frames are recorded.
Writing without waiting or syncing means you may write the same content several times (what I think is happening here), or lose frames.
If you want to keep having threads, you may, but you will need to make your write process wait until there is something new to write.
Likewise to avoid losing frames or writing corrupted frames, you need the read process to wait until writing is done, so that frame can be safely overwritten.
Since the threads need to wait for each other anyway, there's little point in threads at all.
I'd rather recommend this much simpler way:
for (;;) {
capture >> frame;
process(frame); // whatever smart you need
record << frame;
}
If you need parallelism, you'll need much more complex sync mechanism, and maybe some kind of fifo for your frames.