pull/246/head
Jacob Chambers 3 years ago
parent a3a9c7932f
commit 96ffdf21d3

@ -11,32 +11,49 @@ import os
import re import re
import math import math
def next_name(dir="output/"):
i = 0
for path in os.listdir(dir):
if path.endswith(".mp4"):
name = path.split(".")[0]
if name.isdigit():
name = int(name)
if i < name + 1:
i = name + 1
return i
print_markdown( print_markdown(
"### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue." "### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue."
) )
time.sleep(3) time.sleep(3)
load_dotenv()
while True: for thread_number in range(int(os.getenv("LIMIT"))):
reddit_object = get_subreddit_threads() reddit_object = get_subreddit_threads(thread_number)
thread_title = str.strip(reddit_object["thread_title"]).replace(" ", "_") thread_title = str.strip(reddit_object["thread_title"]).replace(" ", "_")
thread_title = re.sub(r"[^_\w]", "", thread_title) thread_title = re.sub(r"[^_\w]", "", thread_title)
file_name = f"{os.getenv('SUBREDDIT')}-{thread_title}.mp4" thread_url_elements = reddit_object['thread_url'].split('/')
file_exists = os.path.isfile(f"output/{file_name}") file_name = f"{thread_url_elements[4]}-{thread_url_elements[6]}"
file_exists = os.path.isfile(f"output/{file_name}.mp4")
fail_exists = os.path.isfile(f"output/fail/{file_name}")
if file_exists is False: if file_exists is False and fail_exists is False:
load_dotenv()
length, number_of_comments = save_text_to_mp3(reddit_object) length, number_of_comments = save_text_to_mp3(reddit_object)
if length >= int(os.getenv("MIN_VID")) and number_of_comments > 0:
print(f"Video is {math.ceil(length)}s long with {number_of_comments} comments.") print(f"Video is {math.ceil(length)}s long with {number_of_comments} comments.")
download_screenshots_of_reddit_posts(reddit_object, number_of_comments, os.getenv("THEME")) download_screenshots_of_reddit_posts(reddit_object, number_of_comments, os.getenv("THEME"))
download_background() download_background()
chop_background_video(length) chop_background_video(length)
final_video = make_final_video(number_of_comments, name=f"{os.getenv('SUBREDDIT')}-{thread_title}") final_video = make_final_video(number_of_comments, name=file_name, length=length)
else:
print("Content too short! Trying again...")
open(f"output/fail/{file_name}", "w+").close()
else: else:
print(f"Video for thread already exists! Trying again...") print(f"Video for thread already exists! Trying again...")
print(os.getenv("LOOP"))
if os.getenv("LOOP") != "yes": if os.getenv("LOOP") != "yes":
break break

@ -4,8 +4,7 @@ import random
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
def get_subreddit_threads(thread_number):
def get_subreddit_threads():
""" """
Returns a list of threads from the AskReddit subreddit. Returns a list of threads from the AskReddit subreddit.
@ -48,8 +47,8 @@ def get_subreddit_threads():
subreddit = reddit.subreddit("askreddit") subreddit = reddit.subreddit("askreddit")
print_substep("Subreddit not defined. Using AskReddit.") print_substep("Subreddit not defined. Using AskReddit.")
threads = subreddit.hot(limit=25) threads = subreddit.top(time_filter="month", limit=int(os.getenv("LIMIT")))
submission = list(threads)[random.randrange(0, 25)] submission = list(threads)[thread_number]
print_substep(f"Video will be: {submission.title} :thumbsup:") print_substep(f"Video will be: {submission.title} :thumbsup:")
try: try:
@ -57,7 +56,7 @@ def get_subreddit_threads():
content["thread_title"] = submission.title content["thread_title"] = submission.title
content["comments"] = [] content["comments"] = []
for top_level_comment in submission.comments: for top_level_comment in submission.comments.top(time_filter="all"):
content["comments"].append( content["comments"].append(
{ {
"comment_body": top_level_comment.body, "comment_body": top_level_comment.body,

@ -13,7 +13,7 @@ from utils.console import print_step
W, H = 1080, 1920 W, H = 1080, 1920
def make_final_video(number_of_clips, name): def make_final_video(number_of_clips, name, length):
print_step("Creating the final video...") print_step("Creating the final video...")
VideoFileClip.reW = lambda clip: clip.resize(width=W) VideoFileClip.reW = lambda clip: clip.resize(width=W)
VideoFileClip.reH = lambda clip: clip.resize(width=H) VideoFileClip.reH = lambda clip: clip.resize(width=H)
@ -52,9 +52,11 @@ def make_final_video(number_of_clips, name):
("center", "center") ("center", "center")
) )
image_concat.audio = audio_composite image_concat.audio = audio_composite
final = CompositeVideoClip([background_clip, image_concat]) final = CompositeVideoClip([background_clip, image_concat]).subclip(0, length)
final.write_videofile( final.write_videofile(
"output/" + name + ".mp4", fps=30, audio_codec="aac", audio_bitrate="64k" "output/" + name + ".mp4", fps=30, audio_codec="aac", audio_bitrate="64k",
threads=12
) )
for i in range(0, number_of_clips): for i in range(0, number_of_clips):

@ -30,7 +30,10 @@ def save_text_to_mp3(reddit_obj):
for i, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): for i, comment in track(enumerate(reddit_obj["comments"]), "Saving..."):
tts = gTTS(text=comment["comment_body"], tld=os.getenv("ACCENT"), slow=False) tts = gTTS(text=comment["comment_body"], tld=os.getenv("ACCENT"), slow=False)
tts.save(f"assets/mp3/{total_files}.mp3") tts.save(f"assets/mp3/{total_files}.mp3")
if total_length(total_files) < 60: clip_length = MP3(f"assets/mp3/{total_files}.mp3").info.length
print(clip_length)
combined_length = total_length(total_files)
if int(os.getenv("MIN_CLIP")) <= clip_length <= int(os.getenv("MAX_CLIP")) and combined_length <= int(os.getenv("MAX_VID")):
total_files += 1 total_files += 1
else: else:
break break

Loading…
Cancel
Save