Created
May 17, 2019 22:37
-
-
Save kenjiSpecial/08473149c4dd69934cc4827e0903bb54 to your computer and use it in GitHub Desktop.
covert mp4 to jpeg
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 | |
print(cv2.__version__) | |
videoName = "n0" | |
cam = cv2.VideoCapture( | |
"C:\\freelance\\small\\nisshin\\Mp4ToJpeg\\videos\\" + videoName + ".mp4") | |
print('hello') | |
try: | |
# creating a folder named data | |
if not os.path.exists('images'): | |
os.makedirs('images') | |
# if not created then raise error | |
except OSError: | |
print('Error: Creating directory of data') | |
if cam.isOpened(): | |
# get cam property | |
width = cam.get(cv2.CAP_PROP_FRAME_WIDTH) # float | |
height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT) # float | |
print(width, height) | |
currentframe = 0 | |
seqNum = 0 | |
cam.set(cv2.CAP_PROP_FRAME_WIDTH, width/2) | |
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, height/2) | |
while(True): | |
# reading from frame | |
ret, frame = cam.read() | |
if ret: | |
# if video is still left continue creating images | |
if currentframe % 30 == 0: | |
name = './images/' + videoName + \ | |
'-seq' + str(seqNum) + '.jpg' | |
# print('Creating...' + name) | |
# writing the extracted images | |
newFrame = cv2.resize(frame, (int(width/2), int(height/2))) | |
cv2.imwrite(name, newFrame) | |
seqNum = seqNum + 1 | |
# increasing counter so that it will | |
# show how many frames are created | |
currentframe += 1 | |
else: | |
break | |
# Release all space and windows once done | |
cam.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment