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

54 lines
1.7 KiB

#!/usr/bin/env python3
3 years ago
from random import randrange
3 years ago
from yt_dlp import YoutubeDL
3 years ago
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
3 years ago
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(
3 years ago
"We need to download the Minecraft background video. This is fairly large but it's only done once."
3 years ago
)
3 years ago
print_substep("Downloading the background video... please be patient.")
ydl_opts = {
"outtmpl": "assets/mp4/background.mp4",
"merge_output_format": "mp4",
3 years ago
}
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")
3 years ago
def chop_background_video(video_length):
3 years ago
print_step("Finding a spot in the background video to chop...")
3 years ago
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",
)
3 years ago
print_substep("Background video chopped successfully!", style="bold green")