Skip to content

Video editing with Python

Repaired Py3 compatibility

Zulko0fd3fa873fb7
diff --git a/moviepy/decorators.py b/moviepy/decorators.py
index ccd48b76..1a120abf 100644
--- a/moviepy/decorators.py
+++ b/moviepy/decorators.py
@@ -70,7 +70,12 @@ def audio_video_fx(f, clip, *a, **k):
def preprocess_args(fun,varnames):
def warper(f, *a, **kw):
- names = f.func_code.co_varnames
+ if hasattr(f, "func_code"):
+ func_code = f.func_code # Python 2
+ else:
+ func_code = f.__code__ # Python 3
+
+ names = func_code.co_varnames
new_a = [cvsecs(arg) if (name in varnames) else arg
for (arg, name) in zip(a, names)]
new_kw = {k: fun(v) if k in varnames else v
diff --git a/moviepy/tools.py b/moviepy/tools.py
index b2dd1efd..6172e3ec 100644
--- a/moviepy/tools.py
+++ b/moviepy/tools.py
@@ -37,6 +37,13 @@ def subprocess_call(cmd, verbose=True, errorprint=True):
del proc
+def is_string(obj):
+ """ Returns true if s is string or string-like object,
+ compatible with Python 2 and Python 3."""
+ try:
+ return isinstance(obj, basestring)
+ except NameError:
+ return isinstance(obj, str)
def cvsecs(time):
""" Will convert any time into seconds.
@@ -49,13 +56,13 @@ def cvsecs(time):
>>> cvsecs('01:01:33.045') -> 3693.045
>>> cvsecs('01:01:33,5') #coma works too
"""
-
- if isinstance(time, basestring):
+
+ if is_string(time):
if (',' not in time) and ('.' not in time):
time = time + '.0'
expr = r"(\d+):(\d+):(\d+)[,|.](\d+)"
finds = re.findall(expr, time)[0]
- nums = map(float, finds)
+ nums = list( map(float, finds) )
return ( 3600*int(finds[0])
+ 60*int(finds[1])
+ int(finds[2])
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 870242e5..aa3f69a4 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -26,8 +26,12 @@ from .tools.drawing import blit
from ..Clip import Clip
from ..conf import FFMPEG_BINARY, IMAGEMAGICK_BINARY
-from ..tools import (subprocess_call, verbose_print,
- deprecated_version_of)
+
+from ..tools import (subprocess_call,
+ verbose_print,
+ is_string,
+ deprecated_version_of)
+
from ..decorators import (apply_to_mask,
requires_duration,
outplace,
@@ -137,7 +141,8 @@ class VideoClip(Clip):
@requires_duration
- def write_videofile(self, filename, fps=24, codec='libx264',
+ def write_videofile(self, filename=None,
+ fps=24, codec='libx264',
bitrate=None, audio=True, audio_fps=44100,
preset="medium",
audio_nbytes = 4, audio_codec= 'libmp3lame',
@@ -152,8 +157,10 @@ class VideoClip(Clip):
-----------
filename
- Name of the video file. The extension must correspond to the
- codec used (see below), ar simply be '.avi'.
+ Name of the video file to write in. Can be None if a 'filebuffer'
+ parameter is provided (see below)
+ The extension must correspond to the "codec" used (see below),
+ or simply be '.avi' (which will work with any codec).
fps
Number of frames per second in the resulting video file.
@@ -217,6 +224,13 @@ class VideoClip(Clip):
These will be files ending with '.log' with the name of the
output file in them.
+ Examples
+ ========
+
+ >>> from moviepy.editor import VideoFileClip
+ >>> clip = VideoFileClip("myvideo.mp4").subclip(100,120)
+ >>> clip.write_videofile("my_new_video.mp4")
+
"""
name, ext = os.path.splitext(os.path.basename(filename))
@@ -226,7 +240,7 @@ class VideoClip(Clip):
elif audio_codec == 'raw32':
audio_codec = 'pcm_s32le'
- audiofile = audio if isinstance(audio, basestring) else None
+ audiofile = audio if is_string(audio) else None
make_audio = ((audiofile is None) and (audio==True) and
(self.audio is not None))
diff --git a/moviepy/video/fx/blackwhite.py b/moviepy/video/fx/blackwhite.py
index b17c0fe1..d9489371 100644
--- a/moviepy/video/fx/blackwhite.py
+++ b/moviepy/video/fx/blackwhite.py
@@ -14,6 +14,6 @@ def blackwhite(clip, RGB = [1,1,1], preserve_luminosity=True):
def fl(im):
im = (R*im[:,:,0] + G*im[:,:,1] + B*im[:,:,2])/3
- return np.dstack(3*[im]).astype('uint8')
+ return np.dstack(3*[im]).astype('uint8')
return clip.fl_image(fl)
diff --git a/moviepy/video/io/VideoFileClip.py b/moviepy/video/io/VideoFileClip.py
index ad9967ef..b3cfb4f1 100644
--- a/moviepy/video/io/VideoFileClip.py
+++ b/moviepy/video/io/VideoFileClip.py
@@ -79,3 +79,7 @@ class VideoFileClip(VideoClip):
buffersize= audio_buffersize,
fps = audio_fps,
nbytes = audio_nbytes)
+
+ def __del__(self):
+ """ Close/delete the internal reader. """
+ del self.reader
diff --git a/moviepy/video/io/ffmpeg_writer.py b/moviepy/video/io/ffmpeg_writer.py
index 68b2b935..4adc2805 100644
--- a/moviepy/video/io/ffmpeg_writer.py
+++ b/moviepy/video/io/ffmpeg_writer.py
@@ -105,7 +105,7 @@ class FFMPEG_VideoWriter:
(size[1]%2 == 0))
else [])
- + [ '-r', "%d"%fps, filename ]
+ + [ '-r', "%.02f"%fps, filename ]
#+ (["-acodec", "copy"] if (audiofile is not None) else [])
)
diff --git a/moviepy/video/tools/drawing.py b/moviepy/video/tools/drawing.py
index 9a6ce88f..5c7121e1 100644
--- a/moviepy/video/tools/drawing.py
+++ b/moviepy/video/tools/drawing.py
@@ -226,7 +226,6 @@ def color_split(size,x=None,y=None,p1=None,p2=None,vector=None,
elif x is not None:
vector = np.array([0,1.0])
p1 = np.array([x, 0])
- print vector, p1
elif y is not None:
vector = np.array([1.0, 0.0])
p1 = np.array([0,y])