#------------------------------------------------------------------------------ #Source code at: github.com/ntshvicky/VideoTimer/blob/master/app.py #Adapted with adding variable to change location of the text and font colour #Days and Hours options deleted - that means script is useful for videos having #duration less than an hour. #------------------------------------------------------------------------------ # importing libraries import cv2 # Create a VideoCapture object and read from input file cap = cv2.VideoCapture('in.mp4') #Specifiy font scale and font-colour in RGB fs = 0.5 fc = (0, 0, 0) # Check if camera opened successfully if (cap.isOpened()== False): print("Error opening video file") (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') fps = 0 if int(major_ver) < 3 : fps = cap.get(cv2.cv.CV_CAP_PROP_FPS) print("FPS using video.get(cv2.cv.CV_CAP_PROP_FPS): {:.2f}".format(fps)) else : fps = cap.get(cv2.CAP_PROP_FPS) print("FPS using video.get(cv2.CAP_PROP_FPS) : {:.2f}".format(fps)) mins = 0 sec = 0 period = '00:00' #Get default resolution of the frame, convert resolution from float to integer frm_w = int(cap.get(3)) frm_h = int(cap.get(4)) x = int(4*frm_w/5) y = int(frm_h-10) # Define the codec and create VideoWriter object, frame size is specified out = cv2.VideoWriter('output.mp4',cv2.VideoWriter_fourcc('m','p','4','v'), fps,(frm_w,frm_h)) font = cv2.FONT_HERSHEY_SIMPLEX #Get size of the written text #txt_w, txt_h = cv2.getTextSize(text_description, font, fs, lineThickness)[0] # Read until video is completed while(cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() if ret == True: cfn = cap.get(1) #cap.get(cv2.CAP_PROP_FRAME_COUNT) if int(cfn)%int(fps)==0: if sec > 59: sec = 0 mins = mins+1 period = "{:02d}:{:02d}".format(mins,sec) sec = sec + 1 cv2.putText(frame,period,(x,y), font, fs,fc,2,cv2.LINE_AA) out.write(frame) # Display the resulting frame cv2.imshow('Frame', frame) # Press Q on keyboard to exit if cv2.waitKey(25) & 0xFF == ord('q'): break # Break the loop else: break # When everything done, release the video capture object cap.release() out.release() # Closes all the frames cv2.destroyAllWindows()