swapped enumerate and track to avoid a track error

pull/347/head
CordlessCoder 3 years ago
parent 1462b1d9b6
commit 6cfd4b19ec

@ -1,9 +1,12 @@
from rich.console import Console from rich.console import Console
from utils.console import print_markdown, print_step, print_substep from utils.console import print_step, print_substep
from dotenv import load_dotenv from dotenv import load_dotenv
import os
import random
import praw
import re
console = Console() console = Console()
import os, random, praw, re
def get_subreddit_threads(): def get_subreddit_threads():
@ -38,7 +41,7 @@ def get_subreddit_threads():
if not os.getenv("RANDOM_THREAD") or os.getenv("RANDOM_THREAD") == "no": if not os.getenv("RANDOM_THREAD") or os.getenv("RANDOM_THREAD") == "no":
print_substep("Insert the full thread link:", style="bold green") print_substep("Insert the full thread link:", style="bold green")
thread_link = input() thread_link = input()
print_step(f"Getting the inserted thread...") print_step("Getting the inserted thread...")
submission = reddit.submission(url=thread_link) submission = reddit.submission(url=thread_link)
else: else:
# Otherwise, picks a random thread from the inserted subreddit # Otherwise, picks a random thread from the inserted subreddit
@ -48,7 +51,11 @@ def get_subreddit_threads():
# ! Prompt the user to enter a subreddit # ! Prompt the user to enter a subreddit
try: try:
subreddit = reddit.subreddit( subreddit = reddit.subreddit(
re.sub(r"r\/", "",input("What subreddit would you like to pull from? ")) re.sub(
r"r\/",
"",
input("What subreddit would you like to pull from? "),
)
) )
except ValueError: except ValueError:
subreddit = reddit.subreddit("askreddit") subreddit = reddit.subreddit("askreddit")
@ -75,7 +82,7 @@ def get_subreddit_threads():
} }
) )
except AttributeError as e: except AttributeError:
pass pass
print_substep("Received AskReddit threads successfully.", style="bold green") print_substep("Received AskReddit threads successfully.", style="bold green")

@ -13,6 +13,7 @@ def get_start_and_end_times(video_length, length_of_clip):
random_time = randrange(180, int(length_of_clip) - int(video_length)) random_time = randrange(180, int(length_of_clip) - int(video_length))
return random_time, random_time + video_length return random_time, random_time + video_length
def download_background(): def download_background():
"""Downloads the background video from youtube. """Downloads the background video from youtube.

@ -7,7 +7,7 @@ from moviepy.editor import (
CompositeAudioClip, CompositeAudioClip,
CompositeVideoClip, CompositeVideoClip,
) )
import reddit.subreddit import reddit.subreddit
import re import re
from utils.console import print_step from utils.console import print_step
from dotenv import load_dotenv from dotenv import load_dotenv
@ -16,13 +16,12 @@ import os
W, H = 1080, 1920 W, H = 1080, 1920
def make_final_video(number_of_clips): def make_final_video(number_of_clips):
# Calls opacity from the .env # Calls opacity from the .env
load_dotenv() load_dotenv()
opacity = os.getenv('OPACITY') opacity = os.getenv("OPACITY")
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)
@ -38,10 +37,10 @@ def make_final_video(number_of_clips):
# Gather all audio clips # Gather all audio clips
audio_clips = [] audio_clips = []
for i in range(0, number_of_clips): for i in range(0, number_of_clips):
audio_clips.append(AudioFileClip(f"assets/mp3/{i}.mp3")) audio_clips.append(AudioFileClip("assets/mp3/{i}.mp3"))
audio_clips.insert(0, AudioFileClip(f"assets/mp3/title.mp3")) audio_clips.insert(0, AudioFileClip("assets/mp3/title.mp3"))
try: try:
audio_clips.insert(1, AudioFileClip(f"assets/mp3/posttext.mp3")) audio_clips.insert(1, AudioFileClip("assets/mp3/posttext.mp3"))
except: except:
OSError() OSError()
audio_concat = concatenate_audioclips(audio_clips) audio_concat = concatenate_audioclips(audio_clips)
@ -57,30 +56,32 @@ def make_final_video(number_of_clips):
.resize(width=W - 100) .resize(width=W - 100)
.set_opacity(float(opacity)), .set_opacity(float(opacity)),
) )
if os.path.exists(f"assets/mp3/posttext.mp3"): if os.path.exists("assets/mp3/posttext.mp3"):
image_clips.insert( image_clips.insert(
0, 0,
ImageClip(f"assets/png/title.png") ImageClip("assets/png/title.png")
.set_duration(audio_clips[0].duration + audio_clips[1].duration) .set_duration(audio_clips[0].duration + audio_clips[1].duration)
.set_position("center") .set_position("center")
.resize(width=W - 100) .resize(width=W - 100)
.set_opacity(float(opacity)), .set_opacity(float(opacity)),
) )
else: else:
image_clips.insert( image_clips.insert(
0, 0,
ImageClip(f"assets/png/title.png") ImageClip("assets/png/title.png")
.set_duration(audio_clips[0].duration) .set_duration(audio_clips[0].duration)
.set_position("center") .set_position("center")
.resize(width=W - 100) .resize(width=W - 100)
.set_opacity(float(opacity)), .set_opacity(float(opacity)),
) )
image_concat = concatenate_videoclips(image_clips).set_position( image_concat = concatenate_videoclips(image_clips).set_position(
("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])
filename = (re.sub('[?\"%*:|<>]', '', ("assets/" + reddit.subreddit.submission.title + ".mp4"))) filename = re.sub(
'[?"%*:|<>]', "", ("assets/" + reddit.subreddit.submission.title + ".mp4")
)
final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k") final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k")
for i in range(0, number_of_clips): for i in range(0, number_of_clips):
pass pass

@ -24,7 +24,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
context = browser.new_context() context = browser.new_context()
if theme.casefold() == "dark": if theme.casefold() == "dark":
cookie_file = open('video_creation/cookies.json') cookie_file = open("video_creation/cookies.json")
cookies = json.load(cookie_file) cookies = json.load(cookie_file)
context.add_cookies(cookies) context.add_cookies(cookies)
@ -42,8 +42,8 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
path="assets/png/title.png" path="assets/png/title.png"
) )
for idx, comment in track( for idx, comment in enumerate(
enumerate(reddit_object["comments"]), "Downloading screenshots..." track(reddit_object["comments"]), "Downloading screenshots..."
): ):
# Stop if we have reached the screenshot_num # Stop if we have reached the screenshot_num
@ -58,5 +58,4 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
path=f"assets/png/comment_{idx}.png" path=f"assets/png/comment_{idx}.png"
) )
print_substep("Screenshots downloaded Successfully.", print_substep("Screenshots downloaded Successfully.", style="bold green")
style="bold green")

@ -18,20 +18,20 @@ def save_text_to_mp3(reddit_obj):
Path("assets/mp3").mkdir(parents=True, exist_ok=True) Path("assets/mp3").mkdir(parents=True, exist_ok=True)
tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False) tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False)
tts.save(f"assets/mp3/title.mp3") tts.save("assets/mp3/title.mp3")
length += MP3(f"assets/mp3/title.mp3").info.length length += MP3("assets/mp3/title.mp3").info.length
try: try:
Path(f"assets/mp3/posttext.mp3").unlink() Path("assets/mp3/posttext.mp3").unlink()
except OSError as e: except OSError:
pass pass
if reddit_obj["thread_post"] != "": if reddit_obj["thread_post"] != "":
tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False) tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False)
tts.save(f"assets/mp3/posttext.mp3") tts.save("assets/mp3/posttext.mp3")
length += MP3(f"assets/mp3/posttext.mp3").info.length length += MP3("assets/mp3/posttext.mp3").info.length
for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): for idx, comment in enumerate(track(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 # ! 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 > 50:
break break

Loading…
Cancel
Save