File size: 2,955 Bytes
cd7d3e7
 
 
 
c411b95
cd7d3e7
c411b95
cd7d3e7
fe1b619
 
 
cd7d3e7
 
 
 
 
 
 
 
c411b95
cd7d3e7
 
 
 
 
 
 
 
b272f24
cd7d3e7
 
c411b95
6ec654e
cd7d3e7
 
 
 
 
 
 
c411b95
 
 
 
c9d7e5d
c411b95
cd7d3e7
 
 
 
 
c411b95
 
cd7d3e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
533fad4
 
b272f24
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python

from pytube import YouTube
from pytube.utils import print_status
from pytube.utils import FullPaths
from pytube.exceptions import YouTubeError
from pprint import pprint

import sys
import argparse


def _main():
    parser = argparse.ArgumentParser(description='YouTube video downloader')
    parser.add_argument("url", help="The URL of the Video to be downloaded")
    parser.add_argument("--extension", "-e",
                        help="The requested format of the video", dest="ext")
    parser.add_argument("--resolution", "-r",
                        help="The requested resolution", dest="res")
    parser.add_argument("--path", "-p", action=FullPaths,
                        help="The path to save the video to.", dest="path")
    parser.add_argument("--filename", "-f",
                        dest="filename",
                        help=("The filename, without extension, "
                              "to save the video in."))

    args = parser.parse_args()

    yt = YouTube()
    try:
        yt.url = args.url
        res_formts = yt.videos
        res_formts = ["%s %s" % (str(res_formts[index].extension), str(res_formts[index].resolution)) for index, video in enumerate(res_formts)]
    except YouTubeError:
        print "Incorrect video URL."
        sys.exit(1)

    if args.filename:
        yt.filename = args.filename

    if args.ext or args.res:
        if not all([args.ext, args.res]):
            print "\nMake sure you give either of the below specified format/resolution combination.\n"
            pprint(res_formts)
            sys.exit(1)

    if args.ext and args.res:
        # There's only ope video that matches both so get it
        vid = yt.get(args.ext, args.res)
        # Check if there's a video returned
        if not vid:
            print "\nThere's no video with the specified format/resolution combination.\n"
            pprint(res_formts)
            sys.exit(1)

    elif args.ext:
        # There are several videos with the same extension
        videos = yt.filter(extension=args.ext)
        # Check if we have a video
        if not videos:
            print "There are no videos in the specified format."
            sys.exit(1)
        # Select the highest resolution one
        vid = max(videos)
    elif args.res:
        # There might be several videos in the same resolution
        videos = yt.filter(res=args.res)
        # Check if we have a video
        if not videos:
            print "There are no videos in the specified in the specified resolution."
            sys.exit(1)
        # Select the highest resolution one
        vid = max(videos)
    else:
        # If nothing is specified get the highest resolution one
        vid = max(yt.videos)

    try:
        vid.download(path=args.path, on_progress=print_status)
    except KeyboardInterrupt:
        print "Download interrupted."
        sys.exit(1)

if __name__ == '__main__':
    _main()