Skip to content

Instantly share code, notes, and snippets.

@Shaked
Created March 17, 2020 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shaked/4ab8f26ca1565bb6ef2f86475dacd979 to your computer and use it in GitHub Desktop.
Save Shaked/4ab8f26ca1565bb6ef2f86475dacd979 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import cv2
import time
import skvideo.io
import time
#https://stackoverflow.com/questions/49138457/what-is-the-opposite-of-cv2-videowriter-fourcc
def decode_fourcc(cc):
return "".join([chr((int(cc) >> 8 * i) & 0xFF) for i in range(4)])
def draw_label(img, text, pos, bg_color):
font_face = cv2.FONT_HERSHEY_SIMPLEX
scale = 1.4
color = (255, 128, 0)
thickness = cv2.FILLED
margin = 12
txt_size = cv2.getTextSize(text, font_face, scale, thickness)
end_x = pos[0] + txt_size[0][0] + margin
end_y = pos[1] - txt_size[0][1] - margin
img_copy = img.copy()
#cv2.rectangle(img, pos, (end_x, end_y), bg_color, thickness)
cv2.putText(img, text, pos, font_face, scale, color, 1, cv2.LINE_AA)
if __name__ == '__main__' :
# Start default camera
video = cv2.VideoCapture("test.mov");
print("CV_CAP_PROP_FORMAT: " + str(video.get(cv2.CAP_PROP_FORMAT)))
print("CV_CAP_PROP_MODE: " + str(video.get(cv2.CAP_PROP_MODE)))
print("CV_CAP_PROP_FPS: " + str(video.get(cv2.CAP_PROP_FPS)))
print("CV_CAP_PROP_CONTRAST: " + str(video.get(cv2.CAP_PROP_CONTRAST)))
print("CV_CAP_PROP_GAIN: " + str(video.get(cv2.CAP_PROP_GAIN)))
print("CV_CAP_PROP_FRAME_WIDTH: " + str(video.get(cv2.CAP_PROP_FRAME_WIDTH)))
print("CV_CAP_PROP_FRAME_HEIGHT: " + str(int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))))
print("CV_CAP_PROP_POS_FRAMES: " + str(video.get(cv2.CAP_PROP_POS_FRAMES)))
print("CV_CAP_PROP_EXPOSURE: " + str(video.get(cv2.CAP_PROP_EXPOSURE)))
print("CAP_PROP_SETTINGS: " + str(video.get(cv2.CAP_PROP_SETTINGS)))
print("CAP_PROP_FOURCC: " + str(video.get(cv2.CAP_PROP_FOURCC)))
print("3 {}: 4 {}: ".format(str(video.get(3)), str(video.get(4))))
# Find OpenCV version
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
# With webvideo.get(CV_CAP_PROP_FPS) does not work.
# Let's see for ourselves.
fps = video.get(cv2.CAP_PROP_FPS)
print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
# Number of frames to capture
num_frames = 120;
print("Capturing {0} frames".format(num_frames))
# Start time
start = time.time()
# Grab a few frames
for i in range(num_frames):
ret, frame = video.read()
# End time
end = time.time()
# Time elapsed
seconds = end - start
print("Time taken : {0} seconds".format(seconds))
# Calculate frames per second
# fps = num_frames / seconds;
# print("Estimated frames per second : {0}".format(fps))
metadata = skvideo.io.ffprobe("video.mp4")
width = video.get(3)
height = video.get(4)
if (len(metadata.keys()) == 3):
vertical = 1
else:
width = video.get(4)
height = video.get(3)
vertical = 0 # if the video is in landscape, metadata.keys() will have a length of 2
savepath = 'test-new.mov'
print("is vertical {}".format(vertical))
#fourcc = cv2.CV_FOURCC('H264')
#fourcc = cv2.VideoWriter_fourcc(*'XVID')
original_fourcc = video.get(cv2.CAP_PROP_FOURCC)
codec = decode_fourcc(original_fourcc)
fourcc = cv2.VideoWriter_fourcc(*codec)
out = cv2.VideoWriter(savepath, fourcc, video.get(cv2.CAP_PROP_FPS), (int(width),int(height)), 1)
ret = True
fps = video.get(cv2.CAP_PROP_FPS)
# 25.15388919977616
fps_in_milliseconds = fps * 1000
# 1000*1000*25.153 ~= 25,153,000
fps_in_nanoseconds = fps_in_milliseconds * 1000
start = counter = 1000000 / fps
countup_in_seconds = 0
#for i in range(num_frames):
while ret:
ret, frame = video.read()
if ret == True:
if vertical == 0:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
text = time.strftime('{}'.format(counter))
draw_label(frame, '{}'.format(text), (int(width)-290,int(height)-90), (255,0,255, 1))
out.write(frame)
counter += start
# Release video
video.release()
out.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment