-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
76 lines (53 loc) · 2.04 KB
/
main.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
import sys
import requests
def get_video_id(url):
# remove slash from the end of the url
if url[-1] == '/':
filtered_url = url[:-1]
else:
filtered_url = url
# get video id from url
result = filtered_url.split('/')[-1]
return result
def get_video_config(id):
# set video config url
video_config_url = 'https://player.vimeo.com/video/' + id + '/config'
# send get request to get video json config
video_config_response = requests.get(video_config_url)
# generate obj from json
config = video_config_response.json()
return config
def find_required_quality_height(video_config, required_quality):
# video config
target_video_config = None
# check all video find the closest one
for video_config in video_config['request']['files']['progressive']:
# skip first video
if target_video_config is None:
target_video_config = video_config
continue
# get video height
video_height = video_config['height']
# check video height
video_height_diff = abs(required_quality - video_height)
target_video_height_diff = abs(required_quality - target_video_config['height'])
# check video height diff
if video_height_diff < target_video_height_diff:
target_video_config = video_config
return target_video_config
def download_video(download_url, file_name):
# download video
video_response = requests.get(download_url)
# open file and write content there
video_file = open(file_name, 'wb')
video_file.write(video_response.content)
video_file.close()
# main sequence of the program
target_video_url = sys.argv[1]
video_id = get_video_id(target_video_url)
video_config_json = get_video_config(video_id)
target_pr_config = find_required_quality_height(video_config_json, 480)
video_url = target_pr_config['url']
video_file_name = video_id + '_' + target_pr_config['quality'] + '.mp4'
download_video(video_url, video_file_name)
print('downloaded: ' + video_file_name)