In this blog, I will show how to extract image (.png) from video recorded using iphone.
Preparation¶
I recorded a 10 minutes and 21 second video using my iphone 6s. In my current directory, I have IMG7367.MOV file with:
- Size: 639.3 MB
- Dimensions: 1280 × 720
You need cv2¶
If you do not have cv2, please make sure to pip install it as:
pip3 install opencv-python==3.2.0.6
Reference¶
The location of the MOV file from iphone.
In [60]:
path_to_MOV = "IMG_7367.MOV"
The location of the directory to save the file. If the folder does NOT exist, then create one.
In [61]:
import os
dir_jpgs = path_to_MOV[:-4]
try:
os.mkdir(dir_jpgs)
print("folder created")
except:
pass
As I recorded a 10 minutes and 21 second video using my iphone 6s, and the iphone 6s has 30fps, getting every frame will create about 18650 images, which is a bit too many.
In [62]:
VIDEO_MIN = 10 + 21.0/60
In [63]:
cap = cv2.VideoCapture(path_to_MOV)
FPS = cap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
print("FPS:{:4.2f}".format(FPS))
print("The maximum number of frames that can be extracted: {}".format(int(VIDEO_MIN *60* FPS)))
I will downsample and sample every 10 frame.
In [64]:
downsamplefactor = 10
print("Downsampling makes FPS: {:4.2f}".format(FPS/float(downsamplefactor)))
This will create roughly 1800 images of size 1280 x 720.
In [65]:
import numpy as np
import cv2
import time
print(cv2.__version__)
count, iframe = 0 , 0
success = 1
start = time.time()
while(success):
# Capture frame-by-frame
success, frame = cap.read()
# my original image was upside down. use flip to fix this.
frame = cv2.flip(frame,0)
# fix the mirror
frame = cv2.flip(frame,1)
if count % downsamplefactor == 0:
# Our operations on the frame come here
cv2.imwrite(dir_jpgs + '/frame{:05.0f}.jpg'.format(iframe),frame)
iframe += 1
count += 1
if count % 2000 == 0:
print(count)
cap.release()
end = time.time()
print("TIME TOOK {:4.2f} MIN".format((end-start)/60))