-
Notifications
You must be signed in to change notification settings - Fork 0
/
story_based_video.py
274 lines (220 loc) · 8.75 KB
/
story_based_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from datetime import timedelta
from random import randrange
import string
import subprocess
import sys
import textgrid
from typing import Literal, Tuple
from moviepy import *
from configuration import Configuration
from video_utils import (
check_if_valid_post,
crop_to_center_and_resize,
generate_intro_clip,
generate_outro_clip,
select_background_video,
)
from openai_interface import OpenAiInterface
from reddit_requests import Post, PostSearch
config = Configuration()
mfa_dictionary_names = {
"english": ["english_us_arpa", "english_us_arpa"],
"german": ["german_mfa", "german_mfa"],
}
class Timestamp:
def __init__(self, text: str, from_time: float, to_time: float) -> None:
self.text = text
self.from_time = from_time
self.to_time = to_time
def generate_story_clip(
post: Post,
resolution: Tuple[int, int],
language: str,
add_intro: bool,
add_outro: bool,
) -> VideoClip:
text: str = post.selftext
intro: VideoClip | None = None
outro: VideoClip | None = None
if add_intro:
intro = generate_intro_clip(post, resolution)
if add_outro:
outro = generate_outro_clip(post, resolution)
# print("SKIPPING GENERATING AUDIO")
openaiinterface = OpenAiInterface()
print("generating audio")
openaiinterface.generate_mp3(text, f"tmp/{post.post_id}-audio.mp3")
audio_clip: AudioClip = AudioFileClip(f"tmp/{post.post_id}-audio.mp3")
audio_clip.write_audiofile(f"tmp/{post.post_id}-audio.wav")
video_duration = audio_clip.duration
if intro != None:
video_duration += intro.duration
if outro != None:
video_duration += outro.duration
print(f"the video will be {video_duration}s long")
with open(f"tmp/{post.post_id}-audio.txt", "w", encoding="utf-8") as file:
exclude = set(string.punctuation)
file.write("".join(char for char in text if char not in exclude))
align_audio_and_text(
f"tmp/{post.post_id}-audio.wav", f"tmp/{post.post_id}-audio.txt", language
)
combined_text_clip: VideoClip = generate_combined_text_clip(
text, resolution, f"tmp/{post.post_id}-audio.TextGrid"
)
combined_text_clip = combined_text_clip.with_audio(audio_clip)
to_combine = [clip for clip in [intro, combined_text_clip, outro] if clip != None]
combined_text_clip = concatenate_videoclips(to_combine)
combined_text_clip = combined_text_clip.with_position("center")
return combined_text_clip
def align_audio_and_text(audiofile: str, textfile: str, language: str):
dictionary_name, acoustic_model_name = mfa_dictionary_names[language]
result = subprocess.run(
[
"mfa",
"align_one",
audiofile,
textfile,
dictionary_name,
acoustic_model_name,
"tmp/",
"--clean",
"--single_speaker",
]
)
try:
result.check_returncode()
except subprocess.CalledProcessError:
raise Exception("Alignment failed")
def remove_differences_to_textgrid(filtered_tg, text: str) -> str:
words = text.split()
for i in range(0, len(filtered_tg)):
look_for_matches = []
for offset in range(i, min(i + 3, len(words))):
word = "".join(
[
letter
for letter in words[offset]
if letter not in set(string.punctuation)
]
)
look_for_matches.append(word.lower())
# print(f"{i} looking for match of {filtered_tg[i].mark} in {look_for_matches}")
correct_match_index = look_for_matches.index(str(filtered_tg[i].mark).lower())
# print(f"looking for {filtered_tg[i].mark} in {look_for_matches}, choosing {look_for_matches[correct_match_index]}")
for delete_index in range(i, i + correct_match_index):
print(f"deleting words {words[delete_index]}")
del words[delete_index]
# print(f"correct match {correct_match_index} {filtered_tg[i].mark} == {words[i]}")
return " ".join(words)
def generate_timestamps(filename, text: str) -> list[Timestamp]:
tg = textgrid.TextGrid.fromFile(filename)
# tg[0] is the list of words
# filter to remove pauses
filtered_tg = filter(
lambda x: not x.mark == "" and not x.mark.startswith("<"), tg[0]
)
filtered_tg = list(filtered_tg)
print(f"filtered_tg is {len(filtered_tg)} long")
text = remove_differences_to_textgrid(filtered_tg, text)
text_segments = generate_text_list(text)
timestamps: list[Timestamp] = []
for index, segment in enumerate(text_segments[:-1]):
from_index = sum(len(s.split()) for s in text_segments[:index])
to_index = sum(len(s.split()) for s in text_segments[: index + 1])
start_time = filtered_tg[from_index].minTime
end_time = filtered_tg[to_index].maxTime
# print(f"from_index {from_index}={words[from_index]}, to_index {to_index}={words[to_index]}")
timestamps.append(Timestamp(segment, start_time, end_time))
last_timestamp_start_time = filtered_tg[
sum(len(s.split()) for s in text_segments[:-1])
].minTime
last_timestamp_end_time = filtered_tg[-1].maxTime
timestamps.append(
Timestamp(text_segments[-1], last_timestamp_start_time, last_timestamp_end_time)
)
# making sure the timestamps dont overlap
for index in range(1, len(timestamps)):
if timestamps[index].from_time < timestamps[index - 1].to_time:
# print(f"changing to_time of index {index-1} from {timestamps[index - 1].to_time} to {timestamps[index].from_time-0.01}")
timestamps[index - 1].to_time = timestamps[index].from_time - 0.01
return timestamps
def generate_combined_text_clip(
text: str, resolution: Tuple[float, float], textgrid_filename: str
):
text_clips: list[TextClip] = []
timestamps: list[Timestamp] = generate_timestamps(textgrid_filename, text)
text_box_size = (resolution[0] * 0.8, 0)
for section in timestamps:
text_clip: TextClip = generate_text_clip(section.text, text_box_size)
text_clip = text_clip.with_start(section.from_time)
text_clip = text_clip.with_end(section.to_time)
print(
f"{section.text} is played from {section.from_time:.2f} to {section.to_time:.2f} seconds"
)
text_clip = text_clip.with_position("center")
text_clips.append(text_clip)
return CompositeVideoClip(text_clips, resolution).with_position("center")
def generate_text_clip(text: str, size: Tuple[float, float]):
# good fonts:
# arial-black
# cooper-black
# franklin-gothic-heavy
return TextClip(
text=text,
method="caption",
color=config.video_font_color,
font=config.video_font,
font_size=config.video_font_size,
stroke_color=config.video_font_stroke_color,
stroke_width=config.video_font_stroke_width,
size=(size[0], size[1]),
align="center",
)
def generate_text_list(text: str):
words: list[str] = [x.strip() for x in text.split(" ")]
text_list: list[str] = []
num_words = []
while len(words) > 0: # split into chunks of different lengths
length = min(randrange(6, 9), len(words))
num_words.append(length)
words = words[length:]
if num_words[-1] < 3: # is the last text too short? add it to the previous one
num_words[-2] += num_words[-1]
num_words.pop()
sum = 0
words = [x.strip() for x in text.split(" ")]
for num in num_words:
text_list.append(" ".join(words[sum : sum + num]))
sum += num
return text_list
def find_story_post(
timeframe: Literal["day", "week", "month", "year", "all"],
listing: Literal["controversial", "best", "hot", "new", "random", "rising", "top"],
subreddit_list: list[str],
approx_video_duration: timedelta | None = None,
min_duration: timedelta | None = None,
max_duration: timedelta | None = None
):
selected_post = None
max_attempts = 50
for i in range(0, max_attempts):
subreddit = subreddit_list[randrange(0, len(subreddit_list))]
search = PostSearch(subreddit, listing, timeframe)
if not hasattr(search, "posts") or len(search.posts) < 1:
continue
p = search.posts[randrange(0, len(search.posts))]
valid = check_if_valid_post(
p.post_id,
p.title,
p.selftext,
p.nsfw,
approx_video_duration=approx_video_duration,
min_duration=min_duration,
max_duration=max_duration,
)
if valid:
selected_post = p
break
if selected_post == None:
raise Exception(f"No valid post found in {max_attempts} attempts.")
return selected_post