#This Python + OvenCV code adds a string to a video in Typewriter style. That #is 1 character at a time. The user can specifiy the start time and the speed #at which text should be added to the video. #------------------------------------------------------------------------------ import cv2, sys #Provide input video and name of the output video inVidName = 'in.mp4' outVidName = 'out.mp4' #Create a video capture object and read from input video file. If the input is #the camera, pass 0 instead of the video file name cap = cv2.VideoCapture(inVidName) #Check if camera opened sucessfully if (cap.isOpened() == False): print("Error Opening Video Stream of the File, exiting") sys.exit() #Specifiy start time after which text should get added tS = 0.5 #Specify duration in seconds between two characters to be added to the video tTxt = 0.25 #Spcify the string (text) to be added txt = "Rotating Wheel" #Specify the font colour in BGR format fc = (0, 0, 255) #Speify the font scale: this value scales base size of the text fs = 1 #Specify font type fo = cv2.FONT_HERSHEY_SIMPLEX #Specify font thickness ft = 1 #Specify width, height location factor: x,y value at which text start appearing xW = 0.10 xH = 0.80 # -----------------------No Futher Inputs from User is Needed------------------ #Get frame height and width of the video and calculate x,y coordinate of text FW = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) FH = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) x = int(FW * xW) y = int(FH * xH) #Get total number of frames in the video and duration of the video fps = int(cap.get(cv2.CAP_PROP_FPS)) nframe = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) duration = nframe/fps minutes = int(duration/60) seconds = duration % 60 #Check that width of the text does not exceed width of the video frame nT = len(txt) txtWidth, _ = cv2.getTextSize(txt, fo, fs, ft) if (x + txtWidth[0]) >= FW: print("\n All text cannot be printed in the width of the video, exiting") print("\n Width of the video: ", FW) print("\n Width of the text excluding start location: ", txtWidth[0]) print("\n Excess width in pixels: ", x + txtWidth[0] - FW, "\n") sys.exit() #Check if the time taken to add the video does not exceed video duration tTxtDura = nT * tTxt if (tS + tTxtDura) > duration: print("\n Text cannot be added before end of video, exiting") sys.exit() #Get frame numbers at which text should start to appear and stops appearing nS = int(tS * fps) nE = int(nS + nT * (fps * tTxt)) #Create a video writter object and start the loop to add 1 character at a time codec = cv2.VideoWriter_fourcc(*'mp4v') video = cv2.VideoWriter(outVidName, codec, fps, (FW, FH)) #Start the counter for frames in the video i = 1 #Start the counter for number of characters in the text j = 0 #Read the video till last frame, add text based on specified conditions while (cap.isOpened() == True): #Capture each frame of the video ret, frm = cap.read() if (ret == True): #Write the frame as it is if falls before start of text addition if (i < nS): video.write(frm) #Write the text after start time of the text addition if (i > nS and i < nE): #Calculate number of frames between two characters m = int((i - nS) / (fps * tTxt)) j = m if (j <= nT): for k in range(j+1): cv2.putText(frm,txt[0:k],(x,y),fo,fs,fc,ft) j = j + 1 video.write(frm) #Write the entire text once all text added with Typewritter Effect if (i > nE): cv2.putText(frm,txt[0:nT],(x,y),fo,fs,fc,ft,cv2.LINE_4) video.write(frm) i = i + 1 #Break the loop once end of video is reached else: break #Release the video capture and writer objects and close all frames cap.release() video.release() cv2.destroyAllWindows()