-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.py
75 lines (56 loc) · 1.68 KB
/
video.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
from moviepy.editor import (
TextClip,
VideoFileClip,
ImageClip,
concatenate_videoclips,
CompositeVideoClip,
)
def formatISP(asn):
words = asn.split(" ", 1)
return " ".join(words[1:])
def animate_text(lines, delay, duration):
clips = []
for i in enumerate(lines):
text_lines = []
for y in range(i[0] + 1):
text_lines.append(lines[y])
clip = TextClip(
"\n".join(text_lines),
color="white",
stroke_color="black",
stroke_width=1,
size=(500, 500),
method="caption",
).set_duration(delay)
clips.append(clip)
total_duration = len(lines) * delay
clips[-1] = clips[-1].set_duration(duration - total_duration + delay)
final_clip = concatenate_videoclips(clips)
return final_clip
def create_video(data):
lines = [
f"IP: {data['query']}",
f"N: {data['lat']}",
f"E: {data['lon']}",
f"Country: {data['country']}",
f"City: {data['city']}",
f"ZIP: {data['zip']}",
f"ISP: {formatISP(data['as'])}",
]
fps = 30
background = VideoFileClip("background.mp4")
delay = 0.3
animation = animate_text(lines, delay, background.duration)
poster = ImageClip("poster.jpg").set_duration(1 / fps)
final_clip = concatenate_videoclips(
[
poster,
CompositeVideoClip(
[
background,
animation.set_position("center"),
]
),
]
).set_duration(background.duration)
final_clip.write_videofile(f"storage/{data['id']}.mp4", fps=fps, logger=None)