Result: https://www.youtube.com/watch?v=Qu7HJrsEYFg

Source: https://github.com/Zulko/moviepy/blob/master/examples/dancing_knights.py

This is how we can imagine knights dancing at the 15th century, based on a very serious historical study here:

Process:

  1. Get the video of a dancing knight, and a (Creative Commons) audio music file.
  2. load the audio file and automatically find the tempo
  3. load the video and automatically find a segment that loops well
  4. extract this segment, slow it down so that it matches the audio tempo, and make it loop forever.
  5. Symmetrize this segment so that we will get two knights instead of one
  6. Add a title screen and some credits, write to a file.

In [29]:
from moviepy.editor import *
from moviepy.video.tools.cuts import find_video_period
from moviepy.audio.tools.cuts import find_audio_period
import os

In [30]:
INPUT_FOLDER = 'data/'
OUTPUT_FOLDER = 'output/'
names = ["knights.mp4", "frontier.mp4"]
fullNames = [INPUT_FOLDER + name for name in names]
if not (os.path.exists(fullNames[0]) and os.path.exists(fullNames[1])):
    os.system("youtube-dl zvCvOC2VwDc -o " + fullNames[0])
    os.system("youtube-dl lkY3Ek9VPtg -o" + fullNames[1])

In [31]:
audio = (AudioFileClip(fullNames[1])
         .subclip((4,7), (4,18))
         .audio_fadein(1)
         .audio_fadeout(1))

audio_period = find_audio_period(audio)
print ('Analyzed the audio, found a period of %.02f seconds'% audio_period )


Analyzed the audio, found a period of 0.48 seconds

In [32]:
clip = (VideoFileClip(fullNames[0], audio=False)
        .subclip((1,24.15),(1,26))
        .crop(x1=332, x2=910, y2=686))

video_period = find_video_period(clip, tmin=.3)
print ('Analyzed the video, found a period of %.02f seconds'% video_period)


Analyzed the video, found a period of 1.23 seconds

In [33]:
edited_right = (clip.subclip(0, video_period)
                .speedx(final_duration = 2*audio_period)
                .fx(vfx.loop, duration = audio.duration)
                .subclip(.25))

edited_left = edited_right.fx(vfx.mirror_x)

dancing_knights = (clips_array([[edited_left, edited_right]])
                   .fadein(1).fadeout(1).set_audio(audio).subclip(.3))

In [42]:
ipython_display(dancing_knights, width = 300)



Out[42]:

In [34]:
txt_title = (TextClip("15th century dancing\n(hypothetical)", fontsize=70,
               font="Arial", color="white")
             .margin(top=25, opacity=0)
             .set_position(("center","top")))

title = (CompositeVideoClip([dancing_knights.to_ImageClip(), txt_title])
         .fadein(.5)
         .set_duration(1.5))

In [35]:
txt_credits = """
CREDITS
Video excerpt: Le combat en armure au XVe siècle
By J. Donzé, D. Jaquet, T. Schmuziger,
Université de Genève, Musée National de Moyen Age
Music: "Frontier", by DOCTOR VOX
Under licence Creative Commons
https://www.youtube.com/user/DOCTORVOXofficial
Video editing © Zulko 2014
 Licence Creative Commons (CC BY 4.0)
Edited with MoviePy: http://zulko.github.io/moviepy/
"""

credits = (TextClip(txt_credits, color='white',
            font="Century-Schoolbook-Roman", fontsize=35, kerning=-2,
            interline=-1, bg_color='black', size=title.size)
          .set_duration(2.5)
          .fadein(.5)
          .fadeout(.5))

In [43]:
final = concatenate_videoclips([title, dancing_knights, credits])

final.write_videofile("dancing_knights.mp4", fps=clip.fps, codec='libx264', 
                  audio_codec='aac', temp_audiofile='temp-audio.m4a', 
                  remove_temp=True, audio_bitrate="1000k", bitrate="4000k")


[MoviePy] >>>> Building video dancing_knights.mp4
[MoviePy] Writing audio in temp-audio.m4a
                                                  
[MoviePy] Done.
[MoviePy] Writing video dancing_knights.mp4
                                                 
[MoviePy] Done.
[MoviePy] >>>> Video ready: dancing_knights.mp4 



In [44]:
ipython_display("dancing_knights.mp4", width=300)


Out[44]:

In [38]:
ipython_display(audio)



Out[38]: