This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlive-dl.py
209 lines (177 loc) · 6.9 KB
/
dlive-dl.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import argparse
import json
import os
import re
import shutil
import sys
import tempfile
from urllib import request
def main():
parser = argparse.ArgumentParser()
parser.add_argument('url', help='vod url')
parser.add_argument('-l', '--list', help='list available encodings and exit', action='store_true')
parser.add_argument('-q', '--quality', help='define which quality of video to download', type=int, metavar='#',
default=1)
parser.add_argument('-o', '--outdir', help='directory to save the video to', type=str, metavar='path',
default=os.getcwd())
args = parser.parse_args()
vod_id = args.url.split('+')[1]
pb_info = get_playback_info(args.url)
# define video info
info = pb_info['data']['pastBroadcast']
title = info['title']
user = info['creator']['displayname']
playback_url = info['playbackUrl']
vid_info = parse_vod_m3u8(playback_url)
videos = []
for vid in vid_info:
v = Video(user, vod_id, title, vid['resolution'], vid['quality'], vid['url'], vid['bandwidth'])
videos.append(v)
if args.list:
print_qualities(videos)
sys.exit(0)
if args.quality > len(videos) or args.quality <= 0:
raise Exception('Selected quality doesn\'t exist')
# download video
try:
video = videos[args.quality - 1]
except IndexError:
video = videos[0]
video.download(args.outdir)
sys.exit(0)
def get_playback_info(url):
perm_link = url.split('/')[-1]
api_url = 'https://graphigo.prd.dlive.tv/'
data = {"operationName": "PastBroadcastPage",
"variables": {
"permlink": perm_link,
"commentsFirst": 0,
"topContributionsFirst": 0,
"isLoggedIn": False
},
"extensions": {
"persistedQuery": {
"version": 1,
"sha256Hash": "8fa2f4a94174e9552b76190ae847926e283f292e24b5f6b4908acffb6902805f"
}
}
}
data = json.dumps(data).encode()
r = request.Request(api_url, method='POST')
r.add_header('Content-Type', 'application/json')
response = request.urlopen(r, data=data)
if response.status != 200:
raise Exception(f'Unexpected response!\n{response.status - response.msg - response.reason}')
pb_json = response.read().decode('utf-8')
pb_info = json.loads(pb_json)
if 'errors' in pb_info.keys():
errors = '\n'.join(error['message'] for error in pb_info['errors'])
raise Exception(f'Unexpected response data!\n{errors}')
return pb_info
def print_qualities(videos):
print(f'{videos[0].user} - {videos[0].title} - {format_duration(videos[0].duration)}')
for i, video in enumerate(videos):
print(f'{i + 1} - {video.quality} - {video.resolution}')
def parse_vod_m3u8(url):
vid_info = []
m3u8_text = request.urlopen(url).read().decode('utf-8')
m3u8_lines = m3u8_text.splitlines()
for i, line in enumerate(m3u8_lines):
if line.startswith('#EXT-X-STREAM-INF:'):
re_str = '#EXT-X-STREAM-INF:PROGRAM-ID=(?P<program_id>.*),BANDWIDTH=(?P<bandwidth>.*),' \
'CODECS="(?P<codecs>.*)",RESOLUTION=(?P<resolution>.*),VIDEO="(?P<quality>.*)"'
match = re.search(re_str, line)
v = {'resolution': match.group('resolution'),
'quality': match.group('quality'),
'url': m3u8_lines[i + 1],
'bandwidth': int(match.group('bandwidth'))}
vid_info.append(v)
return vid_info
def format_duration(duration):
mins, secs = divmod(duration, 60)
hrs, mins = divmod(mins, 60)
hrs = int(hrs)
mins = int(mins)
secs = int(secs)
if hrs:
return f'{hrs:02}:{mins:02}:{secs:02}'
else:
return f'{mins:02}:{secs:02}'
class Video(object):
def __init__(self, user, vod_id, title, resolution, quality, playback_url, bandwidth):
self.user = user
self.vod_id = vod_id
self.title = title
self.resolution = resolution
self.quality = quality
self.m3u8_url = playback_url
self.filename = f'{self.user}-{vod_id}-{resolution}-{quality}.mp4'
self._ts_urls = None
self._duration = None
self._m3u8 = None
self._size = None
self._bandwidth = bandwidth
def download(self, out_dir):
temp_dir = tempfile.TemporaryDirectory()
ts_files = self._download_ts_files(temp_dir)
self._merge_ts_files(ts_files, out_dir)
temp_dir.cleanup()
print('\nDone!')
def _download_ts_files(self, temp_dir):
out_files = []
i = 0
while i < len(self.ts_urls):
percent = ((i + 1) / len(self.ts_urls)) * 100
print(f'Downloading {self.filename} - part {i + 1} of {len(self.ts_urls)} - {percent:0.2f}%', end='\r')
out_file = os.path.join(temp_dir.name, str(i) + '.ts')
block_size = 1024
try:
response = request.urlopen(self.ts_urls[i])
with open(out_file, 'wb') as f:
while True:
buffer = response.read(block_size)
if not buffer:
break
f.write(buffer)
out_files.append(out_file)
i += 1
except ConnectionResetError:
print(f'Failed to download {self.title} - part {i + 1} of '
f'{len(self.ts_urls)} - {percent:0.2f}%', end='\r')
print('\nRetrying download')
print('')
return out_files
def _merge_ts_files(self, ts_list, out_dir):
with open(os.path.join(out_dir, self.filename), 'wb') as merged:
for i, ts_file in enumerate(ts_list):
percent = ((i + 1) / len(ts_list) * 100)
with open(ts_file, 'rb') as merged_file:
shutil.copyfileobj(merged_file, merged)
print(f'Merging files - {percent:0.2f}%', end='\r')
@property
def ts_urls(self):
if self._ts_urls:
return self._ts_urls
self._ts_urls = []
for line in self.m3u8.splitlines():
if line.endswith('.ts'):
self._ts_urls.append(line)
return self._ts_urls
@property
def m3u8(self):
if self._m3u8:
return self._m3u8
self._m3u8 = request.urlopen(self.m3u8_url).read().decode('utf-8')
return self._m3u8
@property
def duration(self):
if self._duration:
return self._duration
self._duration = 0
for line in self.m3u8.splitlines():
match = re.search('^#EXTINF:(\d*.\d*),', line)
if match:
self._duration += float(match.group(1))
return self._duration
if __name__ == '__main__':
main()