Last OpenCV update (3.3.1) makes my videos faster
I have the following code, which just makes videos from a camera.
The user writes for example "python record_video.py 20" where that 20 is the length in seconds. In 3.3.0 the following script works fine, in 3.3.1 it makes 33% faster videos.
def random_filename(size):
''' to create random names for the dataset pictures '''
w = ''.join(random.choice(string.ascii_lowercase) for i in range(size))
return w+'.avi'
def kill_video(event, time_to_wait):
'''Waits for time_wait time.
Then sets and event so that the timeout is marked and the video recording
stops'''
time.sleep(time_to_wait+1) # cam always takes around 1s to warm up
event.set()
if __name__ == "__main__":
if len(sys.argv) > 1:
user_time = float(sys.argv[1])
else:
user_time = 0
vc = cv2.VideoCapture(0)
vc.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
vc.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
cv2.namedWindow("video")
fourcc = cv2.VideoWriter_fourcc(*'XVID')
random_name = random_filename(5)
out = cv2.VideoWriter(random_name,fourcc, 30.0, (800,600))
event = threading.Event()
if user_time > 0:
timer = threading.Thread(target=kill_video, args=(event,user_time))
timer.start()
while(1):
start_time = time.clock() * 1000 # seconds to ms
ret,frame = vc.read()
out.write(frame)
cv2.imshow("video",frame)
end_time = time.clock() * 1000
wait_time = 33333 - (end_time - start_time)*1000
key = cv2.waitKey(int(wait_time/1000))
if key == ord('q') or event.is_set():
break
vc.release()
out.release()
cv2.destroyAllWindows()
As said, with the current version 3.3.1 it makes 33% videos faster, but I just downgraded to 3.3.0 and it makes videos the right time length.
Are you talking about the time the videos take to display? Or the actual length they play when you open the output file?
This code appears to have a constant framerate for the output file, which means it should be the same length no matter what version or what number you give it.
And of course, OpenCV is not meant for displaying videos. So you can't trust imshow and waitKey to show at a constant rate. It's simply not built for it.
I have seen code from stackoverflow
@Tetragramm If I produce a video with that script in 3.3.0, and then I play it with VLC, it will be 20 seconds (as expected). If I produce a video with the same script, but in 3.3.1, then play it in VLC the video is 13 seconds, and plays 33% faster.
Well that's weird. Can you collect some debugging information? In the script, print out how many frames are written. In VLC, can you look in the codec information and get the Frame rate of the video? For both versions of OpenCV please.
Thanks.
@Tetragramm Always aiming for a video of 20 seconds. Using 3.3.0 it writes 614 frames. In VLC it produces a video of 20 seconds and 30FPS. Using 3.3.1 it writes 413 frames. In VLC it produces a video of 13s and 30FPS.
This is for exactly the same code, but using 3.3.0 or 3.3.1. In the 3.3.1 video the action of what you see happens a 33% faster.