Video editing with Python
Add more tests and documentation for FramesMatches (#1600)
Álvaro Mondéjar84582d9af6b8
diff --git a/moviepy/video/tools/cuts.py b/moviepy/video/tools/cuts.pyindex 4c0e2dad..ed393b27 100644--- a/moviepy/video/tools/cuts.py+++ b/moviepy/video/tools/cuts.py@@ -113,10 +113,33 @@ class FramesMatches(list):list.__init__(self, sorted(lst, key=lambda e: e.max_distance))def best(self, n=1, percent=None):- """TODO: needs documentation"""+ """Returns a new instance of FramesMatches object or a FramesMatch+ from the current class instance given different conditions.++ By default returns the first FramesMatch that the current instance+ stores.++ Parameters+ ----------++ n : int, optional+ Number of matches to retrieve from the current FramesMatches object.+ Only has effect when ``percent=None``.++ percent : float, optional+ Percent of the current match to retrieve.++ Returns+ -------++ FramesMatch or FramesMatches : If the number of matches to retrieve is+ greater than 1 returns a FramesMatches object, otherwise a+ FramesMatch.++ """if percent is not None:n = len(self) * percent / 100- return self[0] if n == 1 else FramesMatches(self[:n])+ return self[0] if n == 1 else FramesMatches(self[: int(n)])def filter(self, condition):"""Return a FramesMatches object obtained by filtering out the@@ -208,6 +231,7 @@ class FramesMatches(list):>>> from moviepy import VideoFileClip>>> from moviepy.video.tools.cuts import FramesMatches+ >>>>>> clip = VideoFileClip("foo.mp4").resize(width=200)>>> matches = FramesMatches.from_clip(... clip, distance_threshold=10, max_duration=3, # will take time@@ -286,20 +310,50 @@ class FramesMatches(list):def select_scenes(self, match_threshold, min_time_span, nomatch_threshold=None, time_distance=0):- """+ """Select the scenes at which a video clip can be reproduced as the+ smoothest possible way, mainly oriented for the creation of GIF images.Parameters----------- match_threshold- The smaller, the better-looping the gifs are.- min_time_span- Only GIFs with a duration longer than min_time_span (in seconds)- will be extracted.+ match_threshold : float+ Maximum distance possible between frames. The smaller, the+ better-looping the GIFs are.++ min_time_span : float+ Minimum duration for a scene. Only matches with a duration longer+ than the value passed to this parameters will be extracted.++ nomatch_threshold : float, optional+ Minimum distance possible between frames. If is ``None``, then it is+ chosen equal to ``match_threshold``.++ time_distance : float, optional+ Minimum time offset possible between matches.- nomatch_threshold- If None, then it is chosen equal to match_threshold+ Returns+ -------++ FramesMatches : New instance of the class with the selected scenes.+ Examples+ --------++ >>> from pprint import pprint+ >>> from moviepy.editor import *+ >>> from moviepy.video.tools.cuts import FramesMatches+ >>>+ >>> ch_clip = VideoFileClip("media/chaplin.mp4").subclip(1, 4)+ >>> clip = concatenate_videoclips([ch_clip.time_mirror(), ch_clip])+ >>>+ >>> result = FramesMatches.from_clip(clip, 10, 3).select_scenes(+ ... 1, 2, nomatch_threshold=0,+ ... )+ >>> pprint(result)+ [(1.0000, 4.0000, 0.0000, 0.0000),+ (1.1600, 3.8400, 0.0000, 0.0000),+ (1.2800, 3.7200, 0.0000, 0.0000),+ (1.4000, 3.6000, 0.0000, 0.0000)]"""if nomatch_threshold is None:nomatch_threshold = match_threshold@@ -353,11 +407,47 @@ class FramesMatches(list):return FramesMatches(result)- def write_gifs(self, clip, gif_dir):- """TODO: needs documentation"""+ def write_gifs(self, clip, gifs_dir, **kwargs):+ """Extract the matching frames represented by the instance from a clip+ and write them as GIFs in a directory, one GIF for each matching frame.++ Parameters+ ----------++ clip : video.VideoClip.VideoClip+ A video clip whose frames scenes you want to obtain as GIF images.++ gif_dir : str+ Directory in which the GIF images will be written.++ kwargs+ Passed as ``clip.write_gif`` optional arguments.++ Examples+ --------++ >>> import os+ >>> from pprint import pprint+ >>> from moviepy.editor import *+ >>> from moviepy.video.tools.cuts import FramesMatches+ >>>+ >>> ch_clip = VideoFileClip("media/chaplin.mp4").subclip(1, 4)+ >>> clip = concatenate_videoclips([ch_clip.time_mirror(), ch_clip])+ >>>+ >>> result = FramesMatches.from_clip(clip, 10, 3).select_scenes(+ ... 1, 2, nomatch_threshold=0,+ ... )+ >>>+ >>> os.mkdir("foo")+ >>> result.write_gifs(clip, "foo")+ MoviePy - Building file foo/00000100_00000400.gif with imageio.+ MoviePy - Building file foo/00000115_00000384.gif with imageio.+ MoviePy - Building file foo/00000128_00000372.gif with imageio.+ MoviePy - Building file foo/00000140_00000360.gif with imageio.+ """for (start, end, _, _) in self:- name = "%s/%08d_%08d.gif" % (gif_dir, 100 * start, 100 * end)- clip.subclip(start, end).write_gif(name)+ name = "%s/%08d_%08d.gif" % (gifs_dir, 100 * start, 100 * end)+ clip.subclip(start, end).write_gif(name, **kwargs)@use_clip_fps_by_default@@ -366,11 +456,12 @@ def detect_scenes():"""Detects scenes of a clip based on luminosity changes.- Note that for large clip this may take some time+ Note that for large clip this may take some time.Returns-------- cuts, luminosities++ tuple : cuts, luminositiescuts is a series of cuts [(0,t1), (t1,t2),...(...,tf)]luminosities are the luminosities computed for eachframe of the clip.@@ -378,31 +469,29 @@ def detect_scenes(Parameters----------- clip+ clip : video.VideoClip.VideoClip, optionalA video clip. Can be None if a list of luminosities isprovided instead. If provided, the luminosity of eachframe of the clip will be computed. If the clip has no'fps' attribute, you must provide it.- luminosities+ luminosities : list, optionalA list of luminosities, e.g. returned by detect_scenesin a previous run.- luminosity_threshold+ luminosity_threshold : float, optionalDetermines a threshold above which the 'luminosity jumps'will be considered as scene changes. A scene change is definedas a change between 2 consecutive frames that is larger than(avg * thr) where avg is the average of the absolute changesbetween consecutive frames.- logger+ logger : str, optionalEither ``"bar"`` for progress bar or ``None`` or any Proglog logger.- fps- Must be provided if you provide no clip or a clip without- fps attribute.--+ fps : int, optional+ Frames per second value. Must be provided if you provide+ no clip or a clip without fps attribute."""if luminosities is None:luminosities = [diff --git a/tests/test_videotools.py b/tests/test_videotools.pyindex 890cf5a9..049b0a44 100644--- a/tests/test_videotools.py+++ b/tests/test_videotools.py@@ -3,6 +3,7 @@import importlibimport mathimport os+import shutilimport sysimport pytest@@ -12,6 +13,7 @@ from moviepy.audio.fx.multiply_volume import multiply_volumefrom moviepy.audio.tools.cuts import find_audio_periodfrom moviepy.video.compositing.concatenate import concatenate_videoclipsfrom moviepy.video.fx.loop import loop+from moviepy.video.fx.time_mirror import time_mirrorfrom moviepy.video.io.VideoFileClip import VideoFileClipfrom moviepy.video.tools.credits import CreditsClipfrom moviepy.video.tools.cuts import (@@ -231,6 +233,127 @@ def test_FramesMatches_save_load():assert frames_match == input_matching_frames[i]+ ("n", "percent", "expected_result"),+ (+ pytest.param(1, None, FramesMatch(1, 2, 0, 0), id="n=1"),+ pytest.param(+ 2,+ None,+ FramesMatches([FramesMatch(1, 2, 0, 0), FramesMatch(2, 3, 0, 0)]),+ id="n=2",+ ),+ pytest.param(+ 1,+ 50,+ FramesMatches([FramesMatch(1, 2, 0, 0), FramesMatch(2, 3, 0, 0)]),+ id="percent=50",+ ),+ ),+)+def test_FramesMatches_best(n, percent, expected_result):+ assert (+ FramesMatches(+ [+ FramesMatch(1, 2, 0, 0),+ FramesMatch(2, 3, 0, 0),+ FramesMatch(4, 5, 0, 0),+ FramesMatch(5, 6, 0, 0),+ ]+ ).best(n=n, percent=percent)+ == expected_result+ )+++ (+ "filename",+ "subclip",+ "match_threshold",+ "min_time_span",+ "nomatch_threshold",+ "expected_result",+ ),+ (+ pytest.param(+ "media/chaplin.mp4",+ (1, 3),+ 1,+ 2,+ 0,+ FramesMatches(+ [+ FramesMatch(0.08, 2.92, 0, 0),+ FramesMatch(0.2, 2.8, 0, 0),+ FramesMatch(0.32, 2.68, 0, 0),+ FramesMatch(0.44, 2.56, 0, 0),+ ]+ ),+ id="(media/chaplin.mp4)(1, 3).fx(time_mirror)",+ ),+ ),+)+def test_FramesMatches_select_scenes(+ filename,+ subclip,+ match_threshold,+ min_time_span,+ nomatch_threshold,+ expected_result,+):+ video_clip = VideoFileClip(filename)+ if subclip is not None:+ video_clip = video_clip.subclip(subclip[0], subclip[1])+ clip = concatenate_videoclips([video_clip.fx(time_mirror), video_clip])+ result = FramesMatches.from_clip(clip, 10, 3, logger=None).select_scenes(+ match_threshold,+ min_time_span,+ nomatch_threshold=nomatch_threshold,+ )++ assert len(result) == len(expected_result)+ assert result == expected_result+++def test_FramesMatches_write_gifs():+ video_clip = VideoFileClip("media/chaplin.mp4").subclip(0, 0.2)+ clip = concatenate_videoclips([video_clip.fx(time_mirror), video_clip])++ # add matching frame starting at start < clip.start which should be ignored+ matching_frames = FramesMatches.from_clip(clip, 10, 3, logger=None)+ matching_frames.insert(0, FramesMatch(-1, -0.5, 0, 0))+ matching_frames = matching_frames.select_scenes(+ 1,+ 0.01,+ nomatch_threshold=0,+ )++ gifs_dir = os.path.join(TMP_DIR, "moviepy_FramesMatches_write_gifs")+ if os.path.isdir(gifs_dir):+ shutil.rmtree(gifs_dir)+ os.mkdir(gifs_dir)+ assert os.path.isdir(gifs_dir)++ matching_frames.write_gifs(clip, gifs_dir, logger=None)++ gifs_filenames = os.listdir(gifs_dir)+ assert len(gifs_filenames) == 7++ for filename in gifs_filenames:+ filepath = os.path.join(gifs_dir, filename)+ assert os.path.isfile(filepath)++ with open(filepath, "rb") as f:+ assert len(f.readline())++ end, start = filename.split(".")[0].split("_")+ end, start = (int(end), int(start))+ assert isinstance(end, int)+ assert isinstance(end, int)++ shutil.rmtree(gifs_dir)++@pytest.mark.parametrize(("clip", "filetype", "fps", "maxduration", "t", "expected_error"),(