Python moviepy.editor.CompositeVideoClip() Examples

The following are 6 code examples of moviepy.editor.CompositeVideoClip(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module moviepy.editor , or try the search function .
Example #1
Source File: movies_utils.py    From mmvt with GNU General Public License v3.0 6 votes vote down vote up
def movie_in_movie(movie1_fname, movie2_fname, output_fname, pos=('right', 'bottom'), movie2_ratio=(1/3, 1/3),
                   margin=6, margin_color=(255, 255, 255), audio=False, fps=24, codec='libx264'):
    from moviepy import editor
    movie1 = editor.VideoFileClip(movie1_fname, audio=audio)
    w, h = movie1.size

    # THE PIANO FOOTAGE IS DOWNSIZED, HAS A WHITE MARGIN, IS
    # IN THE BOTTOM RIGHT CORNER
    movie2 = (editor.VideoFileClip(movie2_fname, audio=False).
             resize((w * movie2_ratio[0], h * movie2_ratio[1])).  # one third of the total screen
             margin(margin, color=margin_color).  # white margin
             margin(bottom=20, right=20, top=20, opacity=0).  # transparent
             set_pos(pos))

    final = editor.CompositeVideoClip([movie1, movie2])
    final.write_videofile(output_fname, fps=fps, codec=codec) 
Example #2
Source File: movies_utils.py    From mmvt with GNU General Public License v3.0 6 votes vote down vote up
def add_text_to_movie(movie_fol, movie_name, out_movie_name, subs, fontsize=50, txt_color='red', font='Xolonium-Bold',
                      subs_delim=' ', bg_color=None):
    # Should install ImageMagick
    # For centos6: https://www.vultr.com/docs/install-imagemagick-on-centos-6
    # For centos7: http://helostore.com/blog/install-imagemagick-on-centos-7
    from moviepy import editor

    def annotate(clip, txt, txt_color=txt_color, fontsize=fontsize):
        """ Writes a text at the bottom of the clip. """
        # To make this code works the policy.xml should be editted
        #  identify -list policy
        # sudo gedit /etc/ImageMagick/policy.xml &
        # Put under comment the TEXT and LABEL lines
        txtclip = editor.TextClip(txt, fontsize=fontsize, color=txt_color)  # font=font
        # txtclip = txtclip.on_color((clip.w, txtclip.h + 6), color=(0, 0, 255), pos=(6, 'center'))
        cvc = editor.CompositeVideoClip([clip, txtclip.set_pos(('center', 'bottom'))])
        return cvc.set_duration(clip.duration)

    if isinstance(subs, str):
        subs = import_subs(movie_fol, subs, subs_delim)
    video = editor.VideoFileClip(op.join(movie_fol, movie_name))
    annotated_clips = [annotate(video.subclip(from_t, to_t), txt) for (from_t, to_t), txt in subs]
    final_clip = editor.concatenate_videoclips(annotated_clips)
    final_clip.write_videofile(op.join(movie_fol, out_movie_name)) 
Example #3
Source File: utils.py    From magic-the-gifening with MIT License 5 votes vote down vote up
def create_mtg_gif(name, id, border):
    if border == 'm':   # Modern (post-8th Ed)
        card_upper_corner = (19, 38)
        gif_width = 202 - card_upper_corner[0]
        gif_height = 172 - card_upper_corner[1]
    elif border == 'c':   # Current (post-Magic 2015)
        card_upper_corner = (17, 34)
        gif_width = 204 - card_upper_corner[0]
        gif_height = 173 - card_upper_corner[1]
    else:   # Old (pre-8th Ed)
        card_upper_corner = (25, 30)
        gif_width = 196 - card_upper_corner[0]
        gif_height = 168 - card_upper_corner[1]

    mtg_card = Image.open(BytesIO(requests.get(get_mtg_image(id)).content))
    mtg_card = ImageClip(np.asarray(mtg_card)).resize((222, 310))

    get_giphy_gif(name)
    giphy_gif = (VideoFileClip('giphy_gif.mp4',
                               target_resolution=(gif_height, gif_width))
                 .set_pos(card_upper_corner)

                 )

    if giphy_gif.duration < 2:
        giphy_gif = giphy_gif.fx(loop, n=1+int(2 // giphy_gif.duration))

    mtg_gif = CompositeVideoClip([mtg_card, giphy_gif])
    mtg_gif = mtg_gif.set_start(0).set_duration(giphy_gif.duration)
    # mtg_gif.write_gif("mtg_gif.gif")
    mtg_gif.write_videofile("mtg_gif.mp4", codec='libx264',
                            bitrate=str(np.power(10, 7)), verbose=False,
                            progress_bar=False,
                            audio=False, ffmpeg_params=['-pix_fmt', 'yuv420p']) 
Example #4
Source File: run.py    From starthinker with Apache License 2.0 5 votes vote down vote up
def edit_video(video):
  clips = [mp.VideoFileClip(video['file_or_url'])]

  for effect in video['effects']:
    clips.extend(get_effects(clips[0], effect))
 
  video = mp.CompositeVideoClip(clips)

  return video 
Example #5
Source File: crabrave.py    From Trusty-cogs with MIT License 5 votes vote down vote up
def make_crab(self, t: str, u_id: int) -> bool:
        """Non blocking crab rave video generation from DankMemer bot

        https://github.com/DankMemer/meme-server/blob/master/endpoints/crab.py
        """
        fp = str(cog_data_path(self) / f"Verdana.ttf")
        clip = VideoFileClip(str(cog_data_path(self)) + "/template.mp4")
        # clip.volume(0.5)
        text = TextClip(t[0], fontsize=48, color="white", font=fp)
        text2 = (
            TextClip("____________________", fontsize=48, color="white", font=fp)
            .set_position(("center", 210))
            .set_duration(15.4)
        )
        text = text.set_position(("center", 200)).set_duration(15.4)
        text3 = (
            TextClip(t[1], fontsize=48, color="white", font=fp)
            .set_position(("center", 270))
            .set_duration(15.4)
        )

        video = CompositeVideoClip(
            [clip, text.crossfadein(1), text2.crossfadein(1), text3.crossfadein(1)]
        ).set_duration(15.4)
        video = video.volumex(0.1)
        video.write_videofile(
            str(cog_data_path(self)) + f"/{u_id}crabrave.mp4",
            threads=1,
            preset="superfast",
            verbose=False,
            logger=None,
            temp_audiofile=str(cog_data_path(self) / f"{u_id}crabraveaudio.mp3")
            # ffmpeg_params=["-filter:a", "volume=0.5"]
        )
        clip.close()
        video.close()
        return True 
Example #6
Source File: movies_utils.py    From mmvt with GNU General Public License v3.0 5 votes vote down vote up
def add_text_example(movie):
    from moviepy import editor
    # A CLIP WITH A TEXT AND A BLACK SEMI-OPAQUE BACKGROUND
    txt = editor.TextClip("V. Zulkoninov - Ukulele Sonata", font='Amiri-regular',
                   color='white', fontsize=24)

    txt_col = txt.on_color(size=(movie.w + txt.w, txt.h - 10),
                           color=(0, 0, 0), pos=(6, 'center'), col_opacity=0.6)

    # THE TEXT CLIP IS ANIMATED.
    # I am *NOT* explaining the formula, understands who can/want.
    w, h = movie.size
    txt_mov = txt_col.set_pos(lambda t: (max(w / 30, int(w - 0.5 * w * t)),
                                         max(5 * h / 6, int(100 * t))))

    # FINAL ASSEMBLY
    final = editor.CompositeVideoClip([ukulele, txt_mov, piano])
    final.subclip(0, 5).write_videofile("../../ukulele.avi", fps=24, codec='libx264')


# import tempfile, os
# tempfile_fd, tempfilename = tempfile.mkstemp(suffix='.png', dir='/home/npeled/temp')
# os.close(tempfile_fd)
# temptxt_fd, temptxt = tempfile.mkstemp(suffix='.txt', dir='/home/npeled/temp')
# os.write(temptxt_fd, bytes(txt, 'UTF8'))
# os.close(temptxt_fd)