Skip to content

Instantly share code, notes, and snippets.

@arpruss
Last active December 14, 2022 15:56
Show Gist options
  • Select an option

  • Save arpruss/3a705c859d4ea4a482c4e47e96a08f79 to your computer and use it in GitHub Desktop.

Select an option

Save arpruss/3a705c859d4ea4a482c4e47e96a08f79 to your computer and use it in GitHub Desktop.

Revisions

  1. arpruss revised this gist Dec 14, 2022. 1 changed file with 53 additions and 14 deletions.
    67 changes: 53 additions & 14 deletions timer.py
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@
    import math

    fontScale = 10
    margin = .1
    fontThickness = 2
    margin = .2
    fontThickness = 3
    backColor = (0,0,0)
    textColor = (255,255,255)
    FPS = 59.94
    @@ -13,7 +13,33 @@
    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)
    @@ -24,7 +50,8 @@ def getText(n):
    text = "%02d:%02d" % (sec//60,sec%60)
    if decimalPoints:
    text += ".%0*d" % (decimalPoints, frac)
    return text
    msg = getMessage(t)
    return text,msg

    def getSize(text):
    return cv2.getTextSize(text, font, fontScale, fontThickness)[0]
    @@ -34,36 +61,48 @@ def getSize(text):
    maxWidth = 0
    maxHeight = 0
    prevText = None
    prevMsg = None

    for n in range(numFrames):
    text = getText(n)
    text,msg = getText(n)
    if text != prevText:
    size = getSize(text)
    maxWidth = max(maxWidth,size[0])
    maxHeight = max(maxHeight,size[1])
    prevText = text

    m = int(2*margin*maxHeight)
    width = maxWidth + m
    height = maxHeight + m
    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 = getText(n)
    if text != prevText:
    print(text)
    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),int((height+size[1])/2)), font, fontScale, textColor, fontThickness, lineType = cv2.LINE_AA)
    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)



  2. arpruss revised this gist Dec 4, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion timer.py
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ def getSize(text):

    print("dimensions:",width,height)

    out = cv2.VideoWriter('timer.avi',cv2.VideoWriter_fourcc(*'DIVX'), FPS, (width,height))
    out = cv2.VideoWriter('timer.mp4',cv2.VideoWriter_fourcc(*'mp4v'), FPS, (width,height))

    print("Generating video")

  3. arpruss created this gist Dec 4, 2022.
    69 changes: 69 additions & 0 deletions timer.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    import cv2
    import numpy as np
    import math

    fontScale = 10
    margin = .1
    fontThickness = 2
    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)

    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)
    return text

    def getSize(text):
    return cv2.getTextSize(text, font, fontScale, fontThickness)[0]

    print("Getting dimensions")

    maxWidth = 0
    maxHeight = 0
    prevText = None

    for n in range(numFrames):
    text = getText(n)
    if text != prevText:
    size = getSize(text)
    maxWidth = max(maxWidth,size[0])
    maxHeight = max(maxHeight,size[1])
    prevText = text

    m = int(2*margin*maxHeight)
    width = maxWidth + m
    height = maxHeight + m

    print("dimensions:",width,height)

    out = cv2.VideoWriter('timer.avi',cv2.VideoWriter_fourcc(*'DIVX'), FPS, (width,height))

    print("Generating video")

    prevText = None

    for n in range(numFrames):
    text = getText(n)
    if text != prevText:
    print(text)
    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),int((height+size[1])/2)), font, fontScale, textColor, fontThickness, lineType = cv2.LINE_AA)
    prevText = text
    out.write(img)