From f5db3857584fa871aa9c2868bc14f378cf16ae69 Mon Sep 17 00:00:00 2001 From: alperen cantez Date: Sun, 5 Jun 2022 23:22:23 +0300 Subject: [PATCH] add custom background --- video_creation/background.py | 48 +++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index dce46bd..b2627a7 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -6,36 +6,50 @@ from pathlib import Path from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from moviepy.editor import VideoFileClip from utils.console import print_step, print_substep - +import requests 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(): - """Downloads the background video from youtube. +def choose_bg(bg_video_path): + print_step( + "We need to download the background video. This is fairly large but it's only done once." + ) - Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) - """ + print_substep("Downloading the background video... please be patient.") - if not Path("assets/mp4/background.mp4").is_file(): - print_step( - "We need to download the Minecraft background video. This is fairly large but it's only done once." - ) + ydl_opts = { + "outtmpl": "assets/mp4/background.mp4", + "merge_output_format": "mp4", + } - print_substep("Downloading the background video... please be patient.") + with YoutubeDL(ydl_opts) as ydl: + ydl.download(bg_video_path) - ydl_opts = { - "outtmpl": "assets/mp4/background.mp4", - "merge_output_format": "mp4", - } + # check if the link is reachable + request = requests.get(bg_video_path, allow_redirects=False) + if request.status_code >= 200: + print_substep("Background video downloaded successfully!", style="bold green") + else: + print_substep("The link you've given might be broken or private, make sure the video can be accessed publicly") - with YoutubeDL(ydl_opts) as ydl: - ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") - print_substep("Background video downloaded successfully!", style="bold green") +def download_background(): + """Downloads the background video from youtube. + + Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) + """ + get_bg_preference = input("The default background video is a Minecraft parkour, do you want to provide a custom video? (y/n)").lower() + if not Path("assets/mp4/background.mp4").is_file() and get_bg_preference == "y": + choose_bg(input("Please insert the YouTube link to your video: ")) + elif get_bg_preference == "n": + print_substep("Downloading default Minecraft background! 👾") + choose_bg("https://www.youtube.com/watch?v=n_Dv4JMiwK8") + else: + print_substep("Invalid input or you already have a background video downloaded, if so please delete the video before a retry", style="red") def chop_background_video(video_length): print_step("Finding a spot in the background video to chop...")