Skip to content

Commit

Permalink
Many fixes changes for faster tests - hopefully no regression
Browse files Browse the repository at this point in the history
  • Loading branch information
Zulko committed Dec 16, 2018
1 parent 74b2226 commit dbc9ab1
Show file tree
Hide file tree
Showing 31 changed files with 441 additions and 324 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ lib
lib64
__pycache__
.cache/
.vscode

# Sublime
.sublime-project
Expand Down
16 changes: 8 additions & 8 deletions docs/getting_started/compositing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,26 @@ Positioning clips
If ``clip2`` and ``clip3`` are smaller than ``clip1``, you can decide where they will appear in the composition by setting their position. Here we indicate the coordinates of the top-left pixel of the clips: ::

video = CompositeVideoClip([clip1,
clip2.set_pos((45,150)),
clip3.set_pos((90,100))])
clip2.set_position((45,150)),
clip3.set_position((90,100))])

There are many ways to specify the position: ::

clip2.set_pos((45,150)) # x=45, y=150 , in pixels
clip2.set_position((45,150)) # x=45, y=150 , in pixels

clip2.set_pos("center") # automatically centered
clip2.set_position("center") # automatically centered

# clip2 is horizontally centered, and at the top of the picture
clip2.set_pos(("center","top"))
clip2.set_position(("center","top"))

# clip2 is vertically centered, at the left of the picture
clip2.set_pos(("left","center"))
clip2.set_position(("left","center"))

# clip2 is at 40% of the width, 70% of the height of the screen:
clip2.set_pos((0.4,0.7), relative=True)
clip2.set_position((0.4,0.7), relative=True)

# clip2's position is horizontally centered, and moving down !
clip2.set_pos(lambda t: ('center', 50+t) )
clip2.set_position(lambda t: ('center', 50+t) )

When indicating the position keep in mind that the ``y`` coordinate has its zero at the top of the picture:

Expand Down
2 changes: 1 addition & 1 deletion examples/logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def f(t,size, a = np.pi/3, thickness = 20):

screen = logo.on_color(moviesize, color = (0,0,0), pos='center')

shade = ColorClip(moviesize,col=(0,0,0))
shade = ColorClip(moviesize,color=(0,0,0))
mask_frame = lambda t : f(t,moviesize,duration)
shade.mask = VideoClip(ismask=True, get_frame = mask_frame)

Expand Down
7 changes: 4 additions & 3 deletions moviepy/Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,11 @@ def close(self):
# Implementation note for subclasses:
#
# * Memory-based resources can be left to the garbage-collector.
# * However, any open files should be closed, and subprocesses should be terminated.
# * Be wary that shallow copies are frequently used. Closing a Clip may affect its copies.
# * However, any open files should be closed, and subprocesses
# should be terminated.
# * Be wary that shallow copies are frequently used.
# Closing a Clip may affect its copies.
# * Therefore, should NOT be called by __del__().

pass

# Support the Context Manager protocol, to ensure that resources are cleaned up.
Expand Down
3 changes: 2 additions & 1 deletion moviepy/audio/fx/audio_normalize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from moviepy.decorators import audio_video_fx
from .volumex import volumex

@audio_video_fx
def audio_normalize(clip):
""" Return an audio (or video) clip whose volume is normalized
to 0db."""

mv = clip.max_volume()
return clip.volumex(1 / mv)
return volumex(clip, 1 / mv)
5 changes: 4 additions & 1 deletion moviepy/audio/io/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def read_chunk(self,chunksize):
L = self.nchannels*chunksize*self.nbytes
s = self.proc.stdout.read(L)
dt = {1: 'int8',2:'int16',4:'int32'}[self.nbytes]
result = np.fromstring(s, dtype=dt)
if hasattr(np, 'frombuffer'):
result = np.frombuffer(s, dtype='uint8')
else:
result = np.fromstring(s, dtype='uint8')
result = (1.0*result / 2**(8*self.nbytes-1)).\
reshape((int(len(result)/self.nchannels),
self.nchannels))
Expand Down
18 changes: 18 additions & 0 deletions moviepy/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.video.VideoClip import ImageClip

def close_all_clips(objects='globals', types=('audio', 'video', 'image')):
if objects == 'globals':
objects = globals()
if hasattr(objects, 'values'):
objects = objects.values()
types_tuple = tuple([
{'audio': AudioFileClip,
'video': VideoFileClip,
'image': ImageClip}[t]
for t in types
])
for obj in objects:
if isinstance(obj, types_tuple):
obj.close()
16 changes: 8 additions & 8 deletions moviepy/video/VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,14 @@ def on_color(self, size=None, color=(0, 0, 0), pos=None,
size = self.size
if pos is None:
pos = 'center'
colorclip = ColorClip(size, color)
colorclip = ColorClip(size, color=color)

if col_opacity is not None:
colorclip = (ColorClip(size, color, duration=self.duration)
colorclip = (ColorClip(size, color=color, duration=self.duration)
.set_opacity(col_opacity))
result = CompositeVideoClip([colorclip, self.set_pos(pos)])
result = CompositeVideoClip([colorclip, self.set_position(pos)])
else:
result = CompositeVideoClip([self.set_pos(pos)],
result = CompositeVideoClip([self.set_position(pos)],
size=size,
bg_color=color)

Expand Down Expand Up @@ -710,16 +710,16 @@ def set_position(self, pos, relative=False):
Examples
----------
>>> clip.set_pos((45,150)) # x=45, y=150
>>> clip.set_position((45,150)) # x=45, y=150
>>>
>>> # clip horizontally centered, at the top of the picture
>>> clip.set_pos(("center","top"))
>>> clip.set_position(("center","top"))
>>>
>>> # clip is at 40% of the width, 70% of the height:
>>> clip.set_pos((0.4,0.7), relative=True)
>>> clip.set_position((0.4,0.7), relative=True)
>>>
>>> # clip's position is horizontally centered, and moving up !
>>> clip.set_pos(lambda t: ('center', 50+t) )
>>> clip.set_position(lambda t: ('center', 50+t) )
"""
self.relative_pos = relative
Expand Down
16 changes: 11 additions & 5 deletions moviepy/video/compositing/CompositeVideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ def __init__(self, clips, size=None, bg_color=None, use_bgclip=False,
if bg_color is None:
bg_color = 0.0 if ismask else (0, 0, 0)

fpss = [c.fps for c in clips if hasattr(c, 'fps') and c.fps is not None]
fpss = [
c.fps
for c in clips
if hasattr(c, 'fps')
and c.fps is not None
]
if len(fpss) == 0:
self.fps = None
else:
Expand All @@ -78,7 +83,7 @@ def __init__(self, clips, size=None, bg_color=None, use_bgclip=False,
self.created_bg = False
else:
self.clips = clips
self.bg = ColorClip(size, col=self.bg_color)
self.bg = ColorClip(size, color=self.bg_color)
self.created_bg = True


Expand All @@ -97,7 +102,8 @@ def __init__(self, clips, size=None, bg_color=None, use_bgclip=False,
# compute mask if necessary
if transparent:
maskclips = [(c.mask if (c.mask is not None) else
c.add_mask().mask).set_pos(c.pos).set_end(c.end).set_start(c.start, change_end=False)
c.add_mask().mask).set_position(c.pos)
.set_end(c.end).set_start(c.start, change_end=False)
for c in self.clips]

self.mask = CompositeVideoClip(maskclips,self.size, ismask=True,
Expand Down Expand Up @@ -169,12 +175,12 @@ def clips_array(array, rows_widths=None, cols_widths=None,
clip = array[i,j]
w,h = clip.size
if (w < cw) or (h < rw):
clip = (CompositeVideoClip([clip.set_pos('center')],
clip = (CompositeVideoClip([clip.set_position('center')],
size = (cw,rw),
bg_color = bg_color).
set_duration(clip.duration))

array[i,j] = clip.set_pos((x,y))
array[i,j] = clip.set_position((x,y))

return CompositeVideoClip(array.flatten(), size = (xx[-1],yy[-1]),
bg_color = bg_color)
Expand Down
10 changes: 6 additions & 4 deletions moviepy/video/compositing/concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ def make_frame(t):
return clips[i].get_frame(t - tt[i])

def get_mask(c):
mask = c.mask or ColorClip([1, 1], col=1, ismask=True)
mask = c.mask or ColorClip([1, 1], color=1, ismask=True)
if mask.duration is None:
mask.duration = c.duration
return mask

result = VideoClip(ismask = ismask, make_frame = make_frame)
if any([c.mask is not None for c in clips]):
masks = [get_mask(c) for c in clips]
result.mask = concatenate_videoclips(masks, method="chain", ismask=True)
result.mask = concatenate_videoclips(masks, method="chain",
ismask=True)
result.clips = clips
elif method == "compose":
result = CompositeVideoClip( [c.set_start(t).set_pos('center')
result = CompositeVideoClip( [c.set_start(t).set_position('center')
for (c, t) in zip(clips, tt)],
size = (w, h), bg_color=bg_color, ismask=ismask)
else:
Expand All @@ -120,4 +121,5 @@ def get_mask(c):
return result


concatenate = deprecated_version_of(concatenate_videoclips, oldname="concatenate")
concatenate = deprecated_version_of(concatenate_videoclips,
oldname="concatenate")
4 changes: 2 additions & 2 deletions moviepy/video/compositing/on_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def on_color(clip, size=None, color=(0, 0, 0), pos=None, col_opacity=None):
size = clip.size
if pos is None:
pos = 'center'
colorclip = ColorClip(size, color)
colorclip = ColorClip(size, color=color)
if col_opacity:
colorclip = colorclip.with_mask().set_opacity(col_opacity)

return CompositeVideoClip([colorclip, clip.set_pos(pos)],
return CompositeVideoClip([colorclip, clip.set_position(pos)],
transparent=(col_opacity is not None))
4 changes: 2 additions & 2 deletions moviepy/video/compositing/transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def slide_in(clip, duration, side):
'top': lambda t: ('center', min(0, h*(t/duration-1))),
'bottom': lambda t: ('center', max(0, h*(1-t/duration)))}

return clip.set_pos(pos_dict[side])
return clip.set_position(pos_dict[side])


@requires_duration
Expand Down Expand Up @@ -110,7 +110,7 @@ def slide_out(clip, duration, side):
'top': lambda t: ('center', min(0, h*(1-(t-ts)/duration))),
'bottom': lambda t: ('center', max(0, h*((t-ts)/duration-1)))}

return clip.set_pos(pos_dict[side])
return clip.set_position(pos_dict[side])


@requires_duration
Expand Down
6 changes: 4 additions & 2 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ def read_frame(self):
result = self.lastread

else:

result = np.fromstring(s, dtype='uint8')
if hasattr(np, 'frombuffer'):
result = np.frombuffer(s, dtype='uint8')
else:
result = np.fromstring(s, dtype='uint8')
result.shape =(h, w, len(s)//(w*h)) # reshape((h, w, len(s)//(w*h)))
self.lastread = result

Expand Down
2 changes: 1 addition & 1 deletion moviepy/video/io/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def show(clip, t=0, with_mask=True, interactive=False):

if with_mask and (clip.mask is not None):
import moviepy.video.compositing.CompositeVideoClip as cvc
clip = cvc.CompositeVideoClip([clip.set_pos((0, 0))])
clip = cvc.CompositeVideoClip([clip.set_position((0, 0))])
img = clip.get_frame(t)
imdisplay(img)

Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest
from moviepy.utils import close_all_clips

# @pytest.fixture(autouse=True, scope='function')
# def clean_between_tests():
# yield
# close_all_clips(scope='global')
4 changes: 2 additions & 2 deletions tests/test_AudioClips.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from moviepy.audio.AudioClip import AudioClip, concatenate_audioclips, CompositeAudioClip

sys.path.append("tests")
import download_media
from test_helper import TMP_DIR
from . import download_media
from .test_helper import TMP_DIR


def test_download_media(capsys):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ImageSequenceClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from moviepy.video.io.ImageSequenceClip import ImageSequenceClip

sys.path.append("tests")
import download_media
from test_helper import TMP_DIR
from . import download_media
from .test_helper import TMP_DIR

def test_download_media(capsys):
with capsys.disabled():
Expand Down
11 changes: 6 additions & 5 deletions tests/test_PR.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
from moviepy.video.tools.interpolators import Trajectory
from moviepy.video.VideoClip import ColorClip, ImageClip, TextClip
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
from moviepy.utils import close_all_clips


sys.path.append("tests")
from test_helper import TMP_DIR, FONT
from .test_helper import TMP_DIR, FONT



def test_download_media(capsys):
"""Test downloading."""
import download_media
from . import download_media
with capsys.disabled():
download_media.download()

Expand All @@ -29,6 +30,7 @@ def test_PR_306():

with pytest.raises(Exception, message="Expecting Exception"):
TextClip.list('blah')
close_all_clips(locals())

def test_PR_339():
# In caption mode.
Expand Down Expand Up @@ -85,7 +87,7 @@ def test_PR_424():
assert str(record[1].message) == message2

def test_PR_458():
clip = ColorClip([1000, 600], color=(60, 60, 60), duration=10)
clip = ColorClip([1000, 600], color=(60, 60, 60), duration=2)
clip.write_videofile(os.path.join(TMP_DIR, "test.mp4"),
progress_bar=False, fps=30)
clip.close()
Expand All @@ -101,7 +103,7 @@ def test_PR_515():
def test_PR_528():
with ImageClip("media/vacation_2017.jpg") as clip:
new_clip = scroll(clip, w=1000, x_speed=50)
new_clip = new_clip.set_duration(20)
new_clip = new_clip.set_duration(1)
new_clip.fps = 24
new_clip.write_videofile(os.path.join(TMP_DIR, "pano.mp4"))

Expand All @@ -120,7 +122,6 @@ def test_PR_610():
clip1.fps = 24
clip2.fps = 25
composite = CompositeVideoClip([clip1, clip2])

assert composite.fps == 25


Expand Down
5 changes: 3 additions & 2 deletions tests/test_TextClip.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import sys

import pytest
from moviepy.utils import close_all_clips
from moviepy.video.fx.blink import blink
from moviepy.video.VideoClip import TextClip

sys.path.append("tests")
from test_helper import TMP_DIR
from .test_helper import TMP_DIR

def test_duration():

Expand All @@ -17,7 +18,7 @@ def test_duration():
clip2 = clip.fx(blink, d_on=1, d_off=1)
clip2 = clip2.set_duration(5)
assert clip2.duration == 5
clip2.close()
close_all_clips(locals())

# Moved from tests.py. Maybe we can remove these?
def test_if_textclip_crashes_in_caption_mode():
Expand Down
Loading

0 comments on commit dbc9ab1

Please sign in to comment.