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
tis a time, returns true if t is between the start and the end of the clip.tcan be expressed in seconds (15.35), in (min, sec), in (hour, min, sec), or as a string: ‘01:03:05.35’. Iftis a numpy array, returns False if none of thetis 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
fpsattribute.with_times (bool, optional) – Ff
Trueyield tuples of(t, frame)wheretis the current time for the frame, otherwise only aframeobject.logger (str, optional) – Either
"bar"for progress bar orNoneor 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_timeandend_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
maskandaudioof the resulting subclip will be subclips ofmaskandaudiothe 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_timeis 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
tbeing 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
transformshould also be applied to the audio or the mask of the clip, if any.keep_duration (bool, optional) –
False(default) if the transformation modifies thedurationof 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
gfwill represent the current clip’sget_framemethod, i.e.gfis 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
durationof the clip.
Examples
In the following
new_clipa 100 pixels-high clip whose video content scrolls from the top to the bottom of the frames ofclipat 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
durationattribute 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, theendattribute 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
endattribute 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
endattribute 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
fpsattribute 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_maskattribute 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_timeandend_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
durationattribute set, the duration of the returned clip is automatically computed as `` duration - (end_time - start_time)``.The resulting clip’s
audioandmaskwill 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
startattribute 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
audioandmaskclips 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
startattribute value for the clip.change_end (bool optional) – Indicates if the
endattribute value must be changed accordingly, if possible. Ifchange_end=Trueand the clip has adurationattribute, theendattribute of the clip will be updated tostart + duration. Ifchange_end=Falseand the clip has aendattribute, thedurationattribute of the clip will be updated toend - start.