diff --git a/.gitignore b/.gitignore index 67cef4a..a4589e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ assets/ -.env \ No newline at end of file +.env +reddit-bot-351418-5560ebc49cac.json \ No newline at end of file diff --git a/main.py b/main.py index 8fb3d50..e1e5017 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +from utils.cleanup import cleanup from utils.console import print_markdown import time from reddit.askreddit import get_askreddit_threads @@ -20,3 +21,8 @@ download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_background() chop_background_video(length) final_video = make_final_video(number_of_comments) +cleanup() + +print_markdown( + "### File saved at assets/final_video.mp4 🎉. If you enjoyed this, make sure to give us a star on GitHub! 🌟" +) diff --git a/utils/cleanup.py b/utils/cleanup.py new file mode 100644 index 0000000..2f667e8 --- /dev/null +++ b/utils/cleanup.py @@ -0,0 +1,21 @@ +import os +import glob +from utils.console import print_step, print_substep + + +def cleanup(): + """ + Deletes all files that was used to create the final file. + """ + print_step("Cleaning up working files... 🗑") + files = glob.glob("assets/mp3/*") + for f in files: + os.remove(f) + + files = glob.glob("assets/png/*") + for f in files: + os.remove(f) + + os.remove("assets/mp4/clip.mp4") + + print_substep("Done! 🎉") diff --git a/video_creation/background.py b/video_creation/background.py index 20dbe73..a796136 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -17,6 +17,8 @@ def download_background(): Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) """ + # List of choices for the background video + background_choices = ["https://www.youtube.com/watch?v=n_Dv4JMiwK8"] if not Path("assets/mp4/background.mp4").is_file(): print_step( diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 947ab04..806568c 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -7,7 +7,7 @@ from moviepy.editor import ( CompositeAudioClip, CompositeVideoClip, ) -from utils.console import print_step +from utils.console import print_step, print_substep W, H = 1080, 1920 @@ -48,14 +48,16 @@ def make_final_video(number_of_clips): .set_position("center") .resize(width=W - 100), ) - image_concat = concatenate_videoclips(image_clips).set_position( - ("center", "center") + image_concat = concatenate_videoclips(image_clips, method="compose").set_position( + ("center", 0.3), relative=True ) image_concat.audio = audio_composite final = CompositeVideoClip([background_clip, image_concat]) final.write_videofile( - "assets/final_video.mp4", fps=30, audio_codec="aac", audio_bitrate="192k" + "assets/final_video.mp4", + fps=30, + audio_codec="aac", + audio_bitrate="192k", + preset="ultrafast", ) - - for i in range(0, number_of_clips): - pass + print_substep("WOOHOO! Video is saved at assets/final_video.mp4 🎉") diff --git a/video_creation/voices.py b/video_creation/voices.py index d719ff9..900da35 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -1,3 +1,4 @@ +import os from gtts import gTTS from pathlib import Path from mutagen.mp3 import MP3 @@ -12,18 +13,64 @@ def save_text_to_mp3(reddit_obj): reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. """ print_step("Saving Text to MP3 files 🎶") + + if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"): + from google.cloud import texttospeech + length = 0 # Create a folder for the mp3 files. Path("assets/mp3").mkdir(parents=True, exist_ok=True) + if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"): + client = texttospeech.TextToSpeechClient() + + thread_title = texttospeech.SynthesisInput(text=reddit_obj["thread_title"]) + voice = texttospeech.VoiceSelectionParams( + language_code="en-GB", name="en-GB-Wavenet-D" + ) + + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) + + response = client.synthesize_speech( + request={ + "input": thread_title, + "voice": voice, + "audio_config": audio_config, + } + ) + with open("assets/mp3/title.mp3", "wb") as out: + out.write(response.audio_content) + length += MP3(f"assets/mp3/title.mp3").info.length + + for idx, comment in track( + enumerate(reddit_obj["comments"]), "Saving comments to MP3 files" + ): + if length > 35: + break + comment_body = texttospeech.SynthesisInput(text=comment["comment_body"]) + response = client.synthesize_speech( + request={ + "input": comment_body, + "voice": voice, + "audio_config": audio_config, + } + ) + with open(f"assets/mp3/{idx}.mp3", "wb") as out: + out.write(response.audio_content) + length += MP3(f"assets/mp3/{idx}.mp3").info.length + + return length, idx + tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld="co.uk") tts.save(f"assets/mp3/title.mp3") length += MP3(f"assets/mp3/title.mp3").info.length for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): # ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point - if length > 50: + if length > 35: break tts = gTTS(text=comment["comment_body"], lang="en") tts.save(f"assets/mp3/{idx}.mp3")