@ -2,13 +2,24 @@ import random
from os import listdir , environ
from os import listdir , environ
from pathlib import Path
from pathlib import Path
from random import randrange
from random import randrange
from pytube import YouTube
from moviepy . video . io . ffmpeg_tools import ffmpeg_extract_subclip
from moviepy . editor import VideoFileClip
from moviepy . editor import VideoFileClip
from moviepy . video . io . ffmpeg_tools import ffmpeg_extract_subclip
from pytube import YouTube
from utils . console import print_step , print_substep
from utils . console import print_step , print_substep
def get_start_and_end_times ( video_length , length_of_clip ) :
def get_start_and_end_times ( video_length : int , length_of_clip : int ) - > tuple [ int , int ] :
""" Generates a random interval of time to be used as the beckground of the video.
Args :
video_length ( int ) : Length of the video
length_of_clip ( int ) : Length of the video to be used as the background
Returns :
tuple [ int , int ] : Start and end time of the randomized interval
"""
random_time = randrange ( 180 , int ( length_of_clip ) - int ( video_length ) )
random_time = randrange ( 180 , int ( length_of_clip ) - int ( video_length ) )
return random_time , random_time + video_length
return random_time , random_time + video_length
@ -26,7 +37,7 @@ def download_background():
]
]
# note: make sure the file name doesn't include an - in it
# note: make sure the file name doesn't include an - in it
if not len ( listdir ( " ./assets/backgrounds " ) ) > = len (
if not len ( listdir ( " ./assets/backgrounds " ) ) > = len (
background_options
background_options
) : # if there are any background videos not installed
) : # if there are any background videos not installed
print_step (
print_step (
" We need to download the backgrounds videos. they are fairly large but it ' s only done once. 😎 "
" We need to download the backgrounds videos. they are fairly large but it ' s only done once. 😎 "
@ -45,7 +56,12 @@ def download_background():
)
)
def chop_background_video ( video_length ) :
def chop_background_video ( video_length : int ) :
""" Generates the background footage to be used in the video and writes it to assets/temp/background.mp4
Args :
video_length ( int ) : Length of the clip where the background footage is to be taken out of
"""
print_step ( " Finding a spot in the backgrounds video to chop...✂️ " )
print_step ( " Finding a spot in the backgrounds video to chop...✂️ " )
choice = random . choice ( listdir ( " assets/backgrounds " ) )
choice = random . choice ( listdir ( " assets/backgrounds " ) )
environ [ " background_credit " ] = choice . split ( " - " ) [ 0 ]
environ [ " background_credit " ] = choice . split ( " - " ) [ 0 ]
@ -53,11 +69,16 @@ def chop_background_video(video_length):
background = VideoFileClip ( f " assets/backgrounds/ { choice } " )
background = VideoFileClip ( f " assets/backgrounds/ { choice } " )
start_time , end_time = get_start_and_end_times ( video_length , background . duration )
start_time , end_time = get_start_and_end_times ( video_length , background . duration )
ffmpeg_extract_subclip (
try :
f " assets/backgrounds/ { choice } " ,
ffmpeg_extract_subclip (
start_time ,
f " assets/backgrounds/ { choice } " ,
end_time ,
start_time ,
targetname = " assets/temp/background.mp4 " ,
end_time ,
)
targetname = " assets/temp/background.mp4 " ,
)
except ( OSError , IOError ) : # ffmpeg issue see #348
print_substep ( " FFMPEG issue. Trying again... " )
with VideoFileClip ( f " assets/backgrounds/ { choice } " ) as video :
new = video . subclip ( start_time , end_time )
new . write_videofile ( " assets/temp/background.mp4 " )
print_substep ( " Background video chopped successfully! " , style = " bold green " )
print_substep ( " Background video chopped successfully! " , style = " bold green " )
return True