]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tiktok.py
[ie/bpb] Overhaul extractor (#8119)
[yt-dlp.git] / yt_dlp / extractor / tiktok.py
CommitLineData
f7f18f90 1import itertools
b801cd71 2import json
bd9ff55b 3import random
216bcb66 4import re
bd9ff55b
M
5import string
6import time
1ead840d
KS
7
8from .common import InfoExtractor
b801cd71 9from ..compat import compat_urllib_parse_unquote, compat_urllib_parse_urlparse
3d2623a8 10from ..networking import HEADRequest
1ead840d 11from ..utils import (
ce18a19b 12 ExtractorError,
b801cd71 13 LazyList,
11e1c2e3 14 UnsupportedError,
933ed882 15 UserNotLive,
8ceb07e8 16 determine_ext,
216bcb66 17 format_field,
a39a7ba8 18 get_element_by_id,
ff91cf74 19 get_first,
1ead840d 20 int_or_none,
34921b43 21 join_nonempty,
216bcb66 22 merge_dicts,
b801cd71 23 qualities,
ba723997 24 remove_start,
e0585e65 25 srt_subtitles_timecode,
1ead840d 26 str_or_none,
bd9ff55b 27 traverse_obj,
216bcb66 28 try_call,
bd9ff55b 29 try_get,
943d5ab1 30 url_or_none,
1ead840d
KS
31)
32
33
0fd6661e 34class TikTokBaseIE(InfoExtractor):
f7c5a5e9 35 _APP_VERSIONS = [('26.1.3', '260103'), ('26.1.2', '260102'), ('26.1.1', '260101'), ('25.6.2', '250602')]
046cab39 36 _WORKING_APP_VERSION = None
943d5ab1
M
37 _APP_NAME = 'trill'
38 _AID = 1180
943d5ab1 39 _UPLOADER_URL_FORMAT = 'https://www.tiktok.com/@%s'
53dad39e 40 _WEBPAGE_HOST = 'https://www.tiktok.com/'
be1f331f 41 QUALITIES = ('360p', '540p', '720p', '1080p')
ce18a19b 42
c4cbd3be 43 @property
44 def _API_HOSTNAME(self):
45 return self._configuration_arg(
46 'api_hostname', ['api16-normal-c-useast1a.tiktokv.com'], ie_key=TikTokIE)[0]
47
b801cd71 48 @staticmethod
49 def _create_url(user_id, video_id):
50 return f'https://www.tiktok.com/@{user_id or "_"}/video/{video_id}'
51
a39a7ba8 52 def _get_sigi_state(self, webpage, display_id):
53 return self._parse_json(get_element_by_id(
54 'SIGI_STATE|sigi-persisted-data', webpage, escape_value=False), display_id)
55
046cab39
M
56 def _call_api_impl(self, ep, query, manifest_app_version, video_id, fatal=True,
57 note='Downloading API JSON', errnote='Unable to download API page'):
efa944f4 58 self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choices('0123456789abcdef', k=160)))
046cab39
M
59 webpage_cookies = self._get_cookies(self._WEBPAGE_HOST)
60 if webpage_cookies.get('sid_tt'):
61 self._set_cookie(self._API_HOSTNAME, 'sid_tt', webpage_cookies['sid_tt'].value)
62 return self._download_json(
63 'https://%s/aweme/v1/%s/' % (self._API_HOSTNAME, ep), video_id=video_id,
64 fatal=fatal, note=note, errnote=errnote, headers={
c2a1bdb0 65 'User-Agent': f'com.ss.android.ugc.{self._APP_NAME}/{manifest_app_version} (Linux; U; Android 13; en_US; Pixel 7; Build/TD1A.220804.031; Cronet/58.0.2991.0)',
046cab39
M
66 'Accept': 'application/json',
67 }, query=query)
68
69 def _build_api_query(self, query, app_version, manifest_app_version):
70 return {
0fd6661e 71 **query,
046cab39
M
72 'version_name': app_version,
73 'version_code': manifest_app_version,
74 'build_number': app_version,
75 'manifest_version_code': manifest_app_version,
76 'update_version_code': manifest_app_version,
efa944f4
AM
77 'openudid': ''.join(random.choices('0123456789abcdef', k=16)),
78 'uuid': ''.join(random.choices(string.digits, k=16)),
bd9ff55b
M
79 '_rticket': int(time.time() * 1000),
80 'ts': int(time.time()),
81 'device_brand': 'Google',
c2a1bdb0 82 'device_type': 'Pixel 7',
bd9ff55b 83 'device_platform': 'android',
c2a1bdb0 84 'resolution': '1080*2400',
bd9ff55b 85 'dpi': 420,
c2a1bdb0 86 'os_version': '13',
bd9ff55b
M
87 'os_api': '29',
88 'carrier_region': 'US',
89 'sys_region': 'US',
90 'region': 'US',
943d5ab1 91 'app_name': self._APP_NAME,
bd9ff55b
M
92 'app_language': 'en',
93 'language': 'en',
94 'timezone_name': 'America/New_York',
95 'timezone_offset': '-14400',
96 'channel': 'googleplay',
97 'ac': 'wifi',
98 'mcc_mnc': '310260',
99 'is_my_cn': 0,
943d5ab1 100 'aid': self._AID,
bd9ff55b
M
101 'ssmix': 'a',
102 'as': 'a1qwert123',
103 'cp': 'cbfhckdckkde1',
104 }
046cab39
M
105
106 def _call_api(self, ep, query, video_id, fatal=True,
107 note='Downloading API JSON', errnote='Unable to download API page'):
108 if not self._WORKING_APP_VERSION:
109 app_version = self._configuration_arg('app_version', [''], ie_key=TikTokIE.ie_key())[0]
110 manifest_app_version = self._configuration_arg('manifest_app_version', [''], ie_key=TikTokIE.ie_key())[0]
111 if app_version and manifest_app_version:
112 self._WORKING_APP_VERSION = (app_version, manifest_app_version)
113 self.write_debug('Imported app version combo from extractor arguments')
114 elif app_version or manifest_app_version:
115 self.report_warning('Only one of the two required version params are passed as extractor arguments', only_once=True)
116
117 if self._WORKING_APP_VERSION:
118 app_version, manifest_app_version = self._WORKING_APP_VERSION
119 real_query = self._build_api_query(query, app_version, manifest_app_version)
120 return self._call_api_impl(ep, real_query, manifest_app_version, video_id, fatal, note, errnote)
121
122 for count, (app_version, manifest_app_version) in enumerate(self._APP_VERSIONS, start=1):
123 real_query = self._build_api_query(query, app_version, manifest_app_version)
124 try:
125 res = self._call_api_impl(ep, real_query, manifest_app_version, video_id, fatal, note, errnote)
126 self._WORKING_APP_VERSION = (app_version, manifest_app_version)
127 return res
128 except ExtractorError as e:
129 if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0:
130 if count == len(self._APP_VERSIONS):
131 if fatal:
132 raise e
133 else:
134 self.report_warning(str(e.cause or e.msg))
135 return
136 self.report_warning('%s. Retrying... (attempt %s of %s)' % (str(e.cause or e.msg), count, len(self._APP_VERSIONS)))
137 continue
138 raise e
0fd6661e 139
ba723997 140 def _extract_aweme_app(self, aweme_id):
141 feed_list = self._call_api(
142 'feed', {'aweme_id': aweme_id}, aweme_id, note='Downloading video feed',
143 errnote='Unable to download video feed').get('aweme_list') or []
144 aweme_detail = next((aweme for aweme in feed_list if str(aweme.get('aweme_id')) == aweme_id), None)
145 if not aweme_detail:
146 raise ExtractorError('Unable to find video in feed', video_id=aweme_id)
147 return self._parse_aweme_video_app(aweme_detail)
148
e0585e65
M
149 def _get_subtitles(self, aweme_detail, aweme_id):
150 # TODO: Extract text positioning info
151 subtitles = {}
ba723997 152 # aweme/detail endpoint subs
e0585e65 153 captions_info = traverse_obj(
ba723997 154 aweme_detail, ('interaction_stickers', ..., 'auto_video_caption_info', 'auto_captions', ...), expected_type=dict)
e0585e65
M
155 for caption in captions_info:
156 caption_url = traverse_obj(caption, ('url', 'url_list', ...), expected_type=url_or_none, get_all=False)
157 if not caption_url:
158 continue
159 caption_json = self._download_json(
160 caption_url, aweme_id, note='Downloading captions', errnote='Unable to download captions', fatal=False)
161 if not caption_json:
162 continue
163 subtitles.setdefault(caption.get('language', 'en'), []).append({
164 'ext': 'srt',
165 'data': '\n\n'.join(
166 f'{i + 1}\n{srt_subtitles_timecode(line["start_time"] / 1000)} --> {srt_subtitles_timecode(line["end_time"] / 1000)}\n{line["text"]}'
167 for i, line in enumerate(caption_json['utterances']) if line.get('text'))
168 })
ba723997 169 # feed endpoint subs
170 if not subtitles:
171 for caption in traverse_obj(aweme_detail, ('video', 'cla_info', 'caption_infos', ...), expected_type=dict):
172 if not caption.get('url'):
173 continue
174 subtitles.setdefault(caption.get('lang') or 'en', []).append({
175 'ext': remove_start(caption.get('caption_format'), 'web'),
176 'url': caption['url'],
177 })
178 # webpage subs
179 if not subtitles:
180 for caption in traverse_obj(aweme_detail, ('video', 'subtitleInfos', ...), expected_type=dict):
181 if not caption.get('Url'):
182 continue
183 subtitles.setdefault(caption.get('LanguageCodeName') or 'en', []).append({
184 'ext': remove_start(caption.get('Format'), 'web'),
185 'url': caption['Url'],
186 })
e0585e65
M
187 return subtitles
188
943d5ab1 189 def _parse_aweme_video_app(self, aweme_detail):
0fd6661e 190 aweme_id = aweme_detail['aweme_id']
bd9ff55b
M
191 video_info = aweme_detail['video']
192
193 def parse_url_key(url_key):
194 format_id, codec, res, bitrate = self._search_regex(
195 r'v[^_]+_(?P<id>(?P<codec>[^_]+)_(?P<res>\d+p)_(?P<bitrate>\d+))', url_key,
196 'url key', default=(None, None, None, None), group=('id', 'codec', 'res', 'bitrate'))
197 if not format_id:
198 return {}, None
199 return {
200 'format_id': format_id,
201 'vcodec': 'h265' if codec == 'bytevc1' else codec,
202 'tbr': int_or_none(bitrate, scale=1000) or None,
203 'quality': qualities(self.QUALITIES)(res),
204 }, res
205
206 known_resolutions = {}
207
b09bd0c1 208 def audio_meta(url):
209 ext = determine_ext(url, default_ext='m4a')
8ceb07e8 210 return {
211 'format_note': 'Music track',
b09bd0c1 212 'ext': ext,
213 'acodec': 'aac' if ext == 'm4a' else ext,
8ceb07e8 214 'vcodec': 'none',
215 'width': None,
216 'height': None,
b09bd0c1 217 } if ext == 'mp3' or '-music-' in url else {}
8ceb07e8 218
bd9ff55b
M
219 def extract_addr(addr, add_meta={}):
220 parsed_meta, res = parse_url_key(addr.get('url_key', ''))
221 if res:
ab6057ec 222 known_resolutions.setdefault(res, {}).setdefault('height', add_meta.get('height') or addr.get('height'))
223 known_resolutions[res].setdefault('width', add_meta.get('width') or addr.get('width'))
bd9ff55b
M
224 parsed_meta.update(known_resolutions.get(res, {}))
225 add_meta.setdefault('height', int_or_none(res[:-1]))
226 return [{
227 'url': url,
228 'filesize': int_or_none(addr.get('data_size')),
229 'ext': 'mp4',
230 'acodec': 'aac',
0fd6661e
M
231 'source_preference': -2 if 'aweme/v1' in url else -1, # Downloads from API might get blocked
232 **add_meta, **parsed_meta,
34921b43 233 'format_note': join_nonempty(
8ceb07e8 234 add_meta.get('format_note'), '(API)' if 'aweme/v1' in url else None, delim=' '),
b09bd0c1 235 **audio_meta(url),
bd9ff55b
M
236 } for url in addr.get('url_list') or []]
237
238 # Hack: Add direct video links first to prioritize them when removing duplicate formats
239 formats = []
240 if video_info.get('play_addr'):
241 formats.extend(extract_addr(video_info['play_addr'], {
242 'format_id': 'play_addr',
243 'format_note': 'Direct video',
244 'vcodec': 'h265' if traverse_obj(
be1f331f 245 video_info, 'is_bytevc1', 'is_h265') else 'h264', # TODO: Check for "direct iOS" videos, like https://www.tiktok.com/@cookierun_dev/video/7039716639834656002
bd9ff55b
M
246 'width': video_info.get('width'),
247 'height': video_info.get('height'),
248 }))
249 if video_info.get('download_addr'):
250 formats.extend(extract_addr(video_info['download_addr'], {
251 'format_id': 'download_addr',
252 'format_note': 'Download video%s' % (', watermarked' if video_info.get('has_watermark') else ''),
253 'vcodec': 'h264',
254 'width': video_info.get('width'),
255 'height': video_info.get('height'),
0fd6661e 256 'preference': -2 if video_info.get('has_watermark') else -1,
bd9ff55b
M
257 }))
258 if video_info.get('play_addr_h264'):
259 formats.extend(extract_addr(video_info['play_addr_h264'], {
260 'format_id': 'play_addr_h264',
261 'format_note': 'Direct video',
262 'vcodec': 'h264',
263 }))
264 if video_info.get('play_addr_bytevc1'):
265 formats.extend(extract_addr(video_info['play_addr_bytevc1'], {
266 'format_id': 'play_addr_bytevc1',
267 'format_note': 'Direct video',
268 'vcodec': 'h265',
269 }))
270
271 for bitrate in video_info.get('bit_rate', []):
272 if bitrate.get('play_addr'):
273 formats.extend(extract_addr(bitrate['play_addr'], {
274 'format_id': bitrate.get('gear_name'),
275 'format_note': 'Playback video',
276 'tbr': try_get(bitrate, lambda x: x['bit_rate'] / 1000),
277 'vcodec': 'h265' if traverse_obj(
278 bitrate, 'is_bytevc1', 'is_h265') else 'h264',
943d5ab1 279 'fps': bitrate.get('FPS'),
bd9ff55b
M
280 }))
281
282 self._remove_duplicate_formats(formats)
6134fbeb
M
283 auth_cookie = self._get_cookies(self._WEBPAGE_HOST).get('sid_tt')
284 if auth_cookie:
285 for f in formats:
be1f331f 286 self._set_cookie(compat_urllib_parse_urlparse(f['url']).hostname, 'sid_tt', auth_cookie.value)
bd9ff55b
M
287
288 thumbnails = []
289 for cover_id in ('cover', 'ai_dynamic_cover', 'animated_cover', 'ai_dynamic_cover_bak',
290 'origin_cover', 'dynamic_cover'):
92593690 291 for cover_url in traverse_obj(video_info, (cover_id, 'url_list', ...)):
292 thumbnails.append({
293 'id': cover_id,
294 'url': cover_url,
295 })
296
297 stats_info = aweme_detail.get('statistics') or {}
298 author_info = aweme_detail.get('author') or {}
299 music_info = aweme_detail.get('music') or {}
943d5ab1
M
300 user_url = self._UPLOADER_URL_FORMAT % (traverse_obj(author_info,
301 'sec_uid', 'id', 'uid', 'unique_id',
302 expected_type=str_or_none, get_all=False))
6839ae1f 303 labels = traverse_obj(aweme_detail, ('hybrid_label', ..., 'text'), expected_type=str)
bd9ff55b
M
304
305 contained_music_track = traverse_obj(
306 music_info, ('matched_song', 'title'), ('matched_pgc_sound', 'title'), expected_type=str)
307 contained_music_author = traverse_obj(
308 music_info, ('matched_song', 'author'), ('matched_pgc_sound', 'author'), 'author', expected_type=str)
309
310 is_generic_og_trackname = music_info.get('is_original_sound') and music_info.get('title') == 'original sound - %s' % music_info.get('owner_handle')
311 if is_generic_og_trackname:
312 music_track, music_author = contained_music_track or 'original sound', contained_music_author
313 else:
314 music_track, music_author = music_info.get('title'), music_info.get('author')
315
316 return {
317 'id': aweme_id,
a39a7ba8 318 'extractor_key': TikTokIE.ie_key(),
319 'extractor': TikTokIE.IE_NAME,
320 'webpage_url': self._create_url(author_info.get('uid'), aweme_id),
92593690 321 **traverse_obj(aweme_detail, {
322 'title': ('desc', {str}),
323 'description': ('desc', {str}),
324 'timestamp': ('create_time', {int_or_none}),
325 }),
326 **traverse_obj(stats_info, {
327 'view_count': 'play_count',
328 'like_count': 'digg_count',
329 'repost_count': 'share_count',
330 'comment_count': 'comment_count',
331 }, expected_type=int_or_none),
332 **traverse_obj(author_info, {
333 'uploader': 'unique_id',
334 'uploader_id': 'uid',
335 'creator': 'nickname',
336 'channel_id': 'sec_uid',
337 }, expected_type=str_or_none),
943d5ab1 338 'uploader_url': user_url,
bd9ff55b
M
339 'track': music_track,
340 'album': str_or_none(music_info.get('album')) or None,
f7c5a5e9 341 'artist': music_author or None,
bd9ff55b 342 'formats': formats,
e0585e65 343 'subtitles': self.extract_subtitles(aweme_detail, aweme_id),
bd9ff55b 344 'thumbnails': thumbnails,
53dad39e
M
345 'duration': int_or_none(traverse_obj(video_info, 'duration', ('download_addr', 'duration')), scale=1000),
346 'availability': self._availability(
347 is_private='Private' in labels,
348 needs_subscription='Friends only' in labels,
9f14daf2 349 is_unlisted='Followers only' in labels),
350 '_format_sort_fields': ('quality', 'codec', 'size', 'br'),
bd9ff55b
M
351 }
352
92593690 353 def _parse_aweme_video_web(self, aweme_detail, webpage_url, video_id):
943d5ab1 354 video_info = aweme_detail['video']
11aa91a1 355 author_info = traverse_obj(aweme_detail, 'authorInfo', 'author', expected_type=dict, default={})
943d5ab1
M
356 music_info = aweme_detail.get('music') or {}
357 stats_info = aweme_detail.get('stats') or {}
92593690 358 channel_id = traverse_obj(author_info or aweme_detail, (('authorSecId', 'secUid'), {str}), get_all=False)
359 user_url = self._UPLOADER_URL_FORMAT % channel_id if channel_id else None
943d5ab1
M
360
361 formats = []
92593690 362 width = int_or_none(video_info.get('width'))
363 height = int_or_none(video_info.get('height'))
364
365 for play_url in traverse_obj(video_info, ('playAddr', ((..., 'src'), None), {url_or_none})):
366 formats.append({
943d5ab1
M
367 'url': self._proto_relative_url(play_url),
368 'ext': 'mp4',
369 'width': width,
370 'height': height,
92593690 371 })
943d5ab1 372
92593690 373 for download_url in traverse_obj(video_info, (('downloadAddr', ('download', 'url')), {url_or_none})):
943d5ab1
M
374 formats.append({
375 'format_id': 'download',
376 'url': self._proto_relative_url(download_url),
377 'ext': 'mp4',
378 'width': width,
379 'height': height,
380 })
92593690 381
943d5ab1 382 self._remove_duplicate_formats(formats)
943d5ab1
M
383
384 thumbnails = []
92593690 385 for thumb_url in traverse_obj(aweme_detail, (
386 (None, 'video'), ('thumbnail', 'cover', 'dynamicCover', 'originCover'), {url_or_none})):
387 thumbnails.append({
388 'url': self._proto_relative_url(thumb_url),
389 'width': width,
390 'height': height,
391 })
943d5ab1
M
392
393 return {
92593690 394 'id': video_id,
395 **traverse_obj(aweme_detail, {
396 'title': ('desc', {str}),
397 'description': ('desc', {str}),
398 'duration': ('video', 'duration', {int_or_none}),
399 'timestamp': ('createTime', {int_or_none}),
400 }),
401 **traverse_obj(author_info or aweme_detail, {
402 'creator': ('nickname', {str}),
403 'uploader': (('uniqueId', 'author'), {str}),
404 'uploader_id': (('authorId', 'uid', 'id'), {str_or_none}),
405 }, get_all=False),
406 **traverse_obj(stats_info, {
407 'view_count': 'playCount',
408 'like_count': 'diggCount',
409 'repost_count': 'shareCount',
410 'comment_count': 'commentCount',
411 }, expected_type=int_or_none),
412 **traverse_obj(music_info, {
413 'track': 'title',
414 'album': ('album', {lambda x: x or None}),
415 'artist': 'authorName',
416 }, expected_type=str),
417 'channel_id': channel_id,
943d5ab1 418 'uploader_url': user_url,
943d5ab1
M
419 'formats': formats,
420 'thumbnails': thumbnails,
943d5ab1 421 'http_headers': {
92593690 422 'Referer': webpage_url,
943d5ab1
M
423 }
424 }
425
0fd6661e
M
426
427class TikTokIE(TikTokBaseIE):
c4cbd3be 428 _VALID_URL = r'https?://www\.tiktok\.com/(?:embed|@(?P<user_id>[\w\.-]+)?/video)/(?P<id>\d+)'
bfd973ec 429 _EMBED_REGEX = [rf'<(?:script|iframe)[^>]+\bsrc=(["\'])(?P<url>{_VALID_URL})']
0fd6661e
M
430
431 _TESTS = [{
432 'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610',
0481e266 433 'md5': '736bb7a466c6f0a6afeb597da1e6f5b7',
0fd6661e
M
434 'info_dict': {
435 'id': '6748451240264420610',
436 'ext': 'mp4',
437 'title': '#jassmanak #lehanga #leenabhushan',
438 'description': '#jassmanak #lehanga #leenabhushan',
439 'duration': 13,
0481e266 440 'height': 1024,
441 'width': 576,
0fd6661e
M
442 'uploader': 'leenabhushan',
443 'uploader_id': '6691488002098119685',
0481e266 444 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA_Eb4t1vodM1IuTy_cvp9CY22RAb59xqrO0Xtz9CYQJvgXaDvZxYnZYRzDWhhgJmy',
0fd6661e
M
445 'creator': 'facestoriesbyleenabh',
446 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
447 'upload_date': '20191016',
448 'timestamp': 1571246252,
449 'view_count': int,
450 'like_count': int,
451 'repost_count': int,
452 'comment_count': int,
a44ca5a4 453 'artist': 'Ysrbeats',
454 'album': 'Lehanga',
455 'track': 'Lehanga',
92593690 456 },
457 'skip': '404 Not Found',
0fd6661e
M
458 }, {
459 'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en',
0481e266 460 'md5': '6f3cf8cdd9b28cb8363fe0a9a160695b',
0fd6661e
M
461 'info_dict': {
462 'id': '6742501081818877190',
463 'ext': 'mp4',
464 'title': 'md5:5e2a23877420bb85ce6521dbee39ba94',
465 'description': 'md5:5e2a23877420bb85ce6521dbee39ba94',
466 'duration': 27,
467 'height': 960,
468 'width': 540,
469 'uploader': 'patrox',
470 'uploader_id': '18702747',
0481e266 471 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws',
92593690 472 'channel_id': 'MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws',
0fd6661e
M
473 'creator': 'patroX',
474 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
475 'upload_date': '20190930',
476 'timestamp': 1569860870,
477 'view_count': int,
478 'like_count': int,
479 'repost_count': int,
480 'comment_count': int,
a44ca5a4 481 'artist': 'Evan Todd, Jessica Keenan Wynn, Alice Lee, Barrett Wilbert Weed & Jon Eidson',
482 'track': 'Big Fun',
92593690 483 },
0fd6661e 484 }, {
96f13f01
M
485 # Banned audio, only available on the app
486 'url': 'https://www.tiktok.com/@barudakhb_/video/6984138651336838402',
487 'info_dict': {
488 'id': '6984138651336838402',
489 'ext': 'mp4',
490 'title': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥',
491 'description': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥',
492 'uploader': 'barudakhb_',
493 'creator': 'md5:29f238c49bc0c176cb3cef1a9cea9fa6',
494 'uploader_id': '6974687867511718913',
495 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAbhBwQC-R1iKoix6jDFsF-vBdfx2ABoDjaZrM9fX6arU3w71q3cOWgWuTXn1soZ7d',
92593690 496 'channel_id': 'MS4wLjABAAAAbhBwQC-R1iKoix6jDFsF-vBdfx2ABoDjaZrM9fX6arU3w71q3cOWgWuTXn1soZ7d',
96f13f01
M
497 'track': 'Boka Dance',
498 'artist': 'md5:29f238c49bc0c176cb3cef1a9cea9fa6',
499 'timestamp': 1626121503,
500 'duration': 18,
501 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
502 'upload_date': '20210712',
503 'view_count': int,
504 'like_count': int,
505 'repost_count': int,
506 'comment_count': int,
92593690 507 },
96f13f01
M
508 }, {
509 # Sponsored video, only available with feed workaround
510 'url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_/video/7042692929109986561',
511 'info_dict': {
512 'id': '7042692929109986561',
513 'ext': 'mp4',
514 'title': 'Slap and Run!',
515 'description': 'Slap and Run!',
516 'uploader': 'user440922249',
517 'creator': 'Slap And Run',
518 'uploader_id': '7036055384943690754',
519 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_',
92593690 520 'channel_id': 'MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_',
96f13f01
M
521 'track': 'Promoted Music',
522 'timestamp': 1639754738,
523 'duration': 30,
524 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
525 'upload_date': '20211217',
526 'view_count': int,
527 'like_count': int,
528 'repost_count': int,
529 'comment_count': int,
530 },
b09bd0c1 531 'params': {'skip_download': True}, # XXX: unable to download video data: HTTP Error 403: Forbidden
5fa3c9a8
HTL
532 }, {
533 # Video without title and description
534 'url': 'https://www.tiktok.com/@pokemonlife22/video/7059698374567611694',
535 'info_dict': {
536 'id': '7059698374567611694',
537 'ext': 'mp4',
b801cd71 538 'title': 'TikTok video #7059698374567611694',
5fa3c9a8
HTL
539 'description': '',
540 'uploader': 'pokemonlife22',
541 'creator': 'Pokemon',
542 'uploader_id': '6820838815978423302',
543 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA0tF1nBwQVVMyrGu3CqttkNgM68Do1OXUFuCY0CRQk8fEtSVDj89HqoqvbSTmUP2W',
92593690 544 'channel_id': 'MS4wLjABAAAA0tF1nBwQVVMyrGu3CqttkNgM68Do1OXUFuCY0CRQk8fEtSVDj89HqoqvbSTmUP2W',
5fa3c9a8
HTL
545 'track': 'original sound',
546 'timestamp': 1643714123,
547 'duration': 6,
548 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
549 'upload_date': '20220201',
550 'artist': 'Pokemon',
551 'view_count': int,
552 'like_count': int,
553 'repost_count': int,
554 'comment_count': int,
555 },
a39a7ba8 556 }, {
557 # hydration JSON is sent in a <script> element
558 'url': 'https://www.tiktok.com/@denidil6/video/7065799023130643713',
559 'info_dict': {
560 'id': '7065799023130643713',
561 'ext': 'mp4',
562 'title': '#denidil#денидил',
563 'description': '#denidil#денидил',
564 'uploader': 'denidil6',
565 'uploader_id': '7046664115636405250',
566 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAsvMSzFdQ4ikl3uR2TEJwMBbB2yZh2Zxwhx-WCo3rbDpAharE3GQCrFuJArI3C8QJ',
567 'artist': 'Holocron Music',
568 'album': 'Wolf Sounds (1 Hour) Enjoy the Company of the Animal That Is the Majestic King of the Night',
569 'track': 'Wolf Sounds (1 Hour) Enjoy the Company of the Animal That Is the Majestic King of the Night',
570 'timestamp': 1645134536,
571 'duration': 26,
572 'upload_date': '20220217',
573 'view_count': int,
574 'like_count': int,
575 'repost_count': int,
576 'comment_count': int,
577 },
f7c5a5e9 578 'skip': 'This video is unavailable',
8ceb07e8 579 }, {
580 # slideshow audio-only mp3 format
581 'url': 'https://www.tiktok.com/@_le_cannibale_/video/7139980461132074283',
582 'info_dict': {
583 'id': '7139980461132074283',
584 'ext': 'mp3',
585 'title': 'TikTok video #7139980461132074283',
586 'description': '',
587 'creator': 'Antaura',
588 'uploader': '_le_cannibale_',
589 'uploader_id': '6604511138619654149',
590 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAoShJqaw_5gvy48y3azFeFcT4jeyKWbB0VVYasOCt2tTLwjNFIaDcHAM4D-QGXFOP',
92593690 591 'channel_id': 'MS4wLjABAAAAoShJqaw_5gvy48y3azFeFcT4jeyKWbB0VVYasOCt2tTLwjNFIaDcHAM4D-QGXFOP',
8ceb07e8 592 'artist': 'nathan !',
593 'track': 'grahamscott canon',
594 'upload_date': '20220905',
595 'timestamp': 1662406249,
596 'view_count': int,
597 'like_count': int,
598 'repost_count': int,
599 'comment_count': int,
600 'thumbnail': r're:^https://.+\.webp',
601 },
92593690 602 }, {
603 # only available via web
604 'url': 'https://www.tiktok.com/@moxypatch/video/7206382937372134662',
b09bd0c1 605 'md5': '6aba7fad816e8709ff2c149679ace165',
92593690 606 'info_dict': {
607 'id': '7206382937372134662',
608 'ext': 'mp4',
609 'title': 'md5:1d95c0b96560ca0e8a231af4172b2c0a',
610 'description': 'md5:1d95c0b96560ca0e8a231af4172b2c0a',
611 'creator': 'MoxyPatch',
612 'uploader': 'moxypatch',
613 'uploader_id': '7039142049363379205',
614 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAFhqKnngMHJSsifL0w1vFOP5kn3Ndo1ODp0XuIBkNMBCkALTvwILdpu12g3pTtL4V',
615 'channel_id': 'MS4wLjABAAAAFhqKnngMHJSsifL0w1vFOP5kn3Ndo1ODp0XuIBkNMBCkALTvwILdpu12g3pTtL4V',
616 'artist': 'your worst nightmare',
617 'track': 'original sound',
618 'upload_date': '20230303',
619 'timestamp': 1677866781,
620 'duration': 10,
621 'view_count': int,
622 'like_count': int,
623 'repost_count': int,
624 'comment_count': int,
625 'thumbnail': r're:^https://.+',
626 'thumbnails': 'count:3',
627 },
628 'expected_warnings': ['Unable to find video in feed'],
c2a1bdb0 629 }, {
630 # 1080p format
631 'url': 'https://www.tiktok.com/@tatemcrae/video/7107337212743830830',
632 'md5': '982512017a8a917124d5a08c8ae79621',
633 'info_dict': {
634 'id': '7107337212743830830',
635 'ext': 'mp4',
636 'title': 'new music video 4 don’t come backkkk🧸🖤 i hope u enjoy !! @musicontiktok',
637 'description': 'new music video 4 don’t come backkkk🧸🖤 i hope u enjoy !! @musicontiktok',
638 'uploader': 'tatemcrae',
639 'uploader_id': '86328792343818240',
640 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA-0bQT0CqebTRr6I4IkYvMDMKSRSJHLNPBo5HrSklJwyA2psXLSZG5FP-LMNpHnJd',
641 'channel_id': 'MS4wLjABAAAA-0bQT0CqebTRr6I4IkYvMDMKSRSJHLNPBo5HrSklJwyA2psXLSZG5FP-LMNpHnJd',
b09bd0c1 642 'creator': 'tate mcrae',
643 'artist': 'tate mcrae',
c2a1bdb0 644 'track': 'original sound',
645 'upload_date': '20220609',
646 'timestamp': 1654805899,
647 'duration': 150,
648 'view_count': int,
649 'like_count': int,
650 'repost_count': int,
651 'comment_count': int,
652 'thumbnail': r're:^https://.+\.webp',
653 },
654 'params': {'format': 'bytevc1_1080p_808907-0'},
b09bd0c1 655 }, {
656 # Slideshow, audio-only m4a format
657 'url': 'https://www.tiktok.com/@hara_yoimiya/video/7253412088251534594',
658 'md5': '2ff8fe0174db2dbf49c597a7bef4e47d',
659 'info_dict': {
660 'id': '7253412088251534594',
661 'ext': 'm4a',
662 'title': 'я ред флаг простите #переписка #щитпост #тревожныйтиппривязанности #рекомендации ',
663 'description': 'я ред флаг простите #переписка #щитпост #тревожныйтиппривязанности #рекомендации ',
664 'uploader': 'hara_yoimiya',
665 'uploader_id': '6582536342634676230',
666 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAIAlDxriiPWLE-p8p1R_0Bx8qWKfi-7zwmGhzU8Mv25W8sNxjfIKrol31qTczzuLB',
667 'channel_id': 'MS4wLjABAAAAIAlDxriiPWLE-p8p1R_0Bx8qWKfi-7zwmGhzU8Mv25W8sNxjfIKrol31qTczzuLB',
668 'creator': 'лампочка',
669 'artist': 'Øneheart',
670 'album': 'watching the stars',
671 'track': 'watching the stars',
672 'upload_date': '20230708',
673 'timestamp': 1688816612,
674 'view_count': int,
675 'like_count': int,
676 'comment_count': int,
677 'repost_count': int,
678 'thumbnail': r're:^https://.+\.webp',
679 },
e0585e65
M
680 }, {
681 # Auto-captions available
682 'url': 'https://www.tiktok.com/@hankgreen1/video/7047596209028074758',
683 'only_matching': True
0fd6661e
M
684 }]
685
ce18a19b 686 def _real_extract(self, url):
b801cd71 687 video_id, user_id = self._match_valid_url(url).group('id', 'user_id')
bd9ff55b
M
688 try:
689 return self._extract_aweme_app(video_id)
690 except ExtractorError as e:
a39a7ba8 691 self.report_warning(f'{e}; trying with webpage')
bd9ff55b 692
a39a7ba8 693 url = self._create_url(user_id, video_id)
216bcb66 694 webpage = self._download_webpage(url, video_id, headers={'User-Agent': 'Mozilla/5.0'})
135dfa2c 695 next_data = self._search_nextjs_data(webpage, video_id, default='{}')
135dfa2c 696 if next_data:
11aa91a1
M
697 status = traverse_obj(next_data, ('props', 'pageProps', 'statusCode'), expected_type=int) or 0
698 video_data = traverse_obj(next_data, ('props', 'pageProps', 'itemInfo', 'itemStruct'), expected_type=dict)
699 else:
a39a7ba8 700 sigi_data = self._get_sigi_state(webpage, video_id)
11aa91a1
M
701 status = traverse_obj(sigi_data, ('VideoPage', 'statusCode'), expected_type=int) or 0
702 video_data = traverse_obj(sigi_data, ('ItemModule', video_id), expected_type=dict)
703
1418a043 704 if status == 0:
92593690 705 return self._parse_aweme_video_web(video_data, url, video_id)
1418a043 706 elif status == 10216:
707 raise ExtractorError('This video is private', expected=True)
6fb11ca8 708 raise ExtractorError('Video not available', video_id=video_id)
f7f18f90
A
709
710
0fd6661e 711class TikTokUserIE(TikTokBaseIE):
f7f18f90 712 IE_NAME = 'tiktok:user'
0fd6661e 713 _VALID_URL = r'https?://(?:www\.)?tiktok\.com/@(?P<id>[\w\.-]+)/?(?:$|[#?])'
f7c5a5e9 714 _WORKING = False
f7f18f90 715 _TESTS = [{
526d74ec 716 'url': 'https://tiktok.com/@corgibobaa?lang=en',
f7f18f90
A
717 'playlist_mincount': 45,
718 'info_dict': {
719 'id': '6935371178089399301',
0481e266 720 'title': 'corgibobaa',
b3187433 721 'thumbnail': r're:https://.+_1080x1080\.webp'
f7f18f90 722 },
0481e266 723 'expected_warnings': ['Retrying']
5fa3c9a8
HTL
724 }, {
725 'url': 'https://www.tiktok.com/@6820838815978423302',
726 'playlist_mincount': 5,
727 'info_dict': {
728 'id': '6820838815978423302',
729 'title': '6820838815978423302',
730 'thumbnail': r're:https://.+_1080x1080\.webp'
731 },
732 'expected_warnings': ['Retrying']
f7f18f90
A
733 }, {
734 'url': 'https://www.tiktok.com/@meme',
735 'playlist_mincount': 593,
736 'info_dict': {
737 'id': '79005827461758976',
0481e266 738 'title': 'meme',
b3187433 739 'thumbnail': r're:https://.+_1080x1080\.webp'
f7f18f90 740 },
0481e266 741 'expected_warnings': ['Retrying']
f7f18f90
A
742 }]
743
0fd6661e
M
744 r''' # TODO: Fix by adding _signature to api_url
745 def _entries(self, webpage, user_id, username):
746 secuid = self._search_regex(r'\"secUid\":\"(?P<secUid>[^\"]+)', webpage, username)
f7f18f90
A
747 verifyfp_cookie = self._get_cookies('https://www.tiktok.com').get('s_v_web_id')
748 if not verifyfp_cookie:
749 raise ExtractorError('Improper cookies (missing s_v_web_id).', expected=True)
750 api_url = f'https://m.tiktok.com/api/post/item_list/?aid=1988&cookie_enabled=true&count=30&verifyFp={verifyfp_cookie.value}&secUid={secuid}&cursor='
751 cursor = '0'
752 for page in itertools.count():
0fd6661e 753 data_json = self._download_json(api_url + cursor, username, note='Downloading Page %d' % page)
f7f18f90
A
754 for video in data_json.get('itemList', []):
755 video_id = video['id']
756 video_url = f'https://www.tiktok.com/@{user_id}/video/{video_id}'
bd9ff55b 757 yield self._url_result(video_url, 'TikTok', video_id, str_or_none(video.get('desc')))
526d74ec 758 if not data_json.get('hasMore'):
f7f18f90
A
759 break
760 cursor = data_json['cursor']
0fd6661e
M
761 '''
762
b3187433 763 def _video_entries_api(self, webpage, user_id, username):
0fd6661e
M
764 query = {
765 'user_id': user_id,
766 'count': 21,
767 'max_cursor': 0,
768 'min_cursor': 0,
769 'retry_type': 'no_retry',
efa944f4 770 'device_id': ''.join(random.choices(string.digits, k=19)), # Some endpoints don't like randomized device_id, so it isn't directly set in _call_api.
0fd6661e
M
771 }
772
0fd6661e 773 for page in itertools.count(1):
be5c1ae8 774 for retry in self.RetryManager():
0fd6661e 775 try:
be5c1ae8 776 post_list = self._call_api(
777 'aweme/post', query, username, note=f'Downloading user video list page {page}',
778 errnote='Unable to download user video list')
0fd6661e 779 except ExtractorError as e:
be5c1ae8 780 if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0:
781 retry.error = e
0fd6661e
M
782 continue
783 raise
b3187433 784 yield from post_list.get('aweme_list', [])
0fd6661e
M
785 if not post_list.get('has_more'):
786 break
787 query['max_cursor'] = post_list['max_cursor']
f7f18f90 788
b3187433 789 def _entries_api(self, user_id, videos):
790 for video in videos:
791 yield {
792 **self._parse_aweme_video_app(video),
793 'extractor_key': TikTokIE.ie_key(),
794 'extractor': 'TikTok',
795 'webpage_url': f'https://tiktok.com/@{user_id}/video/{video["aweme_id"]}',
796 }
797
f7f18f90 798 def _real_extract(self, url):
0481e266 799 user_name = self._match_id(url)
800 webpage = self._download_webpage(url, user_name, headers={
0fd6661e
M
801 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)'
802 })
5fa3c9a8 803 user_id = self._html_search_regex(r'snssdk\d*://user/profile/(\d+)', webpage, 'user ID', default=None) or user_name
b3187433 804
805 videos = LazyList(self._video_entries_api(webpage, user_id, user_name))
806 thumbnail = traverse_obj(videos, (0, 'author', 'avatar_larger', 'url_list', 0))
807
808 return self.playlist_result(self._entries_api(user_id, videos), user_id, user_name, thumbnail=thumbnail)
943d5ab1
M
809
810
6368e2e6 811class TikTokBaseListIE(TikTokBaseIE): # XXX: Conventionally, base classes should end with BaseIE/InfoExtractor
8126298c
M
812 def _entries(self, list_id, display_id):
813 query = {
814 self._QUERY_NAME: list_id,
815 'cursor': 0,
816 'count': 20,
817 'type': 5,
efa944f4 818 'device_id': ''.join(random.choices(string.digits, k=19))
8126298c
M
819 }
820
8126298c 821 for page in itertools.count(1):
be5c1ae8 822 for retry in self.RetryManager():
8126298c 823 try:
be5c1ae8 824 post_list = self._call_api(
825 self._API_ENDPOINT, query, display_id, note=f'Downloading video list page {page}',
826 errnote='Unable to download video list')
8126298c 827 except ExtractorError as e:
be5c1ae8 828 if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0:
829 retry.error = e
8126298c
M
830 continue
831 raise
8126298c
M
832 for video in post_list.get('aweme_list', []):
833 yield {
834 **self._parse_aweme_video_app(video),
0b77924a 835 'extractor_key': TikTokIE.ie_key(),
8126298c
M
836 'extractor': 'TikTok',
837 'webpage_url': f'https://tiktok.com/@_/video/{video["aweme_id"]}',
838 }
839 if not post_list.get('has_more'):
840 break
841 query['cursor'] = post_list['cursor']
842
843 def _real_extract(self, url):
844 list_id = self._match_id(url)
845 return self.playlist_result(self._entries(list_id, list_id), list_id)
846
847
848class TikTokSoundIE(TikTokBaseListIE):
849 IE_NAME = 'tiktok:sound'
850 _VALID_URL = r'https?://(?:www\.)?tiktok\.com/music/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
f7c5a5e9 851 _WORKING = False
8126298c
M
852 _QUERY_NAME = 'music_id'
853 _API_ENDPOINT = 'music/aweme'
854 _TESTS = [{
855 'url': 'https://www.tiktok.com/music/Build-a-Btch-6956990112127585029?lang=en',
856 'playlist_mincount': 100,
857 'info_dict': {
858 'id': '6956990112127585029'
859 },
860 'expected_warnings': ['Retrying']
861 }, {
862 # Actual entries are less than listed video count
863 'url': 'https://www.tiktok.com/music/jiefei-soap-remix-7036843036118469381',
864 'playlist_mincount': 2182,
865 'info_dict': {
866 'id': '7036843036118469381'
867 },
868 'expected_warnings': ['Retrying']
869 }]
870
871
872class TikTokEffectIE(TikTokBaseListIE):
873 IE_NAME = 'tiktok:effect'
874 _VALID_URL = r'https?://(?:www\.)?tiktok\.com/sticker/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
f7c5a5e9 875 _WORKING = False
8126298c
M
876 _QUERY_NAME = 'sticker_id'
877 _API_ENDPOINT = 'sticker/aweme'
878 _TESTS = [{
879 'url': 'https://www.tiktok.com/sticker/MATERIAL-GWOOORL-1258156',
880 'playlist_mincount': 100,
881 'info_dict': {
882 'id': '1258156',
883 },
884 'expected_warnings': ['Retrying']
885 }, {
886 # Different entries between mobile and web, depending on region
887 'url': 'https://www.tiktok.com/sticker/Elf-Friend-479565',
888 'only_matching': True
889 }]
890
891
892class TikTokTagIE(TikTokBaseListIE):
893 IE_NAME = 'tiktok:tag'
894 _VALID_URL = r'https?://(?:www\.)?tiktok\.com/tag/(?P<id>[^/?#&]+)'
f7c5a5e9 895 _WORKING = False
8126298c
M
896 _QUERY_NAME = 'ch_id'
897 _API_ENDPOINT = 'challenge/aweme'
898 _TESTS = [{
899 'url': 'https://tiktok.com/tag/hello2018',
900 'playlist_mincount': 39,
901 'info_dict': {
902 'id': '46294678',
903 'title': 'hello2018',
904 },
905 'expected_warnings': ['Retrying']
906 }, {
907 'url': 'https://tiktok.com/tag/fypシ?is_copy_url=0&is_from_webapp=v1',
908 'only_matching': True
909 }]
910
911 def _real_extract(self, url):
912 display_id = self._match_id(url)
913 webpage = self._download_webpage(url, display_id, headers={
914 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)'
915 })
916 tag_id = self._html_search_regex(r'snssdk\d*://challenge/detail/(\d+)', webpage, 'tag ID')
917 return self.playlist_result(self._entries(tag_id, display_id), tag_id, display_id)
918
919
ba723997 920class DouyinIE(TikTokBaseIE):
943d5ab1
M
921 _VALID_URL = r'https?://(?:www\.)?douyin\.com/video/(?P<id>[0-9]+)'
922 _TESTS = [{
923 'url': 'https://www.douyin.com/video/6961737553342991651',
ba723997 924 'md5': 'a97db7e3e67eb57bf40735c022ffa228',
943d5ab1
M
925 'info_dict': {
926 'id': '6961737553342991651',
927 'ext': 'mp4',
928 'title': '#杨超越 小小水手带你去远航❤️',
ba723997 929 'description': '#杨超越 小小水手带你去远航❤️',
943d5ab1 930 'uploader_id': '110403406559',
ba723997 931 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
92593690 932 'channel_id': 'MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
ba723997 933 'creator': '杨超越',
934 'duration': 19782,
935 'timestamp': 1620905839,
936 'upload_date': '20210513',
937 'track': '@杨超越创作的原声',
943d5ab1
M
938 'view_count': int,
939 'like_count': int,
940 'repost_count': int,
941 'comment_count': int,
92593690 942 'thumbnail': r're:https?://.+\.jpe?g',
ba723997 943 },
943d5ab1
M
944 }, {
945 'url': 'https://www.douyin.com/video/6982497745948921092',
ba723997 946 'md5': '34a87ebff3833357733da3fe17e37c0e',
943d5ab1
M
947 'info_dict': {
948 'id': '6982497745948921092',
949 'ext': 'mp4',
950 'title': '这个夏日和小羊@杨超越 一起遇见白色幻想',
ba723997 951 'description': '这个夏日和小羊@杨超越 一起遇见白色幻想',
943d5ab1 952 'uploader_id': '408654318141572',
ba723997 953 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAZJpnglcjW2f_CMVcnqA_6oVBXKWMpH0F8LIHuUu8-lA',
92593690 954 'channel_id': 'MS4wLjABAAAAZJpnglcjW2f_CMVcnqA_6oVBXKWMpH0F8LIHuUu8-lA',
ba723997 955 'creator': '杨超越工作室',
92593690 956 'duration': 42479,
ba723997 957 'timestamp': 1625739481,
958 'upload_date': '20210708',
959 'track': '@杨超越工作室创作的原声',
943d5ab1
M
960 'view_count': int,
961 'like_count': int,
962 'repost_count': int,
963 'comment_count': int,
92593690 964 'thumbnail': r're:https?://.+\.jpe?g',
ba723997 965 },
943d5ab1
M
966 }, {
967 'url': 'https://www.douyin.com/video/6953975910773099811',
ba723997 968 'md5': 'dde3302460f19db59c47060ff013b902',
943d5ab1
M
969 'info_dict': {
970 'id': '6953975910773099811',
971 'ext': 'mp4',
972 'title': '#一起看海 出现在你的夏日里',
ba723997 973 'description': '#一起看海 出现在你的夏日里',
943d5ab1 974 'uploader_id': '110403406559',
ba723997 975 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
92593690 976 'channel_id': 'MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
ba723997 977 'creator': '杨超越',
92593690 978 'duration': 17343,
ba723997 979 'timestamp': 1619098692,
980 'upload_date': '20210422',
981 'track': '@杨超越创作的原声',
943d5ab1
M
982 'view_count': int,
983 'like_count': int,
984 'repost_count': int,
985 'comment_count': int,
92593690 986 'thumbnail': r're:https?://.+\.jpe?g',
ba723997 987 },
943d5ab1
M
988 }, {
989 'url': 'https://www.douyin.com/video/6950251282489675042',
990 'md5': 'b4db86aec367ef810ddd38b1737d2fed',
991 'info_dict': {
992 'id': '6950251282489675042',
993 'ext': 'mp4',
994 'title': '哈哈哈,成功了哈哈哈哈哈哈',
995 'uploader': '杨超越',
996 'upload_date': '20210412',
997 'timestamp': 1618231483,
998 'uploader_id': '110403406559',
999 'view_count': int,
1000 'like_count': int,
1001 'repost_count': int,
1002 'comment_count': int,
ba723997 1003 },
1004 'skip': 'No longer available',
943d5ab1
M
1005 }, {
1006 'url': 'https://www.douyin.com/video/6963263655114722595',
ba723997 1007 'md5': 'cf9f11f0ec45d131445ec2f06766e122',
943d5ab1
M
1008 'info_dict': {
1009 'id': '6963263655114722595',
1010 'ext': 'mp4',
1011 'title': '#哪个爱豆的105度最甜 换个角度看看我哈哈',
ba723997 1012 'description': '#哪个爱豆的105度最甜 换个角度看看我哈哈',
943d5ab1 1013 'uploader_id': '110403406559',
ba723997 1014 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
92593690 1015 'channel_id': 'MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
ba723997 1016 'creator': '杨超越',
1017 'duration': 15115,
1018 'timestamp': 1621261163,
1019 'upload_date': '20210517',
1020 'track': '@杨超越创作的原声',
943d5ab1
M
1021 'view_count': int,
1022 'like_count': int,
1023 'repost_count': int,
1024 'comment_count': int,
92593690 1025 'thumbnail': r're:https?://.+\.jpe?g',
ba723997 1026 },
943d5ab1 1027 }]
ba723997 1028 _APP_VERSIONS = [('23.3.0', '230300')]
943d5ab1
M
1029 _APP_NAME = 'aweme'
1030 _AID = 1128
1031 _API_HOSTNAME = 'aweme.snssdk.com'
1032 _UPLOADER_URL_FORMAT = 'https://www.douyin.com/user/%s'
53dad39e 1033 _WEBPAGE_HOST = 'https://www.douyin.com/'
943d5ab1
M
1034
1035 def _real_extract(self, url):
1036 video_id = self._match_id(url)
1037
1038 try:
1039 return self._extract_aweme_app(video_id)
1040 except ExtractorError as e:
ba723997 1041 e.expected = True
1042 self.to_screen(f'{e}; trying with webpage')
943d5ab1
M
1043
1044 webpage = self._download_webpage(url, video_id)
a2be9781 1045 render_data = self._search_json(
1046 r'<script [^>]*\bid=[\'"]RENDER_DATA[\'"][^>]*>', webpage, 'render data', video_id,
1047 contains_pattern=r'%7B(?s:.+)%7D', fatal=False, transform_source=compat_urllib_parse_unquote)
1048 if not render_data:
943d5ab1 1049 # TODO: Run verification challenge code to generate signature cookies
ba723997 1050 cookies = self._get_cookies(self._WEBPAGE_HOST)
1051 expected = not cookies.get('s_v_web_id') or not cookies.get('ttwid')
1052 raise ExtractorError(
1053 'Fresh cookies (not necessarily logged in) are needed', expected=expected)
943d5ab1 1054
92593690 1055 return self._parse_aweme_video_web(get_first(render_data, ('aweme', 'detail')), url, video_id)
88afe056 1056
1057
49895f06 1058class TikTokVMIE(InfoExtractor):
ba723997 1059 _VALID_URL = r'https?://(?:(?:vm|vt)\.tiktok\.com|(?:www\.)tiktok\.com/t)/(?P<id>\w+)'
88afe056 1060 IE_NAME = 'vm.tiktok'
1061
49895f06 1062 _TESTS = [{
ba723997 1063 'url': 'https://www.tiktok.com/t/ZTRC5xgJp',
49895f06 1064 'info_dict': {
ba723997 1065 'id': '7170520270497680683',
49895f06 1066 'ext': 'mp4',
ba723997 1067 'title': 'md5:c64f6152330c2efe98093ccc8597871c',
1068 'uploader_id': '6687535061741700102',
1069 'upload_date': '20221127',
49895f06 1070 'view_count': int,
ba723997 1071 'like_count': int,
49895f06 1072 'comment_count': int,
ba723997 1073 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAObqu3WCTXxmw2xwZ3iLEHnEecEIw7ks6rxWqOqOhaPja9BI7gqUQnjw8_5FSoDXX',
1074 'album': 'Wave of Mutilation: Best of Pixies',
1075 'thumbnail': r're:https://.+\.webp.*',
1076 'duration': 5,
1077 'timestamp': 1669516858,
49895f06 1078 'repost_count': int,
ba723997 1079 'artist': 'Pixies',
1080 'track': 'Where Is My Mind?',
1081 'description': 'md5:c64f6152330c2efe98093ccc8597871c',
1082 'uploader': 'sigmachaddeus',
1083 'creator': 'SigmaChad',
1084 },
1085 }, {
c4cbd3be 1086 'url': 'https://vm.tiktok.com/ZTR45GpSF/',
1087 'info_dict': {
1088 'id': '7106798200794926362',
1089 'ext': 'mp4',
1090 'title': 'md5:edc3e7ea587847f8537468f2fe51d074',
1091 'uploader_id': '6997695878846268418',
1092 'upload_date': '20220608',
1093 'view_count': int,
1094 'like_count': int,
1095 'comment_count': int,
1096 'thumbnail': r're:https://.+\.webp.*',
1097 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAdZ_NcPPgMneaGrW0hN8O_J_bwLshwNNERRF5DxOw2HKIzk0kdlLrR8RkVl1ksrMO',
1098 'duration': 29,
1099 'timestamp': 1654680400,
1100 'repost_count': int,
1101 'artist': 'Akihitoko',
1102 'track': 'original sound',
1103 'description': 'md5:edc3e7ea587847f8537468f2fe51d074',
1104 'uploader': 'akihitoko1',
1105 'creator': 'Akihitoko',
1106 },
49895f06 1107 }, {
1108 'url': 'https://vt.tiktok.com/ZSe4FqkKd',
1109 'only_matching': True,
1110 }]
1111
88afe056 1112 def _real_extract(self, url):
11e1c2e3 1113 new_url = self._request_webpage(
3d2623a8 1114 HEADRequest(url), self._match_id(url), headers={'User-Agent': 'facebookexternalhit/1.1'}).url
11e1c2e3 1115 if self.suitable(new_url): # Prevent infinite loop in case redirect fails
1116 raise UnsupportedError(new_url)
1117 return self.url_result(new_url)
933ed882
JC
1118
1119
216bcb66 1120class TikTokLiveIE(TikTokBaseIE):
1121 _VALID_URL = r'''(?x)https?://(?:
1122 (?:www\.)?tiktok\.com/@(?P<uploader>[\w.-]+)/live|
1123 m\.tiktok\.com/share/live/(?P<id>\d+)
1124 )'''
933ed882
JC
1125 IE_NAME = 'tiktok:live'
1126
1127 _TESTS = [{
216bcb66 1128 'url': 'https://www.tiktok.com/@weathernewslive/live',
1129 'info_dict': {
1130 'id': '7210809319192726273',
1131 'ext': 'mp4',
1132 'title': r're:ウェザーニュースLiVE[\d\s:-]*',
1133 'creator': 'ウェザーニュースLiVE',
1134 'uploader': 'weathernewslive',
1135 'uploader_id': '6621496731283095554',
1136 'uploader_url': 'https://www.tiktok.com/@weathernewslive',
1137 'live_status': 'is_live',
1138 'concurrent_view_count': int,
1139 },
1140 'params': {'skip_download': 'm3u8'},
1141 }, {
1142 'url': 'https://www.tiktok.com/@pilarmagenta/live',
1143 'info_dict': {
1144 'id': '7209423610325322522',
1145 'ext': 'mp4',
1146 'title': str,
1147 'creator': 'Pilarmagenta',
1148 'uploader': 'pilarmagenta',
1149 'uploader_id': '6624846890674683909',
1150 'uploader_url': 'https://www.tiktok.com/@pilarmagenta',
1151 'live_status': 'is_live',
1152 'concurrent_view_count': int,
1153 },
1154 'skip': 'Livestream',
1155 }, {
1156 'url': 'https://m.tiktok.com/share/live/7209423610325322522/?language=en',
1157 'only_matching': True,
1158 }, {
933ed882
JC
1159 'url': 'https://www.tiktok.com/@iris04201/live',
1160 'only_matching': True,
1161 }]
1162
216bcb66 1163 def _call_api(self, url, param, room_id, uploader, key=None):
1164 response = traverse_obj(self._download_json(
1165 url, room_id, fatal=False, query={
1166 'aid': '1988',
1167 param: room_id,
1168 }), (key, {dict}), default={})
1169
1170 # status == 2 if live else 4
1171 if int_or_none(response.get('status')) == 2:
1172 return response
1173 # If room_id is obtained via mobile share URL and cannot be refreshed, do not wait for live
1174 elif not uploader:
1175 raise ExtractorError('This livestream has ended', expected=True)
1176 raise UserNotLive(video_id=uploader)
1177
933ed882 1178 def _real_extract(self, url):
216bcb66 1179 uploader, room_id = self._match_valid_url(url).group('uploader', 'id')
1180 webpage = self._download_webpage(
1181 url, uploader or room_id, headers={'User-Agent': 'Mozilla/5.0'}, fatal=not room_id)
1182
1183 if webpage:
1184 data = try_call(lambda: self._get_sigi_state(webpage, uploader or room_id))
1185 room_id = (traverse_obj(data, ('UserModule', 'users', ..., 'roomId', {str_or_none}), get_all=False)
1186 or self._search_regex(r'snssdk\d*://live\?room_id=(\d+)', webpage, 'room ID', default=None)
1187 or room_id)
1188 uploader = uploader or traverse_obj(
1189 data, ('LiveRoom', 'liveRoomUserInfo', 'user', 'uniqueId'),
1190 ('UserModule', 'users', ..., 'uniqueId'), get_all=False, expected_type=str)
1191
933ed882
JC
1192 if not room_id:
1193 raise UserNotLive(video_id=uploader)
933ed882 1194
216bcb66 1195 formats = []
1196 live_info = self._call_api(
1197 'https://webcast.tiktok.com/webcast/room/info', 'room_id', room_id, uploader, key='data')
1198
1199 get_quality = qualities(('SD1', 'ld', 'SD2', 'sd', 'HD1', 'hd', 'FULL_HD1', 'uhd', 'ORIGION', 'origin'))
1200 parse_inner = lambda x: self._parse_json(x, None)
1201
1202 for quality, stream in traverse_obj(live_info, (
1203 'stream_url', 'live_core_sdk_data', 'pull_data', 'stream_data',
1204 {parse_inner}, 'data', {dict}), default={}).items():
1205
1206 sdk_params = traverse_obj(stream, ('main', 'sdk_params', {parse_inner}, {
1207 'vcodec': ('VCodec', {str}),
1208 'tbr': ('vbitrate', {lambda x: int_or_none(x, 1000)}),
1209 'resolution': ('resolution', {lambda x: re.match(r'(?i)\d+x\d+|\d+p', x).group().lower()}),
1210 }))
1211
1212 flv_url = traverse_obj(stream, ('main', 'flv', {url_or_none}))
1213 if flv_url:
1214 formats.append({
1215 'url': flv_url,
1216 'ext': 'flv',
1217 'format_id': f'flv-{quality}',
1218 'quality': get_quality(quality),
1219 **sdk_params,
1220 })
1221
1222 hls_url = traverse_obj(stream, ('main', 'hls', {url_or_none}))
1223 if hls_url:
1224 formats.append({
1225 'url': hls_url,
1226 'ext': 'mp4',
1227 'protocol': 'm3u8_native',
1228 'format_id': f'hls-{quality}',
1229 'quality': get_quality(quality),
1230 **sdk_params,
1231 })
1232
1233 def get_vcodec(*keys):
1234 return traverse_obj(live_info, (
1235 'stream_url', *keys, {parse_inner}, 'VCodec', {str}))
1236
1237 for stream in ('hls', 'rtmp'):
1238 stream_url = traverse_obj(live_info, ('stream_url', f'{stream}_pull_url', {url_or_none}))
1239 if stream_url:
1240 formats.append({
1241 'url': stream_url,
1242 'ext': 'mp4' if stream == 'hls' else 'flv',
1243 'protocol': 'm3u8_native' if stream == 'hls' else 'https',
1244 'format_id': f'{stream}-pull',
1245 'vcodec': get_vcodec(f'{stream}_pull_url_params'),
1246 'quality': get_quality('ORIGION'),
1247 })
1248
1249 for f_id, f_url in traverse_obj(live_info, ('stream_url', 'flv_pull_url', {dict}), default={}).items():
1250 if not url_or_none(f_url):
1251 continue
1252 formats.append({
1253 'url': f_url,
1254 'ext': 'flv',
1255 'format_id': f'flv-{f_id}'.lower(),
1256 'vcodec': get_vcodec('flv_pull_url_params', f_id),
1257 'quality': get_quality(f_id),
1258 })
1259
1260 # If uploader is a guest on another's livestream, primary endpoint will not have m3u8 URLs
1261 if not traverse_obj(formats, lambda _, v: v['ext'] == 'mp4'):
1262 live_info = merge_dicts(live_info, self._call_api(
1263 'https://www.tiktok.com/api/live/detail/', 'roomID', room_id, uploader, key='LiveRoomInfo'))
1264 if url_or_none(live_info.get('liveUrl')):
1265 formats.append({
1266 'url': live_info['liveUrl'],
1267 'ext': 'mp4',
1268 'protocol': 'm3u8_native',
1269 'format_id': 'hls-fallback',
1270 'vcodec': 'h264',
1271 'quality': get_quality('origin'),
1272 })
1273
1274 uploader = uploader or traverse_obj(live_info, ('ownerInfo', 'uniqueId'), ('owner', 'display_id'))
933ed882
JC
1275
1276 return {
1277 'id': room_id,
933ed882 1278 'uploader': uploader,
216bcb66 1279 'uploader_url': format_field(uploader, None, self._UPLOADER_URL_FORMAT) or None,
933ed882 1280 'is_live': True,
216bcb66 1281 'formats': formats,
1282 '_format_sort_fields': ('quality', 'ext'),
1283 **traverse_obj(live_info, {
1284 'title': 'title',
1285 'uploader_id': (('ownerInfo', 'owner'), 'id', {str_or_none}),
1286 'creator': (('ownerInfo', 'owner'), 'nickname'),
1287 'concurrent_view_count': (('user_count', ('liveRoomStats', 'userCount')), {int_or_none}),
1288 }, get_all=False),
933ed882 1289 }