Created
January 18, 2021 04:50
-
-
Save shks/cfa2bc204284a7099e6634a6f350a996 to your computer and use it in GitHub Desktop.
split video into jpegs. ビデオをフレーム画像に分割する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import os | |
def save_all_frames(video_path, dir_path, basename, ext='jpg'): | |
cap = cv2.VideoCapture(video_path) | |
if not cap.isOpened(): | |
return | |
os.makedirs(dir_path, exist_ok=True) | |
base_path = os.path.join(dir_path, basename) | |
digit = len(str(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)))) | |
n = 0 | |
while True: | |
ret, frame = cap.read() | |
if ret: | |
print('frame num', n) | |
cv2.imwrite('{}_{}.{}'.format(base_path, str(n).zfill(digit), ext), frame) | |
n += 1 | |
else: | |
return | |
def save_frames(video_path, dir_path, basename, ext='jpg', maxFrame = 60): | |
cap = cv2.VideoCapture(video_path) | |
if not cap.isOpened(): | |
return | |
os.makedirs(dir_path, exist_ok=True) | |
base_path = os.path.join(dir_path, basename) | |
digit = len(str(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)))) | |
n = 0 | |
while True: | |
ret, frame = cap.read() | |
if ret: | |
if (n < maxFrame): | |
cv2.imwrite('{}_{}.{}'.format(base_path, str(n).zfill(digit), ext), frame) | |
n += 1 | |
else: | |
return | |
def save_part_frames(video_path, dir_path, basename, ext='jpg', start = 10, end = 70, orderFlip = False): | |
cap = cv2.VideoCapture(video_path) | |
if not cap.isOpened(): | |
return | |
os.makedirs(dir_path, exist_ok=True) | |
base_path = os.path.join(dir_path, basename) | |
digit = len(str(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)))) | |
n = 0 | |
while True: | |
ret, frame = cap.read() | |
if ret: | |
if (start <= n and n < end): | |
if(orderFlip): | |
cv2.imwrite('{}_{}.{}'.format(base_path, str(n-start).zfill(digit), ext), frame) | |
else: | |
_i = (end -start -1) - (n-start) | |
cv2.imwrite('{}_{}.{}'.format(base_path, str(_i).zfill(digit), ext), frame) | |
n += 1 | |
else: | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment