|
from curses import error |
|
from pytube import YouTube |
|
import os |
|
|
|
def get_obj_from_link(link): |
|
yt = YouTube(link) |
|
return yt |
|
|
|
|
|
def get_filename_title(link): |
|
if "v=" in link: |
|
video_id = link.split("v=")[1] |
|
else: |
|
return "Error: Invalid Link, it does not have the pattern 'v=' in it." |
|
yt = get_obj_from_link(link) |
|
|
|
try: |
|
title=yt.title |
|
except Exception as e: |
|
error_msg = 'Error: Retreiving the video title failed.' |
|
print(error_msg,e) |
|
return e |
|
|
|
|
|
import re |
|
title_= re.sub(' ', '_', title) |
|
filetitle=re.sub('[^0-9a-zA-Z_äüöß-]+','',title_) |
|
filetitle_vid =filetitle+'='+video_id |
|
return filetitle_vid |
|
|
|
def get_video(link): |
|
yt = get_obj_from_link(link) |
|
|
|
|
|
filetitle = 'input_video' |
|
|
|
working_directory = './workdir/' |
|
filetitle_mp4=working_directory+filetitle+'.mp4' |
|
|
|
|
|
|
|
if not os.path.isdir(working_directory): |
|
print('There is no working directory. Create a new one.') |
|
os.mkdir(working_directory) |
|
|
|
|
|
try: |
|
result = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename=filetitle_mp4) |
|
except Exception as e: |
|
error_msg = 'Error: Retreiving the video failed.'+result |
|
print(error_msg,e) |
|
return e |
|
|
|
return result |
|
|