moviepy.Clip.Clip#
- class moviepy.Clip.Clip[source]#
Base class of all clips (VideoClips and AudioClips).
- start#
When the clip is included in a composition, time of the composition at which the clip starts playing (in seconds).
- Type:
float
- end#
When the clip is included in a composition, time of the composition at which the clip stops playing (in seconds).
- Type:
float
- duration#
Duration of the clip (in seconds). Some clips are infinite, in this case their duration will be
None
.- Type:
float
- get_frame(t)[source]#
Gets a numpy array representing the RGB picture of the clip, or (mono or stereo) value for a sound clip, at time
t
.- Parameters:
t (float or tuple or str) – Moment of the clip whose frame will be returned.
- is_playing(t)[source]#
If
t
is a time, returns true if t is between the start and the end of the clip.t
can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’. Ift
is a numpy array, returns False if none of thet
is in the clip, else returns a vector [b_1, b_2, b_3…] where b_i is true if tti is in the clip.
- iter_frames(fps=None, with_times=False, logger=None, dtype=None)[source]#
Iterates over all the frames of the clip.
Returns each frame of the clip as a HxWxN Numpy array, where N=1 for mask clips and N=3 for RGB clips.
This function is not really meant for video editing. It provides an easy way to do frame-by-frame treatment of a video, for fields like science, computer vision…
- Parameters:
fps (int, optional) – Frames per second for clip iteration. Is optional if the clip already has a
fps
attribute.with_times (bool, optional) – Ff
True
yield tuples of(t, frame)
wheret
is the current time for the frame, otherwise only aframe
object.logger (str, optional) – Either
"bar"
for progress bar orNone
or any Proglog logger.dtype (type, optional) – Type to cast Numpy array frames. Use
dtype="uint8"
when using the pictures to write video, images..
Examples
# prints the maximum of red that is contained # on the first line of each frame of the clip. from moviepy import VideoFileClip myclip = VideoFileClip('myvideo.mp4') print([frame[0,:,0].max() for frame in myclip.iter_frames()])
- subclipped(start_time=0, end_time=None)[source]#
Returns a clip playing the content of the current clip between times
start_time
andend_time
, which can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’.The
mask
andaudio
of the resulting subclip will be subclips ofmask
andaudio
the original clip, if they exist.It’s equivalent to slice the clip as a sequence, like
clip[t_start:t_end]
.- Parameters:
start_time (float or tuple or str, optional) – Moment that will be chosen as the beginning of the produced clip. If is negative, it is reset to
clip.duration + start_time
.end_time (float or tuple or str, optional) –
Moment that will be chosen as the end of the produced clip. If not provided, it is assumed to be the duration of the clip (potentially infinite). If is negative, it is reset to
clip.duration + end_time
. For instance:>>> # cut the last two seconds of the clip: >>> new_clip = clip.subclipped(0, -2)
If
end_time
is provided or if the clip has a duration attribute, the duration of the returned clip is set automatically.
- time_transform(time_func, apply_to=None, keep_duration=False)[source]#
Returns a Clip instance playing the content of the current clip but with a modified timeline, time
t
being replaced by the return of time_func(t).- Parameters:
time_func (function) – A function
t -> new_t
.apply_to ({"mask", "audio", ["mask", "audio"]}, optional) – Can be either ‘mask’, or ‘audio’, or [‘mask’,’audio’]. Specifies if the filter
transform
should also be applied to the audio or the mask of the clip, if any.keep_duration (bool, optional) –
False
(default) if the transformation modifies theduration
of the clip.
Examples
# plays the clip (and its mask and sound) twice faster new_clip = clip.time_transform(lambda t: 2*t, apply_to=['mask', 'audio']) # plays the clip starting at t=3, and backwards: new_clip = clip.time_transform(lambda t: 3-t)
- transform(func, apply_to=None, keep_duration=True)[source]#
General processing of a clip.
Returns a new Clip whose frames are a transformation (through function
func
) of the frames of the current clip.- Parameters:
func (function) – A function with signature (gf,t -> frame) where
gf
will represent the current clip’sget_frame
method, i.e.gf
is a function (t->image). Parameter t is a time in seconds, frame is a picture (=Numpy array) which will be returned by the transformed clip (see examples below).apply_to ({"mask", "audio", ["mask", "audio"]}, optional) – Can be either
'mask'
, or'audio'
, or['mask','audio']
. Specifies if the filter should also be applied to the audio or the mask of the clip, if any.keep_duration (bool, optional) – Set to True if the transformation does not change the
duration
of the clip.
Examples
In the following
new_clip
a 100 pixels-high clip whose video content scrolls from the top to the bottom of the frames ofclip
at 50 pixels per second.>>> filter = lambda get_frame,t : get_frame(t)[int(t):int(t)+50, :] >>> new_clip = clip.transform(filter, apply_to='mask')
- with_duration(duration, change_end=True)[source]#
Returns a copy of the clip, with the
duration
attribute set tot
, which can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’. Also sets the duration of the mask and audio, if any, of the returned clip.If
change_end is False
, the start attribute of the clip will be modified in function of the duration and the preset end of the clip.- Parameters:
duration (float) – New duration attribute value for the clip.
change_end (bool, optional) – If
True
, theend
attribute value of the clip will be adjusted accordingly to the new duration usingclip.start + duration
.
- with_effects(effects: List[Effect])[source]#
Return a copy of the current clip with the effects applied
>>> new_clip = clip.with_effects([vfx.Resize(0.2, method="bilinear")])
You can also pass multiple effect as a list
>>> clip.with_effects([afx.VolumeX(0.5), vfx.Resize(0.3), vfx.Mirrorx()])
- with_end(t)[source]#
Returns a copy of the clip, with the
end
attribute set tot
, which can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’. Also sets the duration of the mask and audio, if any, of the returned clip.- note::
The start and end attribute of a clip define when a clip will start playing when used in a composite video clip, not the start time of the clip itself.
i.e: with_start(10) mean the clip will still start at his first frame, but if used in a composite video clip it will only start to show at 10 seconds.
- Parameters:
t (float or tuple or str) – New
end
attribute value for the clip.
- with_fps(fps, change_duration=False)[source]#
Returns a copy of the clip with a new default fps for functions like write_videofile, iterframe, etc.
- Parameters:
fps (int) – New
fps
attribute value for the clip.change_duration (bool, optional) – If
change_duration=True
, then the video speed will change to match the new fps (conserving all frames 1:1). For example, if the fps is halved in this mode, the duration will be doubled.
- with_is_mask(is_mask)[source]#
Says whether the clip is a mask or not.
- Parameters:
is_mask (bool) – New
is_mask
attribute value for the clip.
- with_memoize(memoize)[source]#
Sets whether the clip should keep the last frame read in memory.
- Parameters:
memoize (bool) – Indicates if the clip should keep the last frame read in memory.
- with_section_cut_out(start_time, end_time)[source]#
Returns a clip playing the content of the current clip but skips the extract between
start_time
andend_time
, which can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’.If the original clip has a
duration
attribute set, the duration of the returned clip is automatically computed as `` duration - (end_time - start_time)``.The resulting clip’s
audio
andmask
will also be cutout if they exist.- Parameters:
start_time (float or tuple or str) – Moment from which frames will be ignored in the resulting output.
end_time (float or tuple or str) – Moment until which frames will be ignored in the resulting output.
- with_speed_scaled(factor: float = None, final_duration: float = None)[source]#
Returns a clip playing the current clip but at a speed multiplied by
factor
. For info on the parameters, please seevfx.MultiplySpeed
.
- with_start(t, change_end=True)[source]#
Returns a copy of the clip, with the
start
attribute set tot
, which can be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’.These changes are also applied to the
audio
andmask
clips of the current clip, if they exist.- note::
The start and end attribute of a clip define when a clip will start playing when used in a composite video clip, not the start time of the clip itself.
i.e: with_start(10) mean the clip will still start at his first frame, but if used in a composite video clip it will only start to show at 10 seconds.
- Parameters:
t (float or tuple or str) – New
start
attribute value for the clip.change_end (bool optional) – Indicates if the
end
attribute value must be changed accordingly, if possible. Ifchange_end=True
and the clip has aduration
attribute, theend
attribute of the clip will be updated tostart + duration
. Ifchange_end=False
and the clip has aend
attribute, theduration
attribute of the clip will be updated toend - start
.