LoginSignup
3
4

More than 3 years have passed since last update.

Pythonで動画の幅、高さ、fpsを取得、そこから1秒ごと(任意の間隔)に画像を生成する方法

Last updated at Posted at 2019-10-19

動画の各種情報を取得7

import cv2

video_path = '動画のパス'
cap = cv2.VideoCapture(video_path)

width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)

高さ

height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

総フレーム数

count = cap.get(cv2.CAP_PROP_FRAME_COUNT)

fps

fps = cap.get(cv2.CAP_PROP_FPS)

print("width:{}, height:{}, count:{}, fps:{}".format(width,height,count,fps))

1秒ごとに画像を生成

import cv2

video_path = "動画のパス"
cap = cv2.VideoCapture(video_path)

count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = cap.get(cv2.CAP_PROP_FPS)

print("width:{}, height:{}, count:{}, fps:{}".format(width,height,count,fps))

for num in range(1, int(count), int(fps)):
cap.set(cv2.CAP_PROP_POS_FRAMES, num)
cv2.imwrite("picture{:0=3}".format(int((num-1)/int(fps)))+".jpg", cap.read()[1])
print("save picture{:0=3}".format(int((num-1)/int(fps)))+".jpg")

cap.release()

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4