Clip¶
Clip
¶
-
class
moviepy.Clip.
Clip
[source]¶ Bases:
object
Base class of all clips (VideoClips and AudioClips).
- Attributes:
- startfloat
When the clip is included in a composition, time of the composition at which the clip starts playing (in seconds).
- endfloat
When the clip is included in a composition, time of the composition at which the clip stops playing (in seconds).
- durationfloat
Duration of the clip (in seconds). Some clips are infinite, in this case their duration will be
None
.
-
cutout
(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_timefloat or tuple or str
Moment from which frames will be ignored in the resulting output.
- end_timefloat or tuple or str
Moment until which frames will be ignored in the resulting output.
-
fx
(func, *args, **kwargs)[source]¶ Returns the result of
func(self, *args, **kwargs)
, for instance>>> new_clip = clip.fx(resize, 0.2, method="bilinear")
is equivalent to
>>> new_clip = resize(clip, 0.2, method="bilinear")
The motivation of fx is to keep the name of the effect near its parameters when the effects are chained:
>>> from moviepy.video.fx import multiply_volume, resize, mirrorx >>> clip.fx(multiply_volume, 0.5).fx(resize, 0.3).fx(mirrorx) >>> # Is equivalent, but clearer than >>> mirrorx(resize(multiply_volume(clip, 0.5), 0.3))
-
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:
- tfloat 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:
- fpsint, optional
Frames per second for clip iteration. Is optional if the clip already has a
fps
attribute.- with_timesbool, optional
Ff
True
yield tuples of(t, frame)
wheret
is the current time for the frame, otherwise only aframe
object.- loggerstr, optional
Either
"bar"
for progress bar orNone
or any Proglog logger.- dtypetype, 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()])
-
subclip
(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.- Parameters:
- start_timefloat 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_timefloat 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.subclip(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 another time time_func(t).- Parameters:
- time_funcfunction
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_durationbool, 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:
- funcfunction
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_durationbool, 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:
- durationfloat
New duration attribute value for the clip.
- change_endbool, optional
If
True
, theend
attribute value of the clip will be adjusted accordingly to the new duration usingclip.start + duration
.
-
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.- Parameters:
- tfloat 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:
- fpsint
New
fps
attribute value for the clip.- change_durationbool, 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_maskbool
New
is_mask
attribute value for the clip.
-
with_make_frame
(make_frame)[source]¶ Sets a
make_frame
attribute for the clip. Useful for setting arbitrary/complicated videoclips.- Parameters:
- make_framefunction
New frame creator function for the clip.
-
with_memoize
(memoize)[source]¶ Sets whether the clip should keep the last frame read in memory.
- Parameters:
- memoizebool
Indicates if the clip should keep the last frame read in memory.
-
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.- Parameters:
- tfloat or tuple or str
New
start
attribute value for the clip.- change_endbool 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
.