You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RedditVideoMakerBot/video_creation/background.py

47 lines
1.6 KiB

3 years ago
from random import randrange
from pytube import YouTube
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
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.
Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8)
"""
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. 😎"
)
print_substep("Downloading the background video... please be patient 🙏")
YouTube("https://www.youtube.com/watch?v=n_Dv4JMiwK8").streams.filter(
res="720p"
).first().download(
"assets/mp4",
filename="background.mp4",
)
print_substep("Background video downloaded successfully! 🎉", style="bold green")
def chop_background_video(video_length):
print_step("Finding a spot in the background video to chop...✂️")
background = VideoFileClip("assets/mp4/background.mp4")
start_time, end_time = get_start_and_end_times(video_length, background.duration)
ffmpeg_extract_subclip(
"assets/mp4/background.mp4",
start_time,
end_time,
targetname="assets/mp4/clip.mp4",
)
print_substep("Background video chopped successfully! 🎉", style="bold green")