Skip to content

Commit 0f82738

Browse files
committed
feat: finishing background config + progress bar when downloading video
1 parent e37f2db commit 0f82738

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

background/AssasinCreed.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from background.utils import Background
2+
3+
4+
class AssasinCreed(Background):
5+
def __init__(self):
6+
super().__init__(
7+
"https://www.youtube.com/watch?v=pDkH7odN9dc",
8+
"assasin_creed.mp4",
9+
"Woofioo60",
10+
lambda t : ("center", 480 + t)
11+
)

background/RocketLeague.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ def __init__(self):
77
"https://www.youtube.com/watch?v=2X9QGY__0II",
88
"rocket_league.mp4",
99
"Orbital Gameplay",
10-
"top"
10+
lambda t : ('center', 480 + t)
1111
)

background/swapper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from background.Minecraft import Minecraft
77
from background.MotorGTA import MotorGTA
88
from background.RocketLeague import RocketLeague
9+
from background.AssasinCreed import AssasinCreed
910
from utils.console import print_substep
1011

11-
CHOICE_DIR = {"motor-gta": MotorGTA, "rocket-league": RocketLeague, "minecraft": Minecraft}
12+
CHOICE_DIR = {"motor-gta": MotorGTA, "rocket-league": RocketLeague, "minecraft": Minecraft, "ac": AssasinCreed}
1213

1314

1415
class BackgroundFactory:

background/utils.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
from os import listdir, path
22
from pathlib import Path
3-
from pytube import YouTube
3+
from pytube import YouTube, Stream
4+
from pytube.cli import on_progress
45
from rich.progress import Progress
6+
from tqdm import tqdm
57
from utils.console import print_step, print_substep
68

79
class DownloaderProgressBar:
8-
def __init__(self):
9-
self.bar = Progress(transient=True)
10-
self.task = self.bar.add_task("Downloading background video...", total=100)
11-
self.p = 0
12-
def progress_function(self,stream, chunk, bytes_remaining):
13-
percentage = lambda rem, total : (float(rem) / float(total)) * float(100)
14-
size = stream.filesize
15-
p = 0
16-
with self.bar as progress:
17-
while p <= 100 and not progress.finished:
18-
p = 100.0 - percentage(bytes_remaining, size)
19-
progress.update(self.task, advance=p)
20-
self.p = p
10+
"""
11+
Show download progress for pytube.
12+
Code ref from StackOverflow. It's so efficient, that it's probably the best way to do it.
13+
# Although, it is edited to use less threads.
14+
"""
15+
16+
def start(self, video):
17+
self.pbar = tqdm(total=video.filesize, unit="bytes")
18+
19+
def progress_function(self,stream: Stream, data_chunk: bytes, bytes_remaining: int) -> None:
20+
self.pbar.update(len(data_chunk))
21+
22+
def close(self):
23+
self.pbar.close()
2124

2225
def download_background(background_option):
2326
"""Downloads the backgrounds/s video from YouTube."""
@@ -32,13 +35,27 @@ def download_background(background_option):
3235
print_substep("Downloading the backgrounds videos... please be patient 🙏 ")
3336
print_substep(f"Downloading {filename} from {uri}")
3437
pbar = DownloaderProgressBar()
35-
YouTube(uri, on_progress_callback=pbar.progress_function).streams.filter(res="1080p").first().download(
38+
video = YouTube(uri, on_progress_callback=pbar.progress_function).streams.filter(res="1080p").first()
39+
pbar.start(video)
40+
video.download(
3641
"assets/backgrounds", filename=f"{credit}-{filename}"
3742
)
43+
pbar.close()
3844
print_substep("Background videos downloaded successfully! 🎉", style="bold green")
3945

4046
class Background:
41-
def __init__(self, uri, filename, credit, pos):
47+
"""Calls the given TTS engine to reduce code duplication and allow multiple TTS engines.
48+
49+
Args:
50+
uri : The URI for the video (Youtube)
51+
filename : The local filename to save the downloaded mp4 video. Must be a valid filename.
52+
credit : The owner of the video.
53+
pos (Optional) : The position of the image clip in the video. Please follow this ref to manipulate image position in the background freely (https://zulko.github.io/moviepy/ref/VideoClip/VideoClip.html?highlight=imageclip#moviepy.video.VideoClip.VideoClip.set_position)
54+
55+
Notes:
56+
Make sure that the Youtube video source must have 1080p options.
57+
"""
58+
def __init__(self, uri, filename, credit, pos="center"):
4259
self.uri = uri
4360
self.filename = filename
4461
self.credit = credit

video_creation/background.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def chop_background_video(bg, video_length: int):
3636
"""Generates the background footage to be used in the video and writes it to assets/temp/background.mp4
3737
3838
Args:
39+
bg : Background Obj
3940
video_length (int): Length of the clip where the background footage is to be taken out of
4041
"""
4142

video_creation/final_video.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def make_final_video(bg, number_of_clips: int, length: int):
3030
"""Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp
3131
3232
Args:
33+
bg (Background): The background object for the final video
3334
number_of_clips (int): Index to end at when going through the screenshots
3435
length (int): Length of the video
3536
"""

0 commit comments

Comments
 (0)