From 96ffdf21d3643e672e4fdfaea3f720dcc80e261f Mon Sep 17 00:00:00 2001 From: Jacob Chambers Date: Sat, 4 Jun 2022 20:25:55 +0100 Subject: [PATCH] Changes --- main.py | 45 ++++++++++++++++++++++++----------- output/.keep | 1 - reddit/subreddit.py | 9 ++++--- video_creation/final_video.py | 8 ++++--- video_creation/voices.py | 5 +++- 5 files changed, 44 insertions(+), 24 deletions(-) delete mode 100644 output/.keep diff --git a/main.py b/main.py index 4235de4..5a3ebb6 100644 --- a/main.py +++ b/main.py @@ -11,32 +11,49 @@ import os import re 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( "### 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) +load_dotenv() -while True: - reddit_object = get_subreddit_threads() +for thread_number in range(int(os.getenv("LIMIT"))): + reddit_object = get_subreddit_threads(thread_number) thread_title = str.strip(reddit_object["thread_title"]).replace(" ", "_") thread_title = re.sub(r"[^_\w]", "", thread_title) - file_name = f"{os.getenv('SUBREDDIT')}-{thread_title}.mp4" - file_exists = os.path.isfile(f"output/{file_name}") + thread_url_elements = reddit_object['thread_url'].split('/') + 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: - load_dotenv() + if file_exists is False and fail_exists is False: length, number_of_comments = save_text_to_mp3(reddit_object) - 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_background() - chop_background_video(length) - final_video = make_final_video(number_of_comments, name=f"{os.getenv('SUBREDDIT')}-{thread_title}") + 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.") + download_screenshots_of_reddit_posts(reddit_object, number_of_comments, os.getenv("THEME")) + download_background() + chop_background_video(length) + 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: print(f"Video for thread already exists! Trying again...") - print(os.getenv("LOOP")) - if os.getenv("LOOP") != "yes": - break + break \ No newline at end of file diff --git a/output/.keep b/output/.keep deleted file mode 100644 index 8b13789..0000000 --- a/output/.keep +++ /dev/null @@ -1 +0,0 @@ - diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 5d020fe..eae42a4 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -4,8 +4,7 @@ import random from dotenv import load_dotenv import os - -def get_subreddit_threads(): +def get_subreddit_threads(thread_number): """ Returns a list of threads from the AskReddit subreddit. @@ -48,8 +47,8 @@ def get_subreddit_threads(): subreddit = reddit.subreddit("askreddit") print_substep("Subreddit not defined. Using AskReddit.") - threads = subreddit.hot(limit=25) - submission = list(threads)[random.randrange(0, 25)] + threads = subreddit.top(time_filter="month", limit=int(os.getenv("LIMIT"))) + submission = list(threads)[thread_number] print_substep(f"Video will be: {submission.title} :thumbsup:") try: @@ -57,7 +56,7 @@ def get_subreddit_threads(): content["thread_title"] = submission.title content["comments"] = [] - for top_level_comment in submission.comments: + for top_level_comment in submission.comments.top(time_filter="all"): content["comments"].append( { "comment_body": top_level_comment.body, diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 6eabff1..3ffe86e 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -13,7 +13,7 @@ from utils.console import print_step 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...") VideoFileClip.reW = lambda clip: clip.resize(width=W) VideoFileClip.reH = lambda clip: clip.resize(width=H) @@ -52,9 +52,11 @@ def make_final_video(number_of_clips, name): ("center", "center") ) image_concat.audio = audio_composite - final = CompositeVideoClip([background_clip, image_concat]) + final = CompositeVideoClip([background_clip, image_concat]).subclip(0, length) + 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): diff --git a/video_creation/voices.py b/video_creation/voices.py index 7678d70..2046c0a 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -30,7 +30,10 @@ def save_text_to_mp3(reddit_obj): for i, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): tts = gTTS(text=comment["comment_body"], tld=os.getenv("ACCENT"), slow=False) 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 else: break