Merge branch 'develop' into CordlessCoder-trackEnumfix

pull/347/head
CordlessCoder 3 years ago committed by GitHub
commit 0dc262870b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -51,11 +51,13 @@ In its current state, this bot does exactly what it needs to do. However, lots o
I have tried to simplify the code so anyone can read it and start contributing at any skill level. Don't be shy :) contribute!
- [x] Allowing users to choose a reddit thread instead of being randomized.
- [ ] Allowing users to choose a background that is picked instead of the Minecraft one.
- [x] Allowing users to choose between any subreddit.
- [ ] Allowing users to change voice.
- [ ] Creating better documentation and adding a command line interface.
To-Do:
- [x] Allowing users to choose a reddit thread instead of being randomized.
- [x] Allowing users to choose a background that is picked instead of the Minecraft one.
- [x] Allowing users to choose between any subreddit.
- [ ] Allowing users to change voice.
- [ ] Creating better documentation and adding a command line interface.
Please read our [contributing guidelines](CONTRIBUTING.md) for more detailed information.

@ -22,6 +22,29 @@ REQUIRED_VALUES = [
"OPACITY",
]
#Banner may look bad or wrong in IDE/Text Editor, but looks perfect in CMD, BASH or ZSH
banner = '''
'''
print(banner)
#Banner may look bad or wrong in IDE/Text Editor, but looks perfect in CMD, BASH or ZSH
time.sleep(.5)
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."
@ -43,7 +66,7 @@ reddit2fa = os.getenv("REDDIT_2FA")
load_dotenv()
console.log("[bold green]Checking environment variables...")
time.sleep(1)
time.sleep(.5)
if not os.path.exists(".env"):
@ -92,6 +115,9 @@ if configured:
download_screenshots_of_reddit_posts(
reddit_object, number_of_comments, os.getenv("THEME", "light")
)
download_background()
chop_background_video(length)
while True:
vidpath = download_background(length)
noerror = chop_background_video(length, vidpath)
if noerror is True:
break
final_video = make_final_video(number_of_comments)

@ -13,7 +13,7 @@ console = Console()
def get_subreddit_threads():
global submission
"""
Returns a list of threads from the AskReddit subreddit.
Returns a list of threads from the provided subreddit.
"""
load_dotenv()
@ -33,7 +33,7 @@ def get_subreddit_threads():
reddit = praw.Reddit(
client_id=os.getenv("REDDIT_CLIENT_ID"),
client_secret=os.getenv("REDDIT_CLIENT_SECRET"),
user_agent="Accessing AskReddit threads",
user_agent="Accessing subreddit threads",
username=os.getenv("REDDIT_USERNAME"),
password=passkey,
)
@ -64,9 +64,18 @@ def get_subreddit_threads():
threads = subreddit.hot(limit=25)
submission = list(threads)[random.randrange(0, 25)]
upvotes=submission.score
ratio=submission.upvote_ratio * 100
num_comments=submission.num_comments
print_substep(f"Video will be: {submission.title} :thumbsup:")
console.log(f"[bold green] Video will be: {submission.title} :thumbsup:")
console.log(f"[bold blue] Thread has " + str(upvotes) + " upvotes")
console.log(f"[bold blue] Thread has a upvote ratio of " + str(ratio) + "%")
console.log(f"[bold blue] Thread has " + str(num_comments) + " comments")
console.log("Getting video comments...")
try:
content["thread_url"] = submission.url
content["thread_title"] = submission.title

@ -8,46 +8,68 @@ from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import VideoFileClip
from utils.console import print_step, print_substep
import datetime
def get_start_and_end_times(video_length, length_of_clip):
random_time = randrange(180, int(length_of_clip) - int(video_length))
return random_time, random_time + video_length
def download_background(video_length):
def download_background():
"""Downloads the background video from youtube.
Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8)
"""
if not Path("assets/mp4/background.mp4").is_file():
print_substep("\nPut the URL of the video you want in the background.\nThe default video is a Minecraft parkour video.\n"
"Leave the input field blank to use the default.")
print_substep(f"Make sure the video is longer than {str(datetime.timedelta(seconds=round(video_length + 180)))}!\n", style="red")
inp = input("URL: ")
if not inp:
vidurl = "https://www.youtube.com/watch?v=n_Dv4JMiwK8"
else:
vidurl = inp
vidpath = vidurl.split("v=")[1]
if not Path(f"assets/mp4/{vidpath}.mp4").is_file():
print_step(
"We need to download the Minecraft background video. This is fairly large but it's only done once."
"We need to download the background video. This may be fairly large but it's only done once per background."
)
print_substep("Downloading the background video... please be patient.")
ydl_opts = {
"outtmpl": "assets/mp4/background.mp4",
"outtmpl": f"assets/mp4/{vidpath}.mp4",
"merge_output_format": "mp4",
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8")
ydl.download(vidurl)
print_substep("Background video downloaded successfully!", style="bold green")
return vidpath
def chop_background_video(video_length):
def chop_background_video(video_length, vidpath):
print_step("Finding a spot in the background video to chop...")
background = VideoFileClip("assets/mp4/background.mp4")
background = VideoFileClip(f"assets/mp4/{vidpath}.mp4")
if background.duration < video_length + 180:
print_substep("This video is too short.", style="red")
noerror = False
return noerror
start_time, end_time = get_start_and_end_times(video_length, background.duration)
ffmpeg_extract_subclip(
"assets/mp4/background.mp4",
f"assets/mp4/{vidpath}.mp4",
start_time,
end_time,
targetname="assets/mp4/clip.mp4",
)
print_substep("Background video chopped successfully!", style="bold green")
noerror = True
return noerror

@ -46,6 +46,14 @@ def make_final_video(number_of_clips):
OSError()
audio_concat = concatenate_audioclips(audio_clips)
audio_composite = CompositeAudioClip([audio_concat])
#Get sum of all clip lengths
total_length = sum([clip.duration for clip in audio_clips])
#round total_length to an integer
int_total_length=round(total_length)
#Output Length
console.log(f"[bold green] Video Will Be: {int_total_length} Seconds Long")
# Gather all images
image_clips = []

@ -4,6 +4,8 @@ from pathlib import Path
from rich.progress import track
from utils.console import print_step, print_substep
import json
from rich.console import Console
console = Console()
def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
@ -13,7 +15,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
reddit_object: The Reddit Object you received in askreddit.py
screenshot_num: The number of screenshots you want to download.
"""
print_step("Downloading Screenshots of Reddit Posts 📷")
print_step("Downloading screenshots of reddit posts...")
# ! Make sure the reddit screenshots folder exists
Path("assets/png").mkdir(parents=True, exist_ok=True)
@ -24,10 +26,13 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
browser = p.chromium.launch()
context = browser.new_context()
if theme.casefold() == "dark":
cookie_file = open("video_creation/cookies.json")
cookies = json.load(cookie_file)
context.add_cookies(cookies)
try:
if theme.casefold() == "dark":
cookie_file = open('video_creation/cookies.json')
cookies = json.load(cookie_file)
context.add_cookies(cookies)
except AttributeError:
pass
# Get the thread screenshot
page = context.new_page()
@ -48,6 +53,9 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
track(reddit_object["comments"]), "Downloading screenshots..."
):
#allow user to see what comment is being saved
print_substep(f"Downloading screenshot {idx + 1}")
# Stop if we have reached the screenshot_num
if idx >= screenshot_num:
break
@ -59,5 +67,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
page.locator(f"#t1_{comment['comment_id']}").screenshot(
path=f"assets/png/comment_{idx}.png"
)
print_substep("Screenshots downloaded Successfully.", style="bold green")
#let user know that the screenshots are done
console.log(f"[bold green]Saved {idx + 1} screenshots.")

@ -4,9 +4,10 @@ from pathlib import Path
from mutagen.mp3 import MP3
from utils.console import print_step, print_substep
from rich.progress import track
from rich.console import Console
console = Console()
import re
def save_text_to_mp3(reddit_obj):
"""Saves Text to MP3 files.
@ -43,6 +44,7 @@ def save_text_to_mp3(reddit_obj):
tts.save(f"assets/mp3/{idx}.mp3")
length += MP3(f"assets/mp3/{idx}.mp3").info.length
print_substep("Saved Text to MP3 files successfully.", style="bold green")
#let user know that the MP3 files are saved
console.log(f"[bold green]Saved {idx + 1} MP3 Files.")
# ! Return the index so we know how many screenshots of comments we need to make.
return length, idx

Loading…
Cancel
Save