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.
40 lines
1.1 KiB
40 lines
1.1 KiB
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
|
|
|