Skip to content

Instantly share code, notes, and snippets.

@git-hamza
Created June 13, 2023 12:50
Show Gist options
  • Save git-hamza/bd8d06600f44e6bfafe5e6fa0f9a8f58 to your computer and use it in GitHub Desktop.
Save git-hamza/bd8d06600f44e6bfafe5e6fa0f9a8f58 to your computer and use it in GitHub Desktop.
Get synchronize frames based on different FPS
video_1_path = "1.mp4"
video_2_path = "2.mp4"
paths = [video_1_path, video_2_path]
vid_caps = {idx+1: cv2.VideoCapture(path_) for idx, path_ in enumerate(paths)}
tracebar_video = 1
tracebar_frame = 15000
second_video = tracebar_video-1 if tracebar_video%2==0 else tracebar_video+1
print("second_video", second_video)
greater_cap = tracebar_video if vid_caps[tracebar_video].get(cv2.CAP_PROP_FPS) > vid_caps[second_video].get(cv2.CAP_PROP_FPS) else second_video
print("greater_cap" , greater_cap)
sum_flag = True if tracebar_video == greater_cap else False
print("sum_flag", sum_flag)
fps_diff = abs(vid_caps[tracebar_video].get(cv2.CAP_PROP_FPS) - vid_caps[second_video].get(cv2.CAP_PROP_FPS))
print("fps_diff", fps_diff)
tracebar_footer_frames = vid_caps[tracebar_video].get(cv2.CAP_PROP_FRAME_COUNT) - tracebar_frame
print("tracebar_footer_frames", tracebar_footer_frames)
frames_adjustment = ((tracebar_footer_frames / vid_caps[tracebar_video].get(cv2.CAP_PROP_FPS)) * fps_diff)
print("frames_adjustment", frames_adjustment)
second_video_frames = vid_caps[second_video].get(cv2.CAP_PROP_FRAME_COUNT) - tracebar_footer_frames
second_video_frames = (second_video_frames + frames_adjustment if sum_flag else second_video_frames - frames_adjustment)
print("second_video_frames", second_video_frames)
vid_caps[tracebar_video].set(cv2.CAP_PROP_POS_FRAMES, tracebar_frame)
vid_caps[second_video].set(cv2.CAP_PROP_POS_FRAMES, second_video_frames)
_, frame1 = vid_caps[tracebar_video].read()
_, frame2 = vid_caps[second_video].read()
cv2.imwrite("frame1.jpg", frame1)
cv2.imwrite("frame2.jpg", frame2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment