|
|
|
import yaml
|
|
|
|
import pytube
|
|
|
|
import tempfile
|
|
|
|
from functools import lru_cache
|
|
|
|
import moviepy.editor as mpy
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(5)
|
|
|
|
def get_video(url):
|
|
|
|
output_dir = tempfile.mktemp()
|
|
|
|
video = pytube.YouTube(url)
|
|
|
|
video.streams.filter(res="360p").first().download(filename=output_dir)
|
|
|
|
return output_dir
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(5)
|
|
|
|
def create_gif_from_video_and_timestamps(video_path, start, end):
|
|
|
|
video = mpy.VideoFileClip(video_path)
|
|
|
|
clip: mpy.VideoFileClip = video.subclip(start, end)
|
|
|
|
output_dir = tempfile.mktemp() + '.mp4'
|
|
|
|
clip.without_audio().write_videofile(output_dir)
|
|
|
|
return output_dir
|
|
|
|
|
|
|
|
|
|
|
|
def load_file_and_media_links(file_path):
|
|
|
|
file = open(file_path)
|
|
|
|
exercises = yaml.safe_load(file)
|
|
|
|
for ex in exercises['exercises']:
|
|
|
|
name = ex['name']
|
|
|
|
video_url = ex.get('video')
|
|
|
|
start = ex.get('start')
|
|
|
|
end = ex.get('end')
|
|
|
|
if video_url:
|
|
|
|
video_path = get_video(video_url)
|
|
|
|
gif = create_gif_from_video_and_timestamps(video_path, start, end)
|
|
|
|
ex['gif_path'] = gif
|
|
|
|
else:
|
|
|
|
ex['gif_path'] = None
|
|
|
|
|
|
|
|
return exercises
|