In this article we will see how we can iterate frames of the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. A video is basically combination of lots of frame for each time there is a specific frame, in order to get the frame at given time we use get_frame method. Iterating frames means to traversing the frame a frame is represented in the form of numpy ndarray.
In order to do this we will use iter_frames method with the VideoFileClip object
Syntax : clip.iter_frames()
Argument : It takes no argument
Return : It returns Clip.iter_frames
Below is the implementation
# Import everything needed to edit video clips
from moviepy.editor import *
# loading video dsa gfg intro video
clip = VideoFileClip("dsa_geek.mp4")
# getting only first 5 seconds
clip = clip.subclip(0, 5)
# iterating frames
frames = clip.iter_frames()
# counter to count the frames
counter = 0
# using loop to transverse the frames
for value in frames:
# incrementing the counter
counter += 1
# printing the value of the counter
print("Counter Value ", end = " : ")
print(counter)
# showing clip
clip.ipython_display(width = 360)
Output :
Counter Value : 150
Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4
Another example
# Import everything needed to edit video clips
from moviepy.editor import *
# loading video gfg
clip = VideoFileClip("geeks.mp4")
# getting only first 5 seconds
clip = clip.subclip(0, 5)
# iterating frames
frames = clip.iter_frames()
# counter to count the frames
counter = 0
# using loop to transverse the frames
for value in frames:
# incrementing the counter
counter += 1
# printing the value of the counter
print("Counter Value ", end = " : ")
print(counter)
# showing clip
clip.ipython_display(width = 360)
Output :
Counter Value : 300
Moviepy - Building video __temp__.mp4.
MoviePy - Writing audio in __temp__TEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4