Skip to content

Video editing with Python

addressed critical bug on end frames in videofiles. Also changed the name of temp files to allow for save in subfolders. added loop to audio loop in audio is not yet operational (buffer issues).

Zulkocd62b76f1ae6
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index ca09a25d..8c297eb3 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -276,11 +276,11 @@ class Clip:
def is_playing(self, t):
"""
- If t is a number, returns true if t is between the start and the
- end of the clip.
- If t is a numpy array, returns False if none of the t is in the
- clip, else returns a vector [b_1, b_2, b_3...] where b_i is true
- iff tti is in the clip.
+ If t is a number, returns true if t is between the start and
+ the end of the clip.
+ If t is a numpy array, returns False if none of the t is in
+ theclip, else returns a vector [b_1, b_2, b_3...] where b_i
+ is true iff tti is in the clip.
"""
if isinstance(t, np.ndarray):
@@ -307,19 +307,19 @@ class Clip:
@time_can_be_tuple
@apply_to_mask
@apply_to_audio
- def subclip(self, ta=0, tb=None):
+ def subclip(self, t_start=0, t_end=None):
"""
Returns a clip playing the content of the current clip
- between times ``ta`` and ``tb`` (in seconds).
- If ``tb`` is not provided, it is assumed to be the duration of
- the clip (potentially infinite).
- If ``tb`` is a negative value, it is reset to
- ``clip.duration - tb. ``. For instance: ::
+ between times ``t_start`` and ``t_end`` (in seconds).
+ If ``t_end`` is not provided, it is assumed to be the duration
+ of the clip (potentially infinite).
+ If ``t_end`` is a negative value, it is reset to
+ ``clip.duration + t_end. ``. For instance: ::
>>> # cut the last two seconds of the clip:
>>> newclip = clip.subclip(0,-2)
- If ``tb`` is provided or if the clip has a duration attribute,
+ If ``t_end`` is provided or if the clip has a duration attribute,
the duration of the returned clip is set automatically.
The ``mask`` and ``audio`` of the resulting subclip will be
@@ -327,18 +327,22 @@ class Clip:
they exist.
"""
+ if (self.duration is not None) and (t_start>self.duration):
+ raise ValueError("t_start (%.02f) "%t_start +
+ "should be smaller than the clip's "+
+ "duration (%.02f)."%self.duration)
- newclip = self.fl_time(lambda t: t + ta, apply_to=[])
- if (tb is None) and (self.duration != None):
- tb = self.duration
- elif tb<0:
+ newclip = self.fl_time(lambda t: t + t_start, apply_to=[])
+ if (t_end is None) and (self.duration is not None):
+ t_end = self.duration
+ elif t_end<0:
if self.duration is None:
print ("Error: subclip with negative times can only be"+
- "be taken from clips with a ``duration``")
+ "extracted from clips with a ``duration``")
else:
- tb = self.duration + tb
- if (tb is not None):
- newclip.duration = tb - ta
+ t_end = self.duration + t_end
+ if (t_end is not None):
+ newclip.duration = t_end - t_start
newclip.end = newclip.start + newclip.duration
return newclip
diff --git a/moviepy/audio/io/readers.py b/moviepy/audio/io/readers.py
index 08ad2392..ad68974c 100644
--- a/moviepy/audio/io/readers.py
+++ b/moviepy/audio/io/readers.py
@@ -164,7 +164,9 @@ class FFMPEG_AudioReader:
# 99.99 % of the cases
- # elements of t that are actually in time
+ # elements of t that are actually in the range of the
+ # audio file.
+
in_time = (tt>=0) & (tt < self.duration)
# The np.round in the next line is super-important.
diff --git a/moviepy/decorators.py b/moviepy/decorators.py
index d1ab14a0..93522c48 100644
--- a/moviepy/decorators.py
+++ b/moviepy/decorators.py
@@ -6,6 +6,7 @@ import decorator
from moviepy.tools import cvsecs
+
@decorator.decorator
def outplace(f, clip, *a, **k):
""" Enables to make operations outplace """
@@ -14,6 +15,7 @@ def outplace(f, clip, *a, **k):
return newclip
+
@decorator.decorator
def apply_to_mask(f, clip, *a, **k):
""" This decorator will apply the same function f to the mask of
@@ -25,6 +27,7 @@ def apply_to_mask(f, clip, *a, **k):
return newclip
+
@decorator.decorator
def apply_to_audio(f, clip, *a, **k):
""" This decorator will apply the function f to the audio of
@@ -36,6 +39,7 @@ def apply_to_audio(f, clip, *a, **k):
return newclip
+
@decorator.decorator
def add_mask_if_none(f, clip, *a, **k):
""" Add a mask to the clip if there is none. """
@@ -45,6 +49,7 @@ def add_mask_if_none(f, clip, *a, **k):
return f(clip, *a, **k)
+
@decorator.decorator
def requires_duration(f, clip, *a, **k):
""" Raise an error if the clip has no duration."""
@@ -54,6 +59,8 @@ def requires_duration(f, clip, *a, **k):
else:
return f(clip, *a, **k)
+
+
@decorator.decorator
def audio_video_fx(f, clip, *a, **k):
""" Use an audio function on a video/audio clip
@@ -71,6 +78,8 @@ def audio_video_fx(f, clip, *a, **k):
else:
return f(clip, *a, **k)
+
+
@decorator.decorator
def time_can_be_tuple(f, clip, *a, **k):
"""
diff --git a/moviepy/editor.py b/moviepy/editor.py
index 5d09c9f1..eaec5715 100644
--- a/moviepy/editor.py
+++ b/moviepy/editor.py
@@ -49,6 +49,7 @@ except ImportError:
# they can be walled with myclip.resize(width=500) instead of
# myclip.fx( vfx.resize, width= 500)
for method in ["vfx.crop",
+ "vfx.loop",
"vfx.resize",
"vfx.margin",
"vfx.fadein",
@@ -64,6 +65,7 @@ for method in ["vfx.crop",
for method in ["afx.audio_fadein",
"afx.audio_fadeout",
+ "vfx.loop",
"afx.volumex"]:
exec("AudioClip.%s = %s"%( method.split('.')[1], method))
diff --git a/moviepy/version.py b/moviepy/version.py
index 36467e14..9163927d 100644
--- a/moviepy/version.py
+++ b/moviepy/version.py
@@ -1 +1 @@
-__version__ = '0.2.1.7.15'
+__version__ = "0.2.1.7.16"
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index b765e48e..171eb94e 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -215,8 +215,8 @@ class VideoClip(Clip):
raise ValueError('audio_codec for file'
'%d unkown !'%filename)
- temp_audiofile = (Clip._TEMP_FILES_PREFIX +
- "to_videofile_SOUND_%s."%name + audio_ext)
+ temp_audiofile = (name+Clip._TEMP_FILES_PREFIX +
+ "to_videofile_SOUND%s"%audio_ext)
make_audio = ( (not os.path.exists(temp_audiofile))
or rewrite_audio)
@@ -230,9 +230,8 @@ class VideoClip(Clip):
if merge_audio:
# make a name for the temporary video file
- videofile = (Clip._TEMP_FILES_PREFIX +
- "to_videofile_"+
- filename)
+ videofile = (name + Clip._TEMP_FILES_PREFIX +
+ "to_videofile%s"%ext)
else:
diff --git a/moviepy/video/fx/loop.py b/moviepy/video/fx/loop.py
index 1aa7ba2d..50d7a653 100644
--- a/moviepy/video/fx/loop.py
+++ b/moviepy/video/fx/loop.py
@@ -6,7 +6,7 @@ from moviepy.decorators import (apply_to_mask,
@requires_duration
@apply_to_mask
@apply_to_audio
-def loop(self, n=None):
+def loop(self, n=None, duration=None):
"""
Returns a clip that plays the current clip in an infinite loop.
Ideal for clips coming from gifs.
@@ -16,8 +16,13 @@ def loop(self, n=None):
n
Number of times the clip should be played. If `None` the
the clip will loop indefinitely (i.e. with no set duration).
+
+ duration
+ Total duration of the clip. Can be specified instead of n.
"""
result = self.fl_time(lambda t: t % self.duration)
if n:
- result.duration = n*self.duration
+ duration = n*self.duration
+ if duration:
+ result.duration = duration
return result
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 1c02d82d..9ebe8813 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -96,8 +96,11 @@ class FFMPEG_VideoReader:
line = [l for l in lines if 'Duration: ' in l][0]
match = re.search(" [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]", line)
hms = map(float, line[match.start()+1:match.end()].split(':'))
- self.duration = cvsecs(*hms)
- self.nframes = int(self.duration*self.fps)
+ duration = cvsecs(*hms)
+ self.nframes = int(duration*self.fps)
+ self.duration = self.nframes / self.fps
+
+
def skip_frames(self, n=1):
"""Reads and throws away n frames """
@@ -145,7 +148,12 @@ class FFMPEG_VideoReader:
t = self.duration
pos = int(np.round(self.fps*t))+1
+ if pos > self.nframes+1:
+ raise ValueError("Video file %s has only %d frames but frame"
+ " #%d asked"%(self.filename, self.nframes, pos))
+
+
if pos == self.pos:
return self.lastread
else: