-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrttsvg.py
More file actions
315 lines (285 loc) · 14.2 KB
/
rttsvg.py
File metadata and controls
315 lines (285 loc) · 14.2 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import argparse
from datetime import datetime
from gtts import gTTS
import json
import math
import os
from PIL import Image, ImageDraw, ImageFont
import random
import re
import requests
import shutil
import subprocess
import textwrap
import time
REQUESTS_HEADER = {"User-agent": "Mozilla/5.0 (Linux; U; Android 2.2; de-de; HTC Magic Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"}
with open("filter.json") as f:
FILTER = json.load(f)
with open("config.json") as f:
config = json.load(f)
INTRO_FILE = config["intro"]
OUTRO_FILE = config["outro"]
TRANSITION_FILE = config["transition"]
SUBREDDITS = config["subreddits"]
MUSIC_FOLDER = config["music_folder"]
MUSIC_VOLUME = config["music_volume"]
SORT_BY = config["sort_by"]
COUNT = config["count"]
MIN_LENGTH = config["min_video_length"]
UPVOTE_THRESHOLD = config["upvote_threshold"]
TTS_COOLDOWN = config["tts_cooldown"]
VOICE = config["voice"]
PROFANITY_FILTER = config["ad_friendly_filter"]
NSFW_FILTER = config["include_nsfw_posts"]
ENDLESS = True if COUNT < 1 else False
VIDEO_CODEC = config["ffmpeg_c:v"]
AUDIO_CODEC = config["ffmpeg_c:a"]
PIXEL_FORMAT = config["ffmpeg_pix_fmt"]
AUDIO_SAMPLING_RATE = config["ffmpeg_ar"]
MIN_COMMENT_LENGTH = config["min_comment_length"]
MAX_COMMENT_LENGTH = config["max_comment_length"]
SILENT = config["silent"]
HEADER_FONT = ImageFont.truetype("font.ttf", 26)
TEXT_FONT = ImageFont.truetype("font.ttf", 34)
COMMENT_COUNTER = -1
LENGTH = 0
# Subject to improvement
def log(msg, bypass_silent: bool):
if not SILENT or bypass_silent:
print(msg)
def pick_reddit_post():
global NSFW_FILTER
askreddit_json = requests.get("https://www.reddit.com/r/%s/%s.json?count=100" % (random.choice(SUBREDDITS), SORT_BY), headers=REQUESTS_HEADER).text
potential_posts_list = json.loads(askreddit_json)["data"]["children"]
for p in potential_posts_list:
if p["data"]["pinned"] or p["data"]["stickied"] or (p["data"]["over_18"] and NSFW_FILTER) or p["data"]["ups"] < UPVOTE_THRESHOLD:
potential_posts_list.remove(p)
picked_post_url = random.choice(potential_posts_list)["data"]["url"]
picked_post = requests.get(picked_post_url[:-1] + ".json", headers=REQUESTS_HEADER).text
picked_post_json = json.loads(picked_post)
picked_post_json[0]["data"]["children"][0]["data"]["title"] = filter(picked_post_json[0]["data"]["children"][0]["data"]["title"])
return picked_post_json
def generate_TTS(text):
global LENGTH
filename = os.path.join("tts", "%s.mp3" % str(COMMENT_COUNTER))
edited_text = text.replace("*", "").replace("_", "")
if VOICE.lower() == "google":
log("Generating TTS (Google)", False)
tts = gTTS(text)
tts.save(filename)
else:
log("Generating TTS (Brian)", False)
tts = requests.get("https://api.streamelements.com/kappa/v2/speech?voice=Brian&text=%s" % text)
with open(filename, "wb") as f:
f.write(tts.content)
LENGTH += get_length(filename)
if TRANSITION_FILE is not None:
LENGTH += get_length(TRANSITION_FILE)
# thanks to SingleNegationElimination on StackOverflow!
def get_length(filename):
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return float(result.stdout)
def filter(text):
text = re.sub(r"\[(.+)\]\(.+\)", r"\1", text)
if PROFANITY_FILTER:
for a in FILTER.keys():
text = text.replace(a, FILTER[a])
return text
def truncate(text):
new_text = text.replace("\n", " ")
if len(text) > 128:
new_text = new_text[:128]
new_text += "..."
return new_text
def generate_image(author, points, text, title=False):
log("Generating image", False)
header = "u/%s | %s points" % (author, str(points))
if points >= 1000:
header = "u/%s | %.1fk points" % (author, points/1000)
background = Image.new("RGBA", (1920, 1080), (26, 26, 26, 255))
draw = ImageDraw.Draw(background)
# we have to do this weird replace thing because textwrap deletes exisiting newlines
text_block = "\n".join(textwrap.wrap(text.replace("\n", "#=#"), width=120)).replace("#=#", "\n")
header_size = draw.textsize(header, HEADER_FONT)
text_size = draw.textsize(text_block, TEXT_FONT)
total_size = (max(header_size[0], text_size[0]), header_size[1] + text_size[1] + 12)
text_im = Image.new("RGBA", total_size, (26, 26, 26, 255))
text_draw = ImageDraw.Draw(text_im);
text_draw.text((0, 0), header, font=HEADER_FONT, fill=(86, 86, 86, 255))
text_draw.text((0, header_size[1]), text_block, font=TEXT_FONT, fill=(255, 255, 255, 255))
background.paste(text_im, (int((background.size[0] - total_size[0]) / 2), int((background.size[1] - total_size[1]) / 2)))
background.save(os.path.join("img", "%s.png" % str(COMMENT_COUNTER)))
# master function that generates the whole video
def generate_video():
global COMMENT_COUNTER
global LENGTH
global MIN_LENGTH
global REMOVE_TMP
used_numbers = [-1]
if not os.path.exists("tts"):
os.mkdir("tts")
if not os.path.exists("img"):
os.mkdir("img")
if not os.path.exists("vid"):
os.mkdir("vid")
start_time = datetime.now()
if INTRO_FILE is not None:
LENGTH += get_length(INTRO_FILE)
if OUTRO_FILE is not None:
LENGTH += get_length(OUTRO_FILE)
if TRANSITION_FILE is not None:
LENGTH += get_length(TRANSITION_FILE)
reddit_post = pick_reddit_post()
log("Picked thread \"%s\"" % reddit_post[0]["data"]["children"][0]["data"]["title"], False)
generate_image(filter(reddit_post[0]["data"]["children"][0]["data"]["author"]), reddit_post[0]["data"]["children"][0]["data"]["ups"], filter(reddit_post[0]["data"]["children"][0]["data"]["title"]))
generate_TTS(reddit_post[0]["data"]["children"][0]["data"]["title"])
log("Length reached: %s seconds" % str(LENGTH), False)
log("Timeout for %s seconds to avoid TTS rate-limiting" % str(TTS_COOLDOWN), False)
time.sleep(TTS_COOLDOWN)
COMMENT_COUNTER += 1
while LENGTH < MIN_LENGTH:
comment = reddit_post[1]["data"]["children"][COMMENT_COUNTER]
COMMENT_COUNTER += 1
if COMMENT_COUNTER >= len(reddit_post[1]["data"]["children"]):
log("Out of comments!", False)
break
try:
if comment["data"]["author"] == "AutoModerator":
log("Discarding AutoModerator's comment", False)
continue
# if this happens, we reached a weird comment-like thing in the comments marking the end
except:
log("Out of comments!", False)
break
if len(comment["data"]["body"]) > MAX_COMMENT_LENGTH:
log("Discarding comment #%s because it is too long" % COMMENT_COUNTER, False)
continue
if len(comment["data"]["body"]) < MIN_COMMENT_LENGTH:
log("Discarding comment #%s because it is too short" % COMMENT_COUNTER, False)
continue
comment_text = filter(comment["data"]["body"])
log("Picked comment #%s: %s" % (COMMENT_COUNTER, truncate(comment_text)), False)
generate_image(filter(comment["data"]["author"]), comment["data"]["ups"], comment_text)
generate_TTS(comment_text)
log("Length reached: %s seconds" % str(LENGTH), False)
used_numbers.append(COMMENT_COUNTER)
log("Timeout for %s seconds to avoid TTS rate-limiting" % str(TTS_COOLDOWN), False)
time.sleep(TTS_COOLDOWN)
# stitch it all together
# first we want to make clips of every comment, then we stich all those clips together (including intro, outro and transition if applicable)
intro_conv = os.path.join("vid", "intro.flv")
transition_conv = os.path.join("vid", "transition.flv")
outro_conv = os.path.join("vid", "outro.flv")
with open("content_list.txt", "a") as f:
log("Creating clips out of the comments", False)
if TRANSITION_FILE is not None and not os.path.exists(transition_conv):
f.write("file '%s'\n" % transition_conv)
for i in used_numbers:
if(os.path.exists(os.path.join("tts", "%s.mp3" % str(i)))):
log("Building clip #" + str(i), False)
img_path = os.path.join("img", "%s.png" % str(i))
tts_path = os.path.join("tts", "%s.mp3" % str(i))
vid_path = os.path.join("vid", "%s.flv" % str(i))
subprocess.run(["ffmpeg", "-y", "-loop", "1", "-i", img_path,
"-i", tts_path, "-c:v", VIDEO_CODEC, "-tune", "stillimage", "-c:a", AUDIO_CODEC, "-ar", AUDIO_SAMPLING_RATE, "-shortest", "-pix_fmt", PIXEL_FORMAT,
"-r", "30", "-t", str(get_length(tts_path)), vid_path],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
f.write("file '%s'\n" % vid_path)
if TRANSITION_FILE is not None and os.path.exists(TRANSITION_FILE):
f.write("file '%s'\n" % transition_conv)
if INTRO_FILE is not None and not os.path.exists(intro_conv):
log("Converting intro into the same format", False)
subprocess.run(["ffmpeg", "-y", "-i", INTRO_FILE, "-vf", "scale=1920:1080", "-aspect", "1920:1080",
"-c:v", VIDEO_CODEC, "-s", "1920x1080", "-c:a", AUDIO_CODEC, "-ar", AUDIO_SAMPLING_RATE, "-shortest", "-pix_fmt", PIXEL_FORMAT, "-r", "30",
intro_conv],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
if TRANSITION_FILE is not None and not os.path.exists(transition_conv):
log("Converting transition into the same format", False)
subprocess.run(["ffmpeg", "-y", "-i", TRANSITION_FILE, "-vf", "scale=1920:1080", "-aspect", "1920:1080",
"-c:v", VIDEO_CODEC, "-s", "1920x1080", "-c:a", AUDIO_CODEC, "-ar", AUDIO_SAMPLING_RATE, "-shortest", "-pix_fmt", PIXEL_FORMAT, "-r", "30",
transition_conv],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
if OUTRO_FILE is not None and not os.path.exists(outro_conv):
log("Converting outro into the same format", False)
subprocess.run(["ffmpeg", "-y", "-i", OUTRO_FILE, "-vf", "scale=1920:1080", "-aspect", "1920:1080",
"-c:v", VIDEO_CODEC, "-s", "1920x1080", "-c:a", AUDIO_CODEC, "-ar", AUDIO_SAMPLING_RATE, "-shortest", "-pix_fmt", PIXEL_FORMAT, "-r", "30",
outro_conv],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
log("Stitching the clips together", False)
subprocess.run(["ffmpeg", "-y", "-safe", "0", "-f", "concat", "-i", "content_list.txt", "-c", "copy", os.path.join("vid", "content.flv")],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
music_file = None
if MUSIC_FOLDER != None:
if os.path.isfile(MUSIC_FOLDER):
music_file = MUSIC_FOLDER
elif os.path.isdir(MUSIC_FOLDER):
music_file = os.path.join(MUSIC_FOLDER, random.choice(os.listdir(MUSIC_FOLDER)))
else:
log("Unable to locate music, skipping adding the music.", False)
if music_file != None:
log("Looping the music (unelegantly, sorry :/)", False)
vid_length = get_length(os.path.join("vid", "content.flv"))
loops = math.ceil(vid_length / get_length(music_file))
with open("music_loop.txt", "a") as f:
for i in range(0, loops):
f.write("file '%s'\n" % music_file)
subprocess.run(["ffmpeg", "-y", "-t", str(vid_length), "-f", "concat", "-safe", "0", "-i", "music_loop.txt", "-t", str(vid_length), "-filter:a", "volume=%s" % str(MUSIC_VOLUME), "-c:a", "aac", os.path.join("vid", "looped.m4a")],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
log("Putting the music into the video", False)
subprocess.run(["ffmpeg", "-y", "-i", os.path.join("vid", "content.flv"), "-i", os.path.join("vid", "looped.m4a"),
"-filter_complex", "[1:0]volume=1[a1];[0:a][a1]amix=inputs=2:duration=first", "-map", "0:v:0", "-c:v", "copy", "-c:a", "aac", os.path.join("vid", "content_music.flv")],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
os.remove(os.path.join("vid", "content.flv"))
os.rename(os.path.join("vid", "content_music.flv"), os.path.join("vid", "content.flv"))
# apparently we have to do this to "fix" the video?
subprocess.run(["ffmpeg", "-i", os.path.join("vid", "content.flv"), "-c", "copy", os.path.join("vid", "content_fixed.flv")],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
log("Finishing the video", False)
with open("list.txt", "a") as f:
if INTRO_FILE is not None:
f.write("file '%s'\n" % intro_conv)
f.write("file '%s'\n" % os.path.join("vid", "content_fixed.flv"))
if OUTRO_FILE is not None:
f.write("file '%s'\n" % outro_conv)
video_name = reddit_post[0]["data"]["children"][0]["data"]["title"] + ".flv"
subprocess.run(["ffmpeg", "-y", "-safe", "0", "-f", "concat", "-i", "list.txt", "-c", "copy", video_name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
log("Deleting temporary folders and files", False)
shutil.rmtree("tts")
shutil.rmtree("img")
shutil.rmtree("vid")
os.remove("list.txt")
os.remove("content_list.txt")
if os.path.exists("music_loop.txt"):
os.remove("music_loop.txt")
log("Done, and it only took %s! Have fun with your new video! :)" % str(datetime.now() - start_time), False)
log(os.path.abspath(video_name), True)
COMMENT_COUNTER = -1
LENGTH = 0
if __name__ == "__main__":
video_counter = 0
log("Starting...", False)
if ENDLESS:
log("Warning! Endless mode activated!", False)
while True:
log("Starting video #%s" % str(video_counter + 1), False)
generate_video()
video_counter += 1
else:
while video_counter < COUNT:
log("Starting video #%s" % str(video_counter + 1), False)
generate_video()
video_counter += 1