forked from frostwire/frostwire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telluride.py
121 lines (106 loc) · 3.85 KB
/
telluride.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'''
Telluride Cloud Video Downloader.
Copyright 2020-2021 FrostWire LLC.
Author: @gubatron
A portable and easy to use youtube_dl wrapper by FrostWire.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
# python path imports
import argparse
from datetime import datetime
import json
import sys
import youtube_dl
# our imports
import server
BUILD = 16
def welcome():
'''
Prints the name of the program, build and copyright
'''
print()
print("Telluride Cloud Video Downloader. Build " + str(BUILD))
print("Copyright 2020-{} FrostWire LLC. Licensed under Apache 2.0.".format(datetime.today().year))
print()
def prepare_options_parser(parser):
'''
Initialize all the possible program options
'''
parser.add_argument(
"--server",
"-s",
action="store_true",
help="Launches Telluride as a web server to perform URL queries and return meta data as JSON. There's only one endpoint at the root path. Possible parameters are url=<video_page_url> and shutdown=1 to shutdown the server. The server will only answer to requests from localhost"
)
parser.add_argument(
"--port",
"-p",
default=server.DEFAULT_HTTP_PORT,
type=int,
help='HTTP port when running on server mode. Default port number is 47999. This parameter is only taken into account if --server or -s passed'
)
parser.add_argument(
"--audio-only",
"-a",
action='store_true',
help='Downloads the video and keeps only a separate audio file' +
' usually a .mp3. (requires ffmpeg installed in the system)')
parser.add_argument(
"--meta-only",
"-m",
action='store_true',
help='Prints a JSON dictionary with all the metadata available on' +
' the video file found in the page_url. ' +
'Does not download the video file')
parser.add_argument(
"page_url",
nargs='?',
help="The URL of the page that hosts the video you need to backup locally")
if __name__ == "__main__":
welcome()
PARSER = argparse.ArgumentParser()
prepare_options_parser(PARSER)
ARGS, LEFTOVERS = PARSER.parse_known_args()
SERVER_MODE = ARGS.server
SERVER_PORT = ARGS.port
AUDIO_ONLY = ARGS.audio_only
META_ONLY = ARGS.meta_only
PAGE_URL = ARGS.page_url
if SERVER_MODE:
print('Starting Telluride Web Server on port {}'.format(SERVER_PORT))
server.start(BUILD, SERVER_PORT)
sys.exit(0)
if PAGE_URL is None:
print('Please pass a video page URL or "--help" for instructions\n')
sys.exit(1)
YDL_OPTS = {'nocheckcertificate' : True,
'quiet': False,
'restrictfilenames': True
}
if META_ONLY:
YDL_OPTS['quiet'] = True
YDL_OPTS['format'] = 'bestaudio/best'
with youtube_dl.YoutubeDL(YDL_OPTS) as ydl:
INFO_DICT = ydl.extract_info(PAGE_URL, download=False)
print(json.dumps(INFO_DICT, indent=2))
sys.exit(0)
if AUDIO_ONLY:
print("Audio-only download.")
YDL_OPTS['format'] = 'bestaudio/best'
YDL_OPTS['postprocessors'] = [
{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}
]
with youtube_dl.YoutubeDL(YDL_OPTS) as ydl:
ydl.download([PAGE_URL])