forked from Zulko/moviepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdancing_knights.py
148 lines (109 loc) · 3.63 KB
/
dancing_knights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
"""
Result: https://www.youtube.com/watch?v=Qu7HJrsEYFg
This is how we can imagine knights dancing at the 15th century, based on a very
serious historical study here: https://www.youtube.com/watch?v=zvCvOC2VwDc
Here is what we do:
0- Get the video of a dancing knight, and a (Creative Commons) audio music file.
1- load the audio file and automatically find the tempo
2- load the video and automatically find a segment that loops well
3- extract this segment, slow it down so that it matches the audio tempo,
and make it loop forever.
4- Symmetrize this segment so that we will get two knights instead of one
5- Add a title screen and some credits, write to a file.
This example has been originally edited in an IPython Notebook, which makes it
easy to preview and fine-tune each part of the editing.
"""
import os
from moviepy.audio.tools.cuts import find_audio_period
from moviepy.editor import *
from moviepy.video.tools.cuts import find_video_period
# Next lines are for downloading the required videos from Youtube.
# To do this you must have youtube-dl installed, otherwise you will need to
# download the videos by hand and rename them, as follows:
# https://www.youtube.com/watch?v=zvCvOC2VwDc => knights.mp4
# https://www.youtube.com/watch?v=lkY3Ek9VPtg => frontier.mp4
if not os.path.exists("knights.mp4"):
os.system("youtube-dl zvCvOC2VwDc -o knights.mp4")
os.system("youtube-dl lkY3Ek9VPtg -o frontier.mp4")
# ==========
# LOAD, EDIT, ANALYZE THE AUDIO
audio = (
AudioFileClip("frontier.mp4")
.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)
# LOAD, EDIT, ANALYZE THE VIDEO
clip = (
VideoFileClip("./knights.mp4", audio=False)
.subclip((1, 24.15), (1, 26))
.crop(x1=332, x2=910, y2=686)
)
video_period = find_video_period(clip, tmin=0.3)
print("Analyzed the video, found a period of %.02f seconds" % video_period)
edited_right = (
clip.subclip(0, video_period)
.speedx(final_duration=2 * audio_period)
.fx(vfx.loop, duration=audio.duration)
.subclip(0.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(0.3)
)
# MAKE THE TITLE SCREEN
txt_title = (
TextClip(
"15th century dancing\n(hypothetical)",
fontsize=70,
font="Century-Schoolbook-Roman",
color="white",
)
.margin(top=15, opacity=0)
.set_position(("center", "top"))
)
title = (
CompositeVideoClip([dancing_knights.to_ImageClip(), txt_title])
.fadein(0.5)
.set_duration(3.5)
)
# MAKE THE CREDITS SCREEN
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(0.5)
.fadeout(0.5)
)
# ASSEMBLE EVERYTHING, WRITE TO FILE
final = concatenate_videoclips([title, dancing_knights, credits])
final.write_videofile(
"dancing_knights.mp4", fps=clip.fps, audio_bitrate="1000k", bitrate="4000k"
)