Last active
December 14, 2022 15:56
-
-
Save arpruss/3a705c859d4ea4a482c4e47e96a08f79 to your computer and use it in GitHub Desktop.
Create video of a timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import math | |
fontScale = 10 | |
margin = .2 | |
fontThickness = 3 | |
backColor = (0,0,0) | |
textColor = (255,255,255) | |
FPS = 59.94 | |
startTime = -9 | |
endTime = 61*60 | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
decimalPoints = 1 | |
numFrames = math.floor((endTime-startTime)*FPS) | |
countKey = "b" | |
messages = [] | |
def parseTime(s0): | |
parts=s0.split(":") | |
if len(parts)>2: | |
return 3600*int(parts[0])+60*int(parts[1])+float(parts[2]) | |
elif len(parts)==2: | |
return 60*int(parts[0])+float(parts[1]) | |
else: | |
return float(parts[0]) | |
with open("counts.txt") as f: | |
for line in f: | |
counters = line.strip().split(" ",2) | |
if len(counters)>=2 and counters[1] == countKey: | |
t = parseTime(counters[0]) | |
messages.append((t, counters[2] if len(counters)>=3 else "")) | |
def getMessage(t1): | |
msg = "" | |
for t,m in messages: | |
if t <= t1: | |
msg = m | |
return msg | |
def getText(n): | |
t = n / FPS + startTime | |
sec = math.floor(t) | |
frac = math.floor((t-sec) * 10**decimalPoints) | |
if t<0: | |
text = str(sec) | |
else: | |
text = "%02d:%02d" % (sec//60,sec%60) | |
if decimalPoints: | |
text += ".%0*d" % (decimalPoints, frac) | |
msg = getMessage(t) | |
return text,msg | |
def getSize(text): | |
return cv2.getTextSize(text, font, fontScale, fontThickness)[0] | |
print("Getting dimensions") | |
maxWidth = 0 | |
maxHeight = 0 | |
prevText = None | |
prevMsg = None | |
for n in range(numFrames): | |
text,msg = getText(n) | |
if text != prevText: | |
size = getSize(text) | |
maxWidth = max(maxWidth,size[0]) | |
maxHeight = max(maxHeight,size[1]) | |
prevText = text | |
if msg != prevMsg: | |
size = getSize(msg) | |
maxWidth = max(maxWidth,size[0]) | |
maxHeight = max(maxHeight,size[1]) | |
prevMsg = msg | |
lineHeight = maxHeight | |
m = int(margin*lineHeight) | |
width = maxWidth + 2*m | |
height = 2*lineHeight + 3*m | |
line1Y = m+lineHeight | |
line2Y = 2*m+2*lineHeight | |
print("dimensions:",width,height) | |
out = cv2.VideoWriter('timer.mp4',cv2.VideoWriter_fourcc(*'mp4v'), FPS, (width,height)) | |
print("Generating video") | |
prevText = None | |
prevMsg = None | |
for n in range(numFrames): | |
text,msg = getText(n) | |
if text != prevText or msg != prevMsg: | |
print(text,msg) | |
img = np.zeros((height,width,3),dtype=np.uint8) | |
img[:] = tuple(reversed(backColor)) | |
size = getSize(text) | |
cv2.putText(img, text, (int((width-size[0])/2),line1Y), font, fontScale, textColor, fontThickness, lineType = cv2.LINE_AA) | |
size = getSize(msg) | |
cv2.putText(img, msg, (int((width-size[0])/2),line2Y), font, fontScale, textColor, fontThickness, lineType = cv2.LINE_AA) | |
prevText = text | |
prevMsg = msg | |
out.write(img) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment