Skip to content

Commit 73d15fe

Browse files
Merge pull request #693 from William9923/feat/background-configs
Reddit Post Position Config & Download Progress Bar
2 parents bd52b4f + 51831dd commit 73d15fe

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ def main(POST_ID=None):
3838
length = math.ceil(length)
3939
bg_config = get_background_config()
4040
download_screenshots_of_reddit_posts(reddit_object, number_of_comments)
41-
download_background()
42-
credit = chop_background_video(length)
43-
make_final_video(number_of_comments, length, reddit_object, credit)
41+
bg_config = get_background_config()
42+
download_background(bg_config)
43+
chop_background_video(bg_config, length)
44+
make_final_video(number_of_comments, length, reddit_object, bg_config)
4445

4546

4647
def run_many(times):

video_creation/background.py

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import random
22
from os import listdir
33
from pathlib import Path
4+
import random
45
from random import randrange
5-
from typing import Tuple
6+
from typing import Any, Tuple
7+
8+
from dotenv import load_dotenv
69

710
from moviepy.editor import VideoFileClip
811
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
912
from pytube import YouTube
13+
from pytube.cli import on_progress
1014

1115
from utils import settings
1216
from utils.console import print_step, print_substep
@@ -58,7 +62,7 @@ def get_background_config():
5862
choice = random.choice(list(background_options.keys()))
5963

6064
return background_options[choice]
61-
65+
6266
def get_start_and_end_times(video_length: int, length_of_clip: int) -> Tuple[int, int]:
6367
"""Generates a random interval of time to be used as the background of the video.
6468
@@ -73,51 +77,59 @@ def get_start_and_end_times(video_length: int, length_of_clip: int) -> Tuple[int
7377
return random_time, random_time + video_length
7478

7579

76-
def download_background():
77-
"""Downloads the backgrounds/s video from YouTube."""
80+
def get_background_config():
81+
"""Fetch the background/s configuration"""
82+
load_dotenv()
83+
try:
84+
choice = getenv("BackgroundChoice").casefold()
85+
except AttributeError:
86+
print_substep("No background selected. Picking random background'")
87+
choice = None
88+
89+
# Handle default / not supported background using default option.
90+
# Default : pick random from supported background.
91+
if not choice or choice not in background_options:
92+
choice = random.choice(list(background_options.keys()))
93+
94+
return background_options[choice]
95+
96+
97+
def download_background(background_config: Tuple[str, str, str, Any]):
98+
"""Downloads the background/s video from YouTube."""
7899
Path("./assets/backgrounds/").mkdir(parents=True, exist_ok=True)
79-
background_options = [ # uri , filename , credit
80-
("https://www.youtube.com/watch?v=n_Dv4JMiwK8", "parkour.mp4", "bbswitzer"),
81-
# (
82-
# "https://www.youtube.com/watch?v=2X9QGY__0II",
83-
# "rocket_league.mp4",
84-
# "Orbital Gameplay",
85-
# ),
86-
]
87100
# note: make sure the file name doesn't include an - in it
88-
if not len(listdir("./assets/backgrounds")) >= len(
89-
background_options
90-
): # if there are any background videos not installed
91-
print_step(
92-
"We need to download the backgrounds videos. they are fairly large but it's only done once. 😎"
93-
)
94-
print_substep("Downloading the backgrounds videos... please be patient 🙏 ")
95-
for uri, filename, credit in background_options:
96-
if Path(f"assets/backgrounds/{credit}-{filename}").is_file():
97-
continue # adds check to see if file exists before downloading
98-
print_substep(f"Downloading {filename} from {uri}")
99-
YouTube(uri).streams.filter(res="1080p").first().download(
100-
"assets/backgrounds", filename=f"{credit}-{filename}"
101-
)
102-
103-
print_substep(
104-
"Background videos downloaded successfully! 🎉", style="bold green"
105-
)
101+
uri, filename, credit, _ = background_config
102+
if Path(f"assets/backgrounds/{credit}-{filename}").is_file():
103+
return
104+
print_step(
105+
"We need to download the backgrounds videos. they are fairly large but it's only done once. 😎"
106+
)
107+
print_substep("Downloading the backgrounds videos... please be patient 🙏 ")
108+
print_substep(f"Downloading {filename} from {uri}")
109+
YouTube(uri, on_progress_callback=on_progress).streams.filter(res="1080p").first().download(
110+
"assets/backgrounds", filename=f"{credit}-{filename}"
111+
)
112+
print_substep("Background videos downloaded successfully! 🎉",
113+
style="bold green")
106114

107115

108-
def chop_background_video(video_length: int) -> str:
116+
def chop_background_video(background_config: Tuple[str, str, str, Any], video_length: int):
109117
"""Generates the background footage to be used in the video and writes it to assets/temp/background.mp4
110118
111119
Args:
120+
background_config (Tuple[str, str, str, Any]) : Current background configuration
112121
video_length (int): Length of the clip where the background footage is to be taken out of
113122
"""
123+
114124
print_step("Finding a spot in the backgrounds video to chop...✂️")
115-
choice = random.choice(listdir("assets/backgrounds"))
116-
credit = choice.split("-")[0]
125+
choice = f"{background_config[2]}-{background_config[1]}"
126+
environ["background_credit"] = choice.split("-")[0]
127+
117128

118129
background = VideoFileClip(f"assets/backgrounds/{choice}")
119130

120-
start_time, end_time = get_start_and_end_times(video_length, background.duration)
131+
start_time, end_time = get_start_and_end_times(
132+
video_length, background.duration)
121133
try:
122134
ffmpeg_extract_subclip(
123135
f"assets/backgrounds/{choice}",

video_creation/final_video.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from utils.videos import save_data
2424
from utils import settings
2525

26+
2627
console = Console()
2728

2829
W, H = 1080, 1920
@@ -45,7 +46,6 @@ def name_normalize(name: str) -> str:
4546
else:
4647
return name
4748

48-
4949
def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, background_config: Tuple[str, str, str, Any]):
5050
"""Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp
5151
Args:
@@ -78,7 +78,6 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict, backgr
7878
image_clips = []
7979
# Gather all images
8080
new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity)
81-
8281
image_clips.insert(
8382
0,
8483
ImageClip("assets/temp/png/title.png")

0 commit comments

Comments
 (0)