[PYTHON] Cut out JPEG for time lapse from Theta S video

Purpose

JPEG is saved for each specified frame from the video taken with Theta S. (It's okay to take a video, but it's quite difficult to see everything in real time, so I'm making a time-lapse video as a shortened version.)

In addition, if you time-lapse the jpeg cut out below with Theta + App, it will be a video that can be muzzled. The Theta + app seems to be able to create time-lapse videos only up to 300 JPEGs, so if you have more than that, Lapse It Time Lapse Pro App .ui.LapseItPro).

Reference site

I don't have the skill to write from Ichi, so I borrowed from various places ^^; Thank you.

  1. Install OpenCV3.0 in Python3.0 environment http://retrofocus28.blogspot.jp/2015/08/python3-opencv3windows.html
  2. Read video files with OpenCV3.0 http://derivecv.tumblr.com/post/73561473978

How to use

python mp42jpg.py MP4FileName [FrameInterval] FrameInterval specifies how many frames to save in JPEG. The default is 120 (15fps x 8 seconds every 8 seconds because the ThetaS video is about 15fps) every frame.

Source

mp42jpg.py


import cv2
import os.path
import sys

interval = 120  #about 15fps*8sec

if len(sys.argv) >= 2 and len(sys.argv) <= 3:
    filename = sys.argv[1]
    if len(sys.argv)==3:
        interval = int(sys.argv[2] )
else:
    print('Usage: python %s mp4_filename [frame_interval]' % sys.argv[0])
    quit()

cap = cv2.VideoCapture(filename)
max_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
file_basename, ext = os.path.splitext(filename)
ret = cap.grab()

while(ret):
    frame_count = cap.get(cv2.CAP_PROP_POS_FRAMES)
#    ret, frame = cap.read()
    ret, frame = cap.retrieve()
    
    if ret :

        #cv2.imshow('frame',frame)
        print("frame count:%d" % frame_count)
        cv2.imwrite('%s_%d.jpg' % (file_basename,frame_count),frame)
        if (frame_count + interval ) > max_frames:
            break;
        cap.set(cv2.CAP_PROP_POS_FRAMES,frame_count+interval)
        
#        if cv2.waitKey(1) & 0xFF == ord('q'):
#            break

cap.release()
#cv2.destroyAllWindows()

If you execute imshow with the above comment, it will be displayed in Window.

Recommended Posts

Cut out JPEG for time lapse from Theta S video
Cut out frames from video by 1 second with Python + OpenCV
I tried to cut out a still image from the video