Skip to content

Commit

Permalink
Merge pull request #724 from tburrows13/videocliptest
Browse files Browse the repository at this point in the history
Added tests, new duration arg in to_ImageClip()
  • Loading branch information
tburrows13 committed Feb 28, 2018
2 parents ced055f + d3370e4 commit 6b0710e
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 19 deletions.
14 changes: 10 additions & 4 deletions moviepy/video/VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ def write_gif(self, filename, fps=None, program='imageio',
the colors that are less than fuzz% different are in fact
the same.
tempfiles
Writes every frame to a file instead of passing them in the RAM.
Useful on computers with little RAM. Can only be used with
ImageMagick' or 'ffmpeg'.
Notes
-----
Expand Down Expand Up @@ -453,8 +458,8 @@ def write_gif(self, filename, fps=None, program='imageio',
opt1 ='OptimizeTransparency'
write_gif_with_tempfiles(self, filename, fps=fps,
program=program, opt=opt1, fuzz=fuzz,
verbose=verbose,
loop=loop, dispose=dispose, colors=colors)
verbose=verbose, loop=loop,
dispose=dispose, colors=colors)
else:
write_gif(self, filename, fps=fps, program=program,
opt=opt, fuzz=fuzz, verbose=verbose, loop=loop,
Expand Down Expand Up @@ -719,13 +724,14 @@ def set_position(self, pos, relative=False):
# CONVERSIONS TO OTHER TYPES

@convert_to_seconds(['t'])
def to_ImageClip(self, t=0, with_mask=True):
def to_ImageClip(self, t=0, with_mask=True, duration=None):
"""
Returns an ImageClip made out of the clip's frame at time ``t``,
which can be expressed in seconds (15.35), in (min, sec),
in (hour, min, sec), or as a string: '01:03:05.35'.
"""
newclip = ImageClip(self.get_frame(t), ismask=self.ismask)
newclip = ImageClip(self.get_frame(t), ismask=self.ismask,
duration=duration)
if with_mask and self.mask is not None:
newclip.mask = self.mask.to_ImageClip(t)
return newclip
Expand Down
25 changes: 10 additions & 15 deletions moviepy/video/io/gif_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@
from moviepy.compat import DEVNULL

try:
import imageio
IMAGEIO_FOUND = True
import imageio
IMAGEIO_FOUND = True
except ImportError:
IMAGEIO_FOUND = False



IMAGEIO_FOUND = False


@requires_duration
@use_clip_fps_by_default
def write_gif_with_tempfiles(clip, filename, fps=None, program= 'ImageMagick',
opt="OptimizeTransparency", fuzz=1, verbose=True,
loop=0, dispose=True, colors=None, tempfiles=False):
loop=0, dispose=True, colors=None):
""" Write the VideoClip to a GIF file.
Expand Down Expand Up @@ -250,16 +247,13 @@ def write_gif(clip, filename, fps=None, program= 'ImageMagick',
proc2.wait()
if opt:
proc3.wait()
verbose_print(verbose, "[MoviePy] >>>> File %s is ready !"%filename)
verbose_print(verbose, "[MoviePy] >>>> File %s is ready!"%filename)


def write_gif_with_image_io(clip, filename, fps=None, opt=0, loop=0,
colors=None, verbose=True):
"""
Writes the gif with the Python library ImageIO (calls FreeImage).
For the moment ImageIO is not installed with MoviePy. You need to install
imageio (pip install imageio) to use this.
Parameters
-----------
Expand All @@ -268,16 +262,17 @@ def write_gif_with_image_io(clip, filename, fps=None, opt=0, loop=0,
"""

if colors is None:
colors=256
colors = 256

if not IMAGEIO_FOUND:
raise ImportError("Writing a gif with imageio requires ImageIO installed,"
raise ImportError("Writing a gif with imageio requires ImageIO installed,"
" with e.g. 'pip install imageio'")

if fps is None:
fps = clip.fps

quantizer = 0 if opt!= 0 else 'nq'
quantizer = 0 if opt != 0 else 'nq'

writer = imageio.save(
filename,
duration=1.0/fps,
Expand All @@ -286,7 +281,7 @@ def write_gif_with_image_io(clip, filename, fps=None, opt=0, loop=0,
loop=loop
)

verbose_print(verbose, "\n[MoviePy] Building file %s with imageio\n"%filename)
verbose_print(verbose, "\n[MoviePy] Building file %s with imageio\n" % filename)

for frame in clip.iter_frames(fps=fps, progress_bar=True, dtype='uint8'):

Expand Down
145 changes: 145 additions & 0 deletions tests/test_VideoClip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import sys
import os
from numpy import sin, pi

import pytest

from moviepy.video.VideoClip import VideoClip, ColorClip
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.audio.AudioClip import AudioClip
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.video.fx.speedx import speedx

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


def test_download_media(capsys):
with capsys.disabled():
download_media.download()


def test_check_codec():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "not_a_video.mas")
try:
clip.write_videofile(location)
except ValueError as e:
assert "MoviePy couldn't find the codec associated with the filename." \
" Provide the 'codec' parameter in write_videofile." in str(e)


def test_save_frame():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "save_frame.png")
clip.save_frame(location, t=0.5)
assert os.path.isfile(location)


def test_write_image_sequence():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.3)
locations = clip.write_images_sequence(os.path.join(TMP_DIR, "frame%02d.png"))
for location in locations:
assert os.path.isfile(location)


def test_write_gif_imageio():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.8)
location = os.path.join(TMP_DIR, "imageio_gif.gif")
clip.write_gif(location, program="imageio")
assert os.path.isfile(location)


def test_write_gif_ffmpeg():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "ffmpeg_gif.gif")
clip.write_gif(location, program="ffmpeg")
assert os.path.isfile(location)


def test_write_gif_ffmpeg_tmpfiles():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.4)
location = os.path.join(TMP_DIR, "ffmpeg_tmpfiles_gif.gif")
clip.write_gif(location, program="ffmpeg", tempfiles=True)
assert os.path.isfile(location)


def test_write_gif_ImageMagick():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "imagemagick_gif.gif")
clip.write_gif(location, program="ImageMagick")
# Fails for some reason
#assert os.path.isfile(location)


def test_write_gif_ImageMagick_tmpfiles():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5)
location = os.path.join(TMP_DIR, "imagemagick_tmpfiles_gif.gif")
clip.write_gif(location, program="ImageMagick", tempfiles=True)
assert os.path.isfile(location)


def test_subfx():
clip = VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0, 5)
transform = lambda c: speedx(c, 0.5)
new_clip = clip.subfx(transform, 2, 4)
location = os.path.join(TMP_DIR, "subfx.mp4")
new_clip.write_videofile(location)
assert os.path.isfile(location)


def test_oncolor():
# It doesn't need to be a ColorClip
clip = ColorClip(size=(100, 60), color=(255, 0, 0), duration=4)
on_color_clip = clip.on_color(size=(200, 160), color=(0, 0, 255))
location = os.path.join(TMP_DIR, "oncolor.mp4")
on_color_clip.write_videofile(location, fps=24)
assert os.path.isfile(location)


def test_setaudio():
clip = ColorClip(size=(100, 60), color=(255, 0, 0), duration=2)
make_frame_440 = lambda t: [sin(440 * 2 * pi * t)]
audio = AudioClip(make_frame_440, duration=2)
audio.fps = 44100
clip = clip.set_audio(audio)
location = os.path.join(TMP_DIR, "setaudio.mp4")
clip.write_videofile(location, fps=24)
assert os.path.isfile(location)


def test_setaudio_with_audiofile():
clip = ColorClip(size=(100, 60), color=(255, 0, 0), duration=2)
audio = AudioFileClip("media/crunching.mp3").subclip(0, 2)
clip = clip.set_audio(audio)
location = os.path.join(TMP_DIR, "setaudiofile.mp4")
clip.write_videofile(location, fps=24)
assert os.path.isfile(location)


def test_setopacity():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
clip = clip.set_opacity(0.5)
clip = clip.on_color(size=(1000, 1000), color=(0, 0, 255), col_opacity=0.8)
location = os.path.join(TMP_DIR, "setopacity.mp4")
clip.write_videofile(location)
assert os.path.isfile(location)


def test_toimageclip():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
clip = clip.to_ImageClip(t=0.1, duration=2)
location = os.path.join(TMP_DIR, "toimageclip.mp4")
clip.write_videofile(location, fps=24)
assert os.path.isfile(location)


def test_withoutaudio():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
new_clip = clip.without_audio()
assert new_clip.audio is None


if __name__ == "__main__":
pytest.main()

0 comments on commit 6b0710e

Please sign in to comment.