1
1
import random
2
2
from os import listdir
3
3
from pathlib import Path
4
+ import random
4
5
from random import randrange
5
- from typing import Tuple
6
+ from typing import Any , Tuple
7
+
8
+ from dotenv import load_dotenv
6
9
7
10
from moviepy .editor import VideoFileClip
8
11
from moviepy .video .io .ffmpeg_tools import ffmpeg_extract_subclip
9
12
from pytube import YouTube
13
+ from pytube .cli import on_progress
10
14
11
15
from utils import settings
12
16
from utils .console import print_step , print_substep
@@ -58,7 +62,7 @@ def get_background_config():
58
62
choice = random .choice (list (background_options .keys ()))
59
63
60
64
return background_options [choice ]
61
-
65
+
62
66
def get_start_and_end_times (video_length : int , length_of_clip : int ) -> Tuple [int , int ]:
63
67
"""Generates a random interval of time to be used as the background of the video.
64
68
@@ -73,51 +77,59 @@ def get_start_and_end_times(video_length: int, length_of_clip: int) -> Tuple[int
73
77
return random_time , random_time + video_length
74
78
75
79
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."""
78
99
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
- ]
87
100
# 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" )
106
114
107
115
108
- def chop_background_video (video_length : int ) -> str :
116
+ def chop_background_video (background_config : Tuple [ str , str , str , Any ], video_length : int ) :
109
117
"""Generates the background footage to be used in the video and writes it to assets/temp/background.mp4
110
118
111
119
Args:
120
+ background_config (Tuple[str, str, str, Any]) : Current background configuration
112
121
video_length (int): Length of the clip where the background footage is to be taken out of
113
122
"""
123
+
114
124
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
+
117
128
118
129
background = VideoFileClip (f"assets/backgrounds/{ choice } " )
119
130
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 )
121
133
try :
122
134
ffmpeg_extract_subclip (
123
135
f"assets/backgrounds/{ choice } " ,
0 commit comments