File size: 1,541 Bytes
837fdb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)
    # catch urllib.error.URLError
    try:
        title=yt.title
    except Exception as e:
        error_msg = 'Error: Retreiving the video title failed.'
        print(error_msg,e)
        return e

    # create title
    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)
    #yt = YouTube(link)
    #filetitle = get_filename_title(yt)
    filetitle = 'input_video'
    # download video
    working_directory = './workdir/'
    filetitle_mp4=working_directory+filetitle+'.mp4'


    # create a working directory for the files
    if not os.path.isdir(working_directory):
        print('There is no working directory. Create a new one.')
        os.mkdir(working_directory)

    # catch urllib.error.URLError
    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