Video is collection of Frames . In this article we will know how to extract frames from video @x fps(Frame per second) using Python .
A frame is one of the many still images which compose the complete moving picture . When the moving picture is displayed, each frame is flashed on a screen
for a short time (nowadays, usually 1/24, 1/25 or 1/30 of a second) and
then immediately replaced by the next one
The frame is also sometimes used as a unit of time, so that a momentary
event might be said to last six frames, the actual duration of which
depends on the frame rate
of the system, which varies according to the video or film standard in
use. In North America and Japan, 30 frames per second (fps) is the
broadcast standard, with 24 frames/s now common in production for
high-definition video shot to look like film. In much of the rest of the
world, 25 frames/s is standard.
Below code is extracting frame from video @ 2 fps .
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 14 09:47:58 2019
@author: Jo
"""
#Doing with open cv2
#Import Cv2 library
import cv2,os
from moviepy.editor import VideoFileClip
inputfilepath="Dataset/vid1.mov"
#For saving output
if not os.path.exists('Frames'):
os.makedirs('Frames')
#location of video file
video=cv2.VideoCapture(inputfilepath)
#For Getting Clip duration
clip = VideoFileClip(inputfilepath)
print(clip.duration,"seconds")
def extractframe(sec):
# cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000) is responsible for
#skipping directly to the sec in the video (sec*1000th millisecond)
video.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
#reading frame
hasframes,image = video.read()
if hasframes:
#Write to location , increasing the count to avoid name conflict of images
#
cv2.imwrite("Frames/image"+str(count)+".jpg", image) # save frame as JPG file
return hasframes
#starting from 0th second
sec = 0
#Setting fps , here it will capture image in each 0.5 second , 2fps
frameRate = 0.5
count=1
#to check whether frame are their in video or not.
success = extractframe(sec)
while success:
#increasing counter to name conflick
count = count + 1
#setting sec
sec = sec + frameRate
sec = round(sec, 2)
print(sec)
success = extractframe(sec)
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 14 09:47:58 2019
@author: Jo
"""
#Doing with open cv2
#Import Cv2 library
import cv2,os
from moviepy.editor import VideoFileClip
inputfilepath="Dataset/vid1.mov"
#For saving output
if not os.path.exists('Frames'):
os.makedirs('Frames')
#location of video file
video=cv2.VideoCapture(inputfilepath)
#For Getting Clip duration
clip = VideoFileClip(inputfilepath)
print(clip.duration,"seconds")
def extractframe(sec):
# cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000) is responsible for
#skipping directly to the sec in the video (sec*1000th millisecond)
video.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
#reading frame
hasframes,image = video.read()
if hasframes:
#Write to location , increasing the count to avoid name conflict of images
#
cv2.imwrite("Frames/image"+str(count)+".jpg", image) # save frame as JPG file
return hasframes
#starting from 0th second
sec = 0
#Setting fps , here it will capture image in each 0.5 second , 2fps
frameRate = 0.5
count=1
#to check whether frame are their in video or not.
success = extractframe(sec)
while success:
#increasing counter to name conflick
count = count + 1
#setting sec
sec = sec + frameRate
sec = round(sec, 2)
print(sec)
success = extractframe(sec)
Code at Github
*********************************************
Next Article :
Frame Extraction From Multiple Videos @x fps (Frame Per Second) In Python
Next Article :
Frame Extraction From Multiple Videos @x fps (Frame Per Second) In Python






