[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / tiktok.py
blob9e53b340722071193d71b3767f536e0b7d0a5e63
1 import functools
2 import itertools
3 import json
4 import random
5 import re
6 import string
7 import time
8 import urllib.parse
9 import uuid
11 from .common import InfoExtractor
12 from ..networking import HEADRequest
13 from ..utils import (
14 ExtractorError,
15 UnsupportedError,
16 UserNotLive,
17 determine_ext,
18 filter_dict,
19 format_field,
20 int_or_none,
21 join_nonempty,
22 merge_dicts,
23 mimetype2ext,
24 parse_qs,
25 qualities,
26 srt_subtitles_timecode,
27 str_or_none,
28 traverse_obj,
29 try_call,
30 try_get,
31 url_or_none,
32 urlencode_postdata,
36 class TikTokBaseIE(InfoExtractor):
37 _UPLOADER_URL_FORMAT = 'https://www.tiktok.com/@%s'
38 _WEBPAGE_HOST = 'https://www.tiktok.com/'
39 QUALITIES = ('360p', '540p', '720p', '1080p')
41 _APP_INFO_DEFAULTS = {
42 # unique "install id"
43 'iid': None,
44 # TikTok (KR/PH/TW/TH/VN) = trill, TikTok (rest of world) = musical_ly, Douyin = aweme
45 'app_name': 'musical_ly',
46 'app_version': '35.1.3',
47 'manifest_app_version': '2023501030',
48 # "app id": aweme = 1128, trill = 1180, musical_ly = 1233, universal = 0
49 'aid': '0',
51 _APP_INFO_POOL = None
52 _APP_INFO = None
53 _APP_USER_AGENT = None
55 @functools.cached_property
56 def _KNOWN_APP_INFO(self):
57 # If we have a genuine device ID, we may not need any IID
58 default = [''] if self._KNOWN_DEVICE_ID else []
59 return self._configuration_arg('app_info', default, ie_key=TikTokIE)
61 @functools.cached_property
62 def _KNOWN_DEVICE_ID(self):
63 return self._configuration_arg('device_id', [None], ie_key=TikTokIE)[0]
65 @functools.cached_property
66 def _DEVICE_ID(self):
67 return self._KNOWN_DEVICE_ID or str(random.randint(7250000000000000000, 7351147085025500000))
69 @functools.cached_property
70 def _API_HOSTNAME(self):
71 return self._configuration_arg(
72 'api_hostname', ['api16-normal-c-useast1a.tiktokv.com'], ie_key=TikTokIE)[0]
74 def _get_next_app_info(self):
75 if self._APP_INFO_POOL is None:
76 defaults = {
77 key: self._configuration_arg(key, [default], ie_key=TikTokIE)[0]
78 for key, default in self._APP_INFO_DEFAULTS.items()
79 if key != 'iid'
81 self._APP_INFO_POOL = [
82 {**defaults, **dict(
83 (k, v) for k, v in zip(self._APP_INFO_DEFAULTS, app_info.split('/')) if v
84 )} for app_info in self._KNOWN_APP_INFO
87 if not self._APP_INFO_POOL:
88 return False
90 self._APP_INFO = self._APP_INFO_POOL.pop(0)
92 app_name = self._APP_INFO['app_name']
93 version = self._APP_INFO['manifest_app_version']
94 if app_name == 'musical_ly':
95 package = f'com.zhiliaoapp.musically/{version}'
96 else: # trill, aweme
97 package = f'com.ss.android.ugc.{app_name}/{version}'
98 self._APP_USER_AGENT = f'{package} (Linux; U; Android 13; en_US; Pixel 7; Build/TD1A.220804.031; Cronet/58.0.2991.0)'
100 return True
102 @staticmethod
103 def _create_url(user_id, video_id):
104 return f'https://www.tiktok.com/@{user_id or "_"}/video/{video_id}'
106 def _get_sigi_state(self, webpage, display_id):
107 return self._search_json(
108 r'<script[^>]+\bid="(?:SIGI_STATE|sigi-persisted-data)"[^>]*>', webpage,
109 'sigi state', display_id, end_pattern=r'</script>', default={})
111 def _get_universal_data(self, webpage, display_id):
112 return traverse_obj(self._search_json(
113 r'<script[^>]+\bid="__UNIVERSAL_DATA_FOR_REHYDRATION__"[^>]*>', webpage,
114 'universal data', display_id, end_pattern=r'</script>', default={}),
115 ('__DEFAULT_SCOPE__', {dict})) or {}
117 def _call_api_impl(self, ep, video_id, query=None, data=None, headers=None, fatal=True,
118 note='Downloading API JSON', errnote='Unable to download API page'):
119 self._set_cookie(self._API_HOSTNAME, 'odin_tt', ''.join(random.choices('0123456789abcdef', k=160)))
120 webpage_cookies = self._get_cookies(self._WEBPAGE_HOST)
121 if webpage_cookies.get('sid_tt'):
122 self._set_cookie(self._API_HOSTNAME, 'sid_tt', webpage_cookies['sid_tt'].value)
123 return self._download_json(
124 f'https://{self._API_HOSTNAME}/aweme/v1/{ep}/', video_id=video_id,
125 fatal=fatal, note=note, errnote=errnote, headers={
126 'User-Agent': self._APP_USER_AGENT,
127 'Accept': 'application/json',
128 **(headers or {}),
129 }, query=query, data=data)
131 def _build_api_query(self, query):
132 return filter_dict({
133 **query,
134 'device_platform': 'android',
135 'os': 'android',
136 'ssmix': 'a',
137 '_rticket': int(time.time() * 1000),
138 'cdid': str(uuid.uuid4()),
139 'channel': 'googleplay',
140 'aid': self._APP_INFO['aid'],
141 'app_name': self._APP_INFO['app_name'],
142 'version_code': ''.join(f'{int(v):02d}' for v in self._APP_INFO['app_version'].split('.')),
143 'version_name': self._APP_INFO['app_version'],
144 'manifest_version_code': self._APP_INFO['manifest_app_version'],
145 'update_version_code': self._APP_INFO['manifest_app_version'],
146 'ab_version': self._APP_INFO['app_version'],
147 'resolution': '1080*2400',
148 'dpi': 420,
149 'device_type': 'Pixel 7',
150 'device_brand': 'Google',
151 'language': 'en',
152 'os_api': '29',
153 'os_version': '13',
154 'ac': 'wifi',
155 'is_pad': '0',
156 'current_region': 'US',
157 'app_type': 'normal',
158 'sys_region': 'US',
159 'last_install_time': int(time.time()) - random.randint(86400, 1123200),
160 'timezone_name': 'America/New_York',
161 'residence': 'US',
162 'app_language': 'en',
163 'timezone_offset': '-14400',
164 'host_abi': 'armeabi-v7a',
165 'locale': 'en',
166 'ac2': 'wifi5g',
167 'uoo': '1',
168 'carrier_region': 'US',
169 'op_region': 'US',
170 'build_number': self._APP_INFO['app_version'],
171 'region': 'US',
172 'ts': int(time.time()),
173 'iid': self._APP_INFO.get('iid'),
174 'device_id': self._DEVICE_ID,
175 'openudid': ''.join(random.choices('0123456789abcdef', k=16)),
178 def _call_api(self, ep, video_id, query=None, data=None, headers=None, fatal=True,
179 note='Downloading API JSON', errnote='Unable to download API page'):
180 if not self._APP_INFO and not self._get_next_app_info():
181 message = 'No working app info is available'
182 if fatal:
183 raise ExtractorError(message, expected=True)
184 else:
185 self.report_warning(message)
186 return
188 max_tries = len(self._APP_INFO_POOL) + 1 # _APP_INFO_POOL + _APP_INFO
189 for count in itertools.count(1):
190 self.write_debug(str(self._APP_INFO))
191 real_query = self._build_api_query(query or {})
192 try:
193 return self._call_api_impl(
194 ep, video_id, query=real_query, data=data, headers=headers,
195 fatal=fatal, note=note, errnote=errnote)
196 except ExtractorError as e:
197 if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0:
198 message = str(e.cause or e.msg)
199 if not self._get_next_app_info():
200 if fatal:
201 raise
202 else:
203 self.report_warning(message)
204 return
205 self.report_warning(f'{message}. Retrying... (attempt {count} of {max_tries})')
206 continue
207 raise
209 def _extract_aweme_app(self, aweme_id):
210 aweme_detail = traverse_obj(
211 self._call_api('multi/aweme/detail', aweme_id, data=urlencode_postdata({
212 'aweme_ids': f'[{aweme_id}]',
213 'request_source': '0',
214 }), headers={'X-Argus': ''}), ('aweme_details', 0, {dict}))
215 if not aweme_detail:
216 raise ExtractorError('Unable to extract aweme detail info', video_id=aweme_id)
217 return self._parse_aweme_video_app(aweme_detail)
219 def _extract_web_data_and_status(self, url, video_id, fatal=True):
220 video_data, status = {}, -1
222 res = self._download_webpage_handle(url, video_id, fatal=fatal, headers={'User-Agent': 'Mozilla/5.0'})
223 if res is False:
224 return video_data, status
226 webpage, urlh = res
227 if urllib.parse.urlparse(urlh.url).path == '/login':
228 message = 'TikTok is requiring login for access to this content'
229 if fatal:
230 self.raise_login_required(message)
231 self.report_warning(f'{message}. {self._login_hint()}')
232 return video_data, status
234 if universal_data := self._get_universal_data(webpage, video_id):
235 self.write_debug('Found universal data for rehydration')
236 status = traverse_obj(universal_data, ('webapp.video-detail', 'statusCode', {int})) or 0
237 video_data = traverse_obj(universal_data, ('webapp.video-detail', 'itemInfo', 'itemStruct', {dict}))
239 elif sigi_data := self._get_sigi_state(webpage, video_id):
240 self.write_debug('Found sigi state data')
241 status = traverse_obj(sigi_data, ('VideoPage', 'statusCode', {int})) or 0
242 video_data = traverse_obj(sigi_data, ('ItemModule', video_id, {dict}))
244 elif next_data := self._search_nextjs_data(webpage, video_id, default={}):
245 self.write_debug('Found next.js data')
246 status = traverse_obj(next_data, ('props', 'pageProps', 'statusCode', {int})) or 0
247 video_data = traverse_obj(next_data, ('props', 'pageProps', 'itemInfo', 'itemStruct', {dict}))
249 elif fatal:
250 raise ExtractorError('Unable to extract webpage video data')
252 return video_data, status
254 def _get_subtitles(self, aweme_detail, aweme_id, user_name):
255 # TODO: Extract text positioning info
257 EXT_MAP = { # From lowest to highest preference
258 'creator_caption': 'json',
259 'srt': 'srt',
260 'webvtt': 'vtt',
262 preference = qualities(tuple(EXT_MAP.values()))
264 subtitles = {}
266 # aweme/detail endpoint subs
267 captions_info = traverse_obj(
268 aweme_detail, ('interaction_stickers', ..., 'auto_video_caption_info', 'auto_captions', ...), expected_type=dict)
269 for caption in captions_info:
270 caption_url = traverse_obj(caption, ('url', 'url_list', ...), expected_type=url_or_none, get_all=False)
271 if not caption_url:
272 continue
273 caption_json = self._download_json(
274 caption_url, aweme_id, note='Downloading captions', errnote='Unable to download captions', fatal=False)
275 if not caption_json:
276 continue
277 subtitles.setdefault(caption.get('language', 'en'), []).append({
278 'ext': 'srt',
279 'data': '\n\n'.join(
280 f'{i + 1}\n{srt_subtitles_timecode(line["start_time"] / 1000)} --> {srt_subtitles_timecode(line["end_time"] / 1000)}\n{line["text"]}'
281 for i, line in enumerate(caption_json['utterances']) if line.get('text')),
283 # feed endpoint subs
284 if not subtitles:
285 for caption in traverse_obj(aweme_detail, ('video', 'cla_info', 'caption_infos', ...), expected_type=dict):
286 if not caption.get('url'):
287 continue
288 subtitles.setdefault(caption.get('lang') or 'en', []).append({
289 'url': caption['url'],
290 'ext': EXT_MAP.get(caption.get('Format')),
292 # webpage subs
293 if not subtitles:
294 if user_name: # only _parse_aweme_video_app needs to extract the webpage here
295 aweme_detail, _ = self._extract_web_data_and_status(
296 self._create_url(user_name, aweme_id), aweme_id, fatal=False)
297 for caption in traverse_obj(aweme_detail, ('video', 'subtitleInfos', lambda _, v: v['Url'])):
298 subtitles.setdefault(caption.get('LanguageCodeName') or 'en', []).append({
299 'url': caption['Url'],
300 'ext': EXT_MAP.get(caption.get('Format')),
303 # Deprioritize creator_caption json since it can't be embedded or used by media players
304 for lang, subs_list in subtitles.items():
305 subtitles[lang] = sorted(subs_list, key=lambda x: preference(x['ext']))
307 return subtitles
309 def _parse_url_key(self, url_key):
310 format_id, codec, res, bitrate = self._search_regex(
311 r'v[^_]+_(?P<id>(?P<codec>[^_]+)_(?P<res>\d+p)_(?P<bitrate>\d+))', url_key,
312 'url key', default=(None, None, None, None), group=('id', 'codec', 'res', 'bitrate'))
313 if not format_id:
314 return {}, None
315 return {
316 'format_id': format_id,
317 'vcodec': 'h265' if codec == 'bytevc1' else codec,
318 'tbr': int_or_none(bitrate, scale=1000) or None,
319 'quality': qualities(self.QUALITIES)(res),
320 }, res
322 def _parse_aweme_video_app(self, aweme_detail):
323 aweme_id = aweme_detail['aweme_id']
324 video_info = aweme_detail['video']
325 known_resolutions = {}
327 def audio_meta(url):
328 ext = determine_ext(url, default_ext='m4a')
329 return {
330 'format_note': 'Music track',
331 'ext': ext,
332 'acodec': 'aac' if ext == 'm4a' else ext,
333 'vcodec': 'none',
334 'width': None,
335 'height': None,
336 } if ext == 'mp3' or '-music-' in url else {}
338 def extract_addr(addr, add_meta={}):
339 parsed_meta, res = self._parse_url_key(addr.get('url_key', ''))
340 is_bytevc2 = parsed_meta.get('vcodec') == 'bytevc2'
341 if res:
342 known_resolutions.setdefault(res, {}).setdefault('height', int_or_none(addr.get('height')))
343 known_resolutions[res].setdefault('width', int_or_none(addr.get('width')))
344 parsed_meta.update(known_resolutions.get(res, {}))
345 add_meta.setdefault('height', int_or_none(res[:-1]))
346 return [{
347 'url': url,
348 'filesize': int_or_none(addr.get('data_size')),
349 'ext': 'mp4',
350 'acodec': 'aac',
351 'source_preference': -2 if 'aweme/v1' in url else -1, # Downloads from API might get blocked
352 **add_meta, **parsed_meta,
353 # bytevc2 is bytedance's own custom h266/vvc codec, as-of-yet unplayable
354 'preference': -100 if is_bytevc2 else -1,
355 'format_note': join_nonempty(
356 add_meta.get('format_note'), '(API)' if 'aweme/v1' in url else None,
357 '(UNPLAYABLE)' if is_bytevc2 else None, delim=' '),
358 **audio_meta(url),
359 } for url in addr.get('url_list') or []]
361 # Hack: Add direct video links first to prioritize them when removing duplicate formats
362 formats = []
363 width = int_or_none(video_info.get('width'))
364 height = int_or_none(video_info.get('height'))
365 ratio = try_call(lambda: width / height) or 0.5625
366 if video_info.get('play_addr'):
367 formats.extend(extract_addr(video_info['play_addr'], {
368 'format_id': 'play_addr',
369 'format_note': 'Direct video',
370 'vcodec': 'h265' if traverse_obj(
371 video_info, 'is_bytevc1', 'is_h265') else 'h264', # TODO: Check for "direct iOS" videos, like https://www.tiktok.com/@cookierun_dev/video/7039716639834656002
372 'width': width,
373 'height': height,
375 if video_info.get('download_addr'):
376 download_addr = video_info['download_addr']
377 dl_width = int_or_none(download_addr.get('width'))
378 formats.extend(extract_addr(download_addr, {
379 'format_id': 'download_addr',
380 'format_note': 'Download video%s' % (', watermarked' if video_info.get('has_watermark') else ''),
381 'vcodec': 'h264',
382 'width': dl_width,
383 'height': try_call(lambda: int(dl_width / ratio)), # download_addr['height'] is wrong
384 'preference': -2 if video_info.get('has_watermark') else -1,
386 if video_info.get('play_addr_h264'):
387 formats.extend(extract_addr(video_info['play_addr_h264'], {
388 'format_id': 'play_addr_h264',
389 'format_note': 'Direct video',
390 'vcodec': 'h264',
392 if video_info.get('play_addr_bytevc1'):
393 formats.extend(extract_addr(video_info['play_addr_bytevc1'], {
394 'format_id': 'play_addr_bytevc1',
395 'format_note': 'Direct video',
396 'vcodec': 'h265',
399 for bitrate in video_info.get('bit_rate', []):
400 if bitrate.get('play_addr'):
401 formats.extend(extract_addr(bitrate['play_addr'], {
402 'format_id': bitrate.get('gear_name'),
403 'format_note': 'Playback video',
404 'tbr': try_get(bitrate, lambda x: x['bit_rate'] / 1000),
405 'vcodec': 'h265' if traverse_obj(
406 bitrate, 'is_bytevc1', 'is_h265') else 'h264',
407 'fps': bitrate.get('FPS'),
410 self._remove_duplicate_formats(formats)
411 auth_cookie = self._get_cookies(self._WEBPAGE_HOST).get('sid_tt')
412 if auth_cookie:
413 for f in formats:
414 self._set_cookie(urllib.parse.urlparse(f['url']).hostname, 'sid_tt', auth_cookie.value)
416 stats_info = aweme_detail.get('statistics') or {}
417 music_info = aweme_detail.get('music') or {}
418 labels = traverse_obj(aweme_detail, ('hybrid_label', ..., 'text'), expected_type=str)
420 contained_music_track = traverse_obj(
421 music_info, ('matched_song', 'title'), ('matched_pgc_sound', 'title'), expected_type=str)
422 contained_music_author = traverse_obj(
423 music_info, ('matched_song', 'author'), ('matched_pgc_sound', 'author'), 'author', expected_type=str)
425 is_generic_og_trackname = music_info.get('is_original_sound') and music_info.get('title') == 'original sound - {}'.format(music_info.get('owner_handle'))
426 if is_generic_og_trackname:
427 music_track, music_author = contained_music_track or 'original sound', contained_music_author
428 else:
429 music_track, music_author = music_info.get('title'), traverse_obj(music_info, ('author', {str}))
431 author_info = traverse_obj(aweme_detail, ('author', {
432 'uploader': ('unique_id', {str}),
433 'uploader_id': ('uid', {str_or_none}),
434 'channel': ('nickname', {str}),
435 'channel_id': ('sec_uid', {str}),
438 return {
439 'id': aweme_id,
440 **traverse_obj(aweme_detail, {
441 'title': ('desc', {str}),
442 'description': ('desc', {str}),
443 'timestamp': ('create_time', {int_or_none}),
445 **traverse_obj(stats_info, {
446 'view_count': 'play_count',
447 'like_count': 'digg_count',
448 'repost_count': 'share_count',
449 'comment_count': 'comment_count',
450 }, expected_type=int_or_none),
451 **author_info,
452 'channel_url': format_field(author_info, 'channel_id', self._UPLOADER_URL_FORMAT, default=None),
453 'uploader_url': format_field(
454 author_info, ['uploader', 'uploader_id'], self._UPLOADER_URL_FORMAT, default=None),
455 'track': music_track,
456 'album': str_or_none(music_info.get('album')) or None,
457 'artists': re.split(r'(?:, | & )', music_author) if music_author else None,
458 'formats': formats,
459 'subtitles': self.extract_subtitles(
460 aweme_detail, aweme_id, traverse_obj(author_info, 'uploader', 'uploader_id', 'channel_id')),
461 'thumbnails': [
463 'id': cover_id,
464 'url': cover_url,
465 'preference': -1 if cover_id in ('cover', 'origin_cover') else -2,
467 for cover_id in (
468 'cover', 'ai_dynamic_cover', 'animated_cover',
469 'ai_dynamic_cover_bak', 'origin_cover', 'dynamic_cover')
470 for cover_url in traverse_obj(video_info, (cover_id, 'url_list', ...))
472 'duration': (traverse_obj(video_info, (
473 (None, 'download_addr'), 'duration', {int_or_none(scale=1000)}, any))
474 or traverse_obj(music_info, ('duration', {int_or_none}))),
475 'availability': self._availability(
476 is_private='Private' in labels,
477 needs_subscription='Friends only' in labels,
478 is_unlisted='Followers only' in labels),
479 '_format_sort_fields': ('quality', 'codec', 'size', 'br'),
482 def _extract_web_formats(self, aweme_detail):
483 COMMON_FORMAT_INFO = {
484 'ext': 'mp4',
485 'vcodec': 'h264',
486 'acodec': 'aac',
488 video_info = traverse_obj(aweme_detail, ('video', {dict})) or {}
489 play_width = int_or_none(video_info.get('width'))
490 play_height = int_or_none(video_info.get('height'))
491 ratio = try_call(lambda: play_width / play_height) or 0.5625
492 formats = []
494 for bitrate_info in traverse_obj(video_info, ('bitrateInfo', lambda _, v: v['PlayAddr']['UrlList'])):
495 format_info, res = self._parse_url_key(
496 traverse_obj(bitrate_info, ('PlayAddr', 'UrlKey', {str})) or '')
497 # bytevc2 is bytedance's own custom h266/vvc codec, as-of-yet unplayable
498 is_bytevc2 = format_info.get('vcodec') == 'bytevc2'
499 format_info.update({
500 'format_note': 'UNPLAYABLE' if is_bytevc2 else None,
501 'preference': -100 if is_bytevc2 else -1,
502 'filesize': traverse_obj(bitrate_info, ('PlayAddr', 'DataSize', {int_or_none})),
505 if dimension := (res and int(res[:-1])):
506 if dimension == 540: # '540p' is actually 576p
507 dimension = 576
508 if ratio < 1: # portrait: res/dimension is width
509 y = int(dimension / ratio)
510 format_info.update({
511 'width': dimension,
512 'height': y - (y % 2),
514 else: # landscape: res/dimension is height
515 x = int(dimension * ratio)
516 format_info.update({
517 'width': x + (x % 2),
518 'height': dimension,
521 for video_url in traverse_obj(bitrate_info, ('PlayAddr', 'UrlList', ..., {url_or_none})):
522 formats.append({
523 **COMMON_FORMAT_INFO,
524 **format_info,
525 'url': self._proto_relative_url(video_url),
528 # We don't have res string for play formats, but need quality for sorting & de-duplication
529 play_quality = traverse_obj(formats, (lambda _, v: v['width'] == play_width, 'quality', any))
531 for play_url in traverse_obj(video_info, ('playAddr', ((..., 'src'), None), {url_or_none})):
532 formats.append({
533 **COMMON_FORMAT_INFO,
534 'format_id': 'play',
535 'url': self._proto_relative_url(play_url),
536 'width': play_width,
537 'height': play_height,
538 'quality': play_quality,
541 for download_url in traverse_obj(video_info, (('downloadAddr', ('download', 'url')), {url_or_none})):
542 formats.append({
543 **COMMON_FORMAT_INFO,
544 'format_id': 'download',
545 'url': self._proto_relative_url(download_url),
546 'format_note': 'watermarked',
547 'preference': -2,
550 self._remove_duplicate_formats(formats)
552 # Is it a slideshow with only audio for download?
553 if not formats and traverse_obj(aweme_detail, ('music', 'playUrl', {url_or_none})):
554 audio_url = aweme_detail['music']['playUrl']
555 ext = traverse_obj(parse_qs(audio_url), (
556 'mime_type', -1, {lambda x: x.replace('_', '/')}, {mimetype2ext})) or 'm4a'
557 formats.append({
558 'format_id': 'audio',
559 'url': self._proto_relative_url(audio_url),
560 'ext': ext,
561 'acodec': 'aac' if ext == 'm4a' else ext,
562 'vcodec': 'none',
565 # Filter out broken formats, see https://github.com/yt-dlp/yt-dlp/issues/11034
566 return [f for f in formats if urllib.parse.urlparse(f['url']).hostname != 'www.tiktok.com']
568 def _parse_aweme_video_web(self, aweme_detail, webpage_url, video_id, extract_flat=False):
569 author_info = traverse_obj(aweme_detail, (('authorInfo', 'author', None), {
570 'channel': ('nickname', {str}),
571 'channel_id': (('authorSecId', 'secUid'), {str}),
572 'uploader': (('uniqueId', 'author'), {str}),
573 'uploader_id': (('authorId', 'uid', 'id'), {str_or_none}),
574 }), get_all=False)
576 return {
577 'id': video_id,
578 'formats': None if extract_flat else self._extract_web_formats(aweme_detail),
579 'subtitles': None if extract_flat else self.extract_subtitles(aweme_detail, video_id, None),
580 'http_headers': {'Referer': webpage_url},
581 **author_info,
582 'channel_url': format_field(author_info, 'channel_id', self._UPLOADER_URL_FORMAT, default=None),
583 'uploader_url': format_field(
584 author_info, ['uploader', 'uploader_id'], self._UPLOADER_URL_FORMAT, default=None),
585 **traverse_obj(aweme_detail, ('music', {
586 'track': ('title', {str}),
587 'album': ('album', {str}, filter),
588 'artists': ('authorName', {str}, {lambda x: re.split(r'(?:, | & )', x) if x else None}),
589 'duration': ('duration', {int_or_none}),
590 })),
591 **traverse_obj(aweme_detail, {
592 'title': ('desc', {str}),
593 'description': ('desc', {str}),
594 # audio-only slideshows have a video duration of 0 and an actual audio duration
595 'duration': ('video', 'duration', {int_or_none}, filter),
596 'timestamp': ('createTime', {int_or_none}),
598 **traverse_obj(aweme_detail, ('stats', {
599 'view_count': 'playCount',
600 'like_count': 'diggCount',
601 'repost_count': 'shareCount',
602 'comment_count': 'commentCount',
603 }), expected_type=int_or_none),
604 'thumbnails': [
606 'id': cover_id,
607 'url': self._proto_relative_url(cover_url),
608 'preference': -2 if cover_id == 'dynamicCover' else -1,
610 for cover_id in ('thumbnail', 'cover', 'dynamicCover', 'originCover')
611 for cover_url in traverse_obj(aweme_detail, ((None, 'video'), cover_id, {url_or_none}))
616 class TikTokIE(TikTokBaseIE):
617 _VALID_URL = r'https?://www\.tiktok\.com/(?:embed|@(?P<user_id>[\w\.-]+)?/video)/(?P<id>\d+)'
618 _EMBED_REGEX = [rf'<(?:script|iframe)[^>]+\bsrc=(["\'])(?P<url>{_VALID_URL})']
620 _TESTS = [{
621 'url': 'https://www.tiktok.com/@leenabhushan/video/6748451240264420610',
622 'md5': '736bb7a466c6f0a6afeb597da1e6f5b7',
623 'info_dict': {
624 'id': '6748451240264420610',
625 'ext': 'mp4',
626 'title': '#jassmanak #lehanga #leenabhushan',
627 'description': '#jassmanak #lehanga #leenabhushan',
628 'duration': 13,
629 'height': 1024,
630 'width': 576,
631 'uploader': 'leenabhushan',
632 'uploader_id': '6691488002098119685',
633 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA_Eb4t1vodM1IuTy_cvp9CY22RAb59xqrO0Xtz9CYQJvgXaDvZxYnZYRzDWhhgJmy',
634 'creator': 'facestoriesbyleenabh',
635 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
636 'upload_date': '20191016',
637 'timestamp': 1571246252,
638 'view_count': int,
639 'like_count': int,
640 'repost_count': int,
641 'comment_count': int,
642 'artist': 'Ysrbeats',
643 'album': 'Lehanga',
644 'track': 'Lehanga',
646 'skip': '404 Not Found',
647 }, {
648 'url': 'https://www.tiktok.com/@patroxofficial/video/6742501081818877190?langCountry=en',
649 'md5': 'f21112672ee4ce05ca390fb6522e1b6f',
650 'info_dict': {
651 'id': '6742501081818877190',
652 'ext': 'mp4',
653 'title': 'md5:5e2a23877420bb85ce6521dbee39ba94',
654 'description': 'md5:5e2a23877420bb85ce6521dbee39ba94',
655 'duration': 27,
656 'height': 1024,
657 'width': 576,
658 'uploader': 'patrox',
659 'uploader_id': '18702747',
660 'uploader_url': 'https://www.tiktok.com/@patrox',
661 'channel_url': 'https://www.tiktok.com/@MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws',
662 'channel_id': 'MS4wLjABAAAAiFnldaILebi5heDoVU6bn4jBWWycX6-9U3xuNPqZ8Ws',
663 'channel': 'patroX',
664 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
665 'upload_date': '20190930',
666 'timestamp': 1569860870,
667 'view_count': int,
668 'like_count': int,
669 'repost_count': int,
670 'comment_count': int,
671 'artists': ['Evan Todd', 'Jessica Keenan Wynn', 'Alice Lee', 'Barrett Wilbert Weed', 'Jon Eidson'],
672 'track': 'Big Fun',
674 }, {
675 # Banned audio, was available on the app, now works with web too
676 'url': 'https://www.tiktok.com/@barudakhb_/video/6984138651336838402',
677 'info_dict': {
678 'id': '6984138651336838402',
679 'ext': 'mp4',
680 'title': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥',
681 'description': 'Balas @yolaaftwsr hayu yu ? #SquadRandom_ 🔥',
682 'uploader': 'barudakhb_',
683 'channel': 'md5:29f238c49bc0c176cb3cef1a9cea9fa6',
684 'uploader_id': '6974687867511718913',
685 'uploader_url': 'https://www.tiktok.com/@barudakhb_',
686 'channel_url': 'https://www.tiktok.com/@MS4wLjABAAAAbhBwQC-R1iKoix6jDFsF-vBdfx2ABoDjaZrM9fX6arU3w71q3cOWgWuTXn1soZ7d',
687 'channel_id': 'MS4wLjABAAAAbhBwQC-R1iKoix6jDFsF-vBdfx2ABoDjaZrM9fX6arU3w71q3cOWgWuTXn1soZ7d',
688 'track': 'Boka Dance',
689 'artists': ['md5:29f238c49bc0c176cb3cef1a9cea9fa6'],
690 'timestamp': 1626121503,
691 'duration': 18,
692 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
693 'upload_date': '20210712',
694 'view_count': int,
695 'like_count': int,
696 'repost_count': int,
697 'comment_count': int,
699 }, {
700 # Sponsored video, only available with feed workaround
701 'url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_/video/7042692929109986561',
702 'info_dict': {
703 'id': '7042692929109986561',
704 'ext': 'mp4',
705 'title': 'Slap and Run!',
706 'description': 'Slap and Run!',
707 'uploader': 'user440922249',
708 'channel': 'Slap And Run',
709 'uploader_id': '7036055384943690754',
710 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_',
711 'channel_id': 'MS4wLjABAAAATh8Vewkn0LYM7Fo03iec3qKdeCUOcBIouRk1mkiag6h3o_pQu_dUXvZ2EZlGST7_',
712 'track': 'Promoted Music',
713 'timestamp': 1639754738,
714 'duration': 30,
715 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
716 'upload_date': '20211217',
717 'view_count': int,
718 'like_count': int,
719 'repost_count': int,
720 'comment_count': int,
722 'skip': 'This video is unavailable',
723 }, {
724 # Video without title and description
725 'url': 'https://www.tiktok.com/@pokemonlife22/video/7059698374567611694',
726 'info_dict': {
727 'id': '7059698374567611694',
728 'ext': 'mp4',
729 'title': 'TikTok video #7059698374567611694',
730 'description': '',
731 'uploader': 'pokemonlife22',
732 'channel': 'Pokemon',
733 'uploader_id': '6820838815978423302',
734 'uploader_url': 'https://www.tiktok.com/@pokemonlife22',
735 'channel_url': 'https://www.tiktok.com/@MS4wLjABAAAA0tF1nBwQVVMyrGu3CqttkNgM68Do1OXUFuCY0CRQk8fEtSVDj89HqoqvbSTmUP2W',
736 'channel_id': 'MS4wLjABAAAA0tF1nBwQVVMyrGu3CqttkNgM68Do1OXUFuCY0CRQk8fEtSVDj89HqoqvbSTmUP2W',
737 'track': 'original sound',
738 'timestamp': 1643714123,
739 'duration': 6,
740 'thumbnail': r're:^https?://[\w\/\.\-]+(~[\w\-]+\.image)?',
741 'upload_date': '20220201',
742 'artists': ['Pokemon'],
743 'view_count': int,
744 'like_count': int,
745 'repost_count': int,
746 'comment_count': int,
748 }, {
749 # hydration JSON is sent in a <script> element
750 'url': 'https://www.tiktok.com/@denidil6/video/7065799023130643713',
751 'info_dict': {
752 'id': '7065799023130643713',
753 'ext': 'mp4',
754 'title': '#denidil#денидил',
755 'description': '#denidil#денидил',
756 'uploader': 'denidil6',
757 'uploader_id': '7046664115636405250',
758 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAsvMSzFdQ4ikl3uR2TEJwMBbB2yZh2Zxwhx-WCo3rbDpAharE3GQCrFuJArI3C8QJ',
759 'artist': 'Holocron Music',
760 'album': 'Wolf Sounds (1 Hour) Enjoy the Company of the Animal That Is the Majestic King of the Night',
761 'track': 'Wolf Sounds (1 Hour) Enjoy the Company of the Animal That Is the Majestic King of the Night',
762 'timestamp': 1645134536,
763 'duration': 26,
764 'upload_date': '20220217',
765 'view_count': int,
766 'like_count': int,
767 'repost_count': int,
768 'comment_count': int,
770 'skip': 'This video is unavailable',
771 }, {
772 # slideshow audio-only mp3 format
773 'url': 'https://www.tiktok.com/@_le_cannibale_/video/7139980461132074283',
774 'info_dict': {
775 'id': '7139980461132074283',
776 'ext': 'mp3',
777 'title': 'TikTok video #7139980461132074283',
778 'description': '',
779 'channel': 'Antaura',
780 'uploader': '_le_cannibale_',
781 'uploader_id': '6604511138619654149',
782 'uploader_url': 'https://www.tiktok.com/@_le_cannibale_',
783 'channel_url': 'https://www.tiktok.com/@MS4wLjABAAAAoShJqaw_5gvy48y3azFeFcT4jeyKWbB0VVYasOCt2tTLwjNFIaDcHAM4D-QGXFOP',
784 'channel_id': 'MS4wLjABAAAAoShJqaw_5gvy48y3azFeFcT4jeyKWbB0VVYasOCt2tTLwjNFIaDcHAM4D-QGXFOP',
785 'artists': ['nathan !'],
786 'track': 'grahamscott canon',
787 'duration': 10,
788 'upload_date': '20220905',
789 'timestamp': 1662406249,
790 'view_count': int,
791 'like_count': int,
792 'repost_count': int,
793 'comment_count': int,
794 'thumbnail': r're:^https://.+\.(?:webp|jpe?g)',
796 }, {
797 # only available via web
798 'url': 'https://www.tiktok.com/@moxypatch/video/7206382937372134662',
799 'md5': '4cdefa501ac8ac20bf04986e10916fea',
800 'info_dict': {
801 'id': '7206382937372134662',
802 'ext': 'mp4',
803 'title': 'md5:1d95c0b96560ca0e8a231af4172b2c0a',
804 'description': 'md5:1d95c0b96560ca0e8a231af4172b2c0a',
805 'channel': 'MoxyPatch',
806 'uploader': 'moxypatch',
807 'uploader_id': '7039142049363379205',
808 'uploader_url': 'https://www.tiktok.com/@moxypatch',
809 'channel_url': 'https://www.tiktok.com/@MS4wLjABAAAAFhqKnngMHJSsifL0w1vFOP5kn3Ndo1ODp0XuIBkNMBCkALTvwILdpu12g3pTtL4V',
810 'channel_id': 'MS4wLjABAAAAFhqKnngMHJSsifL0w1vFOP5kn3Ndo1ODp0XuIBkNMBCkALTvwILdpu12g3pTtL4V',
811 'artists': ['your worst nightmare'],
812 'track': 'original sound',
813 'upload_date': '20230303',
814 'timestamp': 1677866781,
815 'duration': 10,
816 'view_count': int,
817 'like_count': int,
818 'repost_count': int,
819 'comment_count': int,
820 'thumbnail': r're:^https://.+',
821 'thumbnails': 'count:3',
823 'expected_warnings': ['Unable to find video in feed'],
824 }, {
825 # 1080p format
826 'url': 'https://www.tiktok.com/@tatemcrae/video/7107337212743830830', # FIXME: Web can only get audio
827 'md5': '982512017a8a917124d5a08c8ae79621',
828 'info_dict': {
829 'id': '7107337212743830830',
830 'ext': 'mp4',
831 'title': 'new music video 4 don’t come backkkk🧸🖤 i hope u enjoy !! @musicontiktok',
832 'description': 'new music video 4 don’t come backkkk🧸🖤 i hope u enjoy !! @musicontiktok',
833 'uploader': 'tatemcrae',
834 'uploader_id': '86328792343818240',
835 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAA-0bQT0CqebTRr6I4IkYvMDMKSRSJHLNPBo5HrSklJwyA2psXLSZG5FP-LMNpHnJd',
836 'channel_id': 'MS4wLjABAAAA-0bQT0CqebTRr6I4IkYvMDMKSRSJHLNPBo5HrSklJwyA2psXLSZG5FP-LMNpHnJd',
837 'channel': 'tate mcrae',
838 'artists': ['tate mcrae'],
839 'track': 'original sound',
840 'upload_date': '20220609',
841 'timestamp': 1654805899,
842 'duration': 150,
843 'view_count': int,
844 'like_count': int,
845 'repost_count': int,
846 'comment_count': int,
847 'thumbnail': r're:^https://.+\.webp',
849 'skip': 'Unavailable via feed API, only audio available via web',
850 }, {
851 # Slideshow, audio-only m4a format
852 'url': 'https://www.tiktok.com/@hara_yoimiya/video/7253412088251534594',
853 'md5': '2ff8fe0174db2dbf49c597a7bef4e47d',
854 'info_dict': {
855 'id': '7253412088251534594',
856 'ext': 'm4a',
857 'title': 'я ред флаг простите #переписка #щитпост #тревожныйтиппривязанности #рекомендации ',
858 'description': 'я ред флаг простите #переписка #щитпост #тревожныйтиппривязанности #рекомендации ',
859 'uploader': 'hara_yoimiya',
860 'uploader_id': '6582536342634676230',
861 'uploader_url': 'https://www.tiktok.com/@hara_yoimiya',
862 'channel_url': 'https://www.tiktok.com/@MS4wLjABAAAAIAlDxriiPWLE-p8p1R_0Bx8qWKfi-7zwmGhzU8Mv25W8sNxjfIKrol31qTczzuLB',
863 'channel_id': 'MS4wLjABAAAAIAlDxriiPWLE-p8p1R_0Bx8qWKfi-7zwmGhzU8Mv25W8sNxjfIKrol31qTczzuLB',
864 'channel': 'лампочка(!)',
865 'artists': ['Øneheart'],
866 'album': 'watching the stars',
867 'track': 'watching the stars',
868 'duration': 60,
869 'upload_date': '20230708',
870 'timestamp': 1688816612,
871 'view_count': int,
872 'like_count': int,
873 'comment_count': int,
874 'repost_count': int,
875 'thumbnail': r're:^https://.+\.(?:webp|jpe?g)',
877 }, {
878 # Auto-captions available
879 'url': 'https://www.tiktok.com/@hankgreen1/video/7047596209028074758',
880 'only_matching': True,
883 def _real_extract(self, url):
884 video_id, user_id = self._match_valid_url(url).group('id', 'user_id')
886 if self._KNOWN_APP_INFO:
887 try:
888 return self._extract_aweme_app(video_id)
889 except ExtractorError as e:
890 e.expected = True
891 self.report_warning(f'{e}; trying with webpage')
893 url = self._create_url(user_id, video_id)
894 video_data, status = self._extract_web_data_and_status(url, video_id)
896 if video_data and status == 0:
897 return self._parse_aweme_video_web(video_data, url, video_id)
898 elif status == 10216:
899 raise ExtractorError('This video is private', expected=True)
900 raise ExtractorError(f'Video not available, status code {status}', video_id=video_id)
903 class TikTokUserIE(TikTokBaseIE):
904 IE_NAME = 'tiktok:user'
905 _VALID_URL = r'(?:tiktokuser:|https?://(?:www\.)?tiktok\.com/@)(?P<id>[\w.-]+)/?(?:$|[#?])'
906 _TESTS = [{
907 'url': 'https://tiktok.com/@corgibobaa?lang=en',
908 'playlist_mincount': 45,
909 'info_dict': {
910 'id': 'MS4wLjABAAAAepiJKgwWhulvCpSuUVsp7sgVVsFJbbNaLeQ6OQ0oAJERGDUIXhb2yxxHZedsItgT',
911 'title': 'corgibobaa',
913 }, {
914 'url': 'https://www.tiktok.com/@6820838815978423302',
915 'playlist_mincount': 5,
916 'info_dict': {
917 'id': 'MS4wLjABAAAA0tF1nBwQVVMyrGu3CqttkNgM68Do1OXUFuCY0CRQk8fEtSVDj89HqoqvbSTmUP2W',
918 'title': '6820838815978423302',
920 }, {
921 'url': 'https://www.tiktok.com/@meme',
922 'playlist_mincount': 593,
923 'info_dict': {
924 'id': 'MS4wLjABAAAAiKfaDWeCsT3IHwY77zqWGtVRIy9v4ws1HbVi7auP1Vx7dJysU_hc5yRiGywojRD6',
925 'title': 'meme',
927 }, {
928 'url': 'tiktokuser:MS4wLjABAAAAM3R2BtjzVT-uAtstkl2iugMzC6AtnpkojJbjiOdDDrdsTiTR75-8lyWJCY5VvDrZ',
929 'playlist_mincount': 31,
930 'info_dict': {
931 'id': 'MS4wLjABAAAAM3R2BtjzVT-uAtstkl2iugMzC6AtnpkojJbjiOdDDrdsTiTR75-8lyWJCY5VvDrZ',
934 _USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:115.0) Gecko/20100101 Firefox/115.0'
935 _API_BASE_URL = 'https://www.tiktok.com/api/creator/item_list/'
937 def _build_web_query(self, sec_uid, cursor):
938 return {
939 'aid': '1988',
940 'app_language': 'en',
941 'app_name': 'tiktok_web',
942 'browser_language': 'en-US',
943 'browser_name': 'Mozilla',
944 'browser_online': 'true',
945 'browser_platform': 'Win32',
946 'browser_version': '5.0 (Windows)',
947 'channel': 'tiktok_web',
948 'cookie_enabled': 'true',
949 'count': '15',
950 'cursor': cursor,
951 'device_id': self._DEVICE_ID,
952 'device_platform': 'web_pc',
953 'focus_state': 'true',
954 'from_page': 'user',
955 'history_len': '2',
956 'is_fullscreen': 'false',
957 'is_page_visible': 'true',
958 'language': 'en',
959 'os': 'windows',
960 'priority_region': '',
961 'referer': '',
962 'region': 'US',
963 'screen_height': '1080',
964 'screen_width': '1920',
965 'secUid': sec_uid,
966 'type': '1', # pagination type: 0 == oldest-to-newest, 1 == newest-to-oldest
967 'tz_name': 'UTC',
968 'verifyFp': f'verify_{"".join(random.choices(string.hexdigits, k=7))}',
969 'webcast_language': 'en',
972 def _entries(self, sec_uid, user_name):
973 display_id = user_name or sec_uid
974 seen_ids = set()
976 cursor = int(time.time() * 1E3)
977 for page in itertools.count(1):
978 response = self._download_json(
979 self._API_BASE_URL, display_id, f'Downloading page {page}',
980 query=self._build_web_query(sec_uid, cursor), headers={'User-Agent': self._USER_AGENT})
982 for video in traverse_obj(response, ('itemList', lambda _, v: v['id'])):
983 video_id = video['id']
984 if video_id in seen_ids:
985 continue
986 seen_ids.add(video_id)
987 webpage_url = self._create_url(display_id, video_id)
988 yield self.url_result(
989 webpage_url, TikTokIE,
990 **self._parse_aweme_video_web(video, webpage_url, video_id, extract_flat=True))
992 old_cursor = cursor
993 cursor = traverse_obj(
994 response, ('itemList', -1, 'createTime', {lambda x: int(x * 1E3)}))
995 if not cursor or old_cursor == cursor:
996 # User may not have posted within this ~1 week lookback, so manually adjust cursor
997 cursor = old_cursor - 7 * 86_400_000
998 # In case 'hasMorePrevious' is wrong, break if we have gone back before TikTok existed
999 if cursor < 1472706000000 or not traverse_obj(response, 'hasMorePrevious'):
1000 break
1002 def _get_sec_uid(self, user_url, user_name, msg):
1003 webpage = self._download_webpage(
1004 user_url, user_name, fatal=False, headers={'User-Agent': 'Mozilla/5.0'},
1005 note=f'Downloading {msg} webpage', errnote=f'Unable to download {msg} webpage') or ''
1006 return (traverse_obj(self._get_universal_data(webpage, user_name),
1007 ('webapp.user-detail', 'userInfo', 'user', 'secUid', {str}))
1008 or traverse_obj(self._get_sigi_state(webpage, user_name),
1009 ('LiveRoom', 'liveRoomUserInfo', 'user', 'secUid', {str}),
1010 ('UserModule', 'users', ..., 'secUid', {str}, any)))
1012 def _real_extract(self, url):
1013 user_name, sec_uid = self._match_id(url), None
1014 if mobj := re.fullmatch(r'MS4wLjABAAAA[\w-]{64}', user_name):
1015 user_name, sec_uid = None, mobj.group(0)
1016 else:
1017 sec_uid = (self._get_sec_uid(self._UPLOADER_URL_FORMAT % user_name, user_name, 'user')
1018 or self._get_sec_uid(self._UPLOADER_URL_FORMAT % f'{user_name}/live', user_name, 'live'))
1020 if not sec_uid:
1021 webpage = self._download_webpage(
1022 f'https://www.tiktok.com/embed/@{user_name}', user_name,
1023 note='Downloading user embed page', fatal=False) or ''
1024 data = traverse_obj(self._search_json(
1025 r'<script[^>]+\bid=[\'"]__FRONTITY_CONNECT_STATE__[\'"][^>]*>',
1026 webpage, 'data', user_name, default={}),
1027 ('source', 'data', f'/embed/@{user_name}', {dict}))
1029 for aweme_id in traverse_obj(data, ('videoList', ..., 'id', {str})):
1030 webpage_url = self._create_url(user_name, aweme_id)
1031 video_data, _ = self._extract_web_data_and_status(webpage_url, aweme_id, fatal=False)
1032 sec_uid = self._parse_aweme_video_web(
1033 video_data, webpage_url, aweme_id, extract_flat=True).get('channel_id')
1034 if sec_uid:
1035 break
1037 if not sec_uid:
1038 raise ExtractorError(
1039 'Unable to extract secondary user ID. If you are able to get the channel_id '
1040 'from a video posted by this user, try using "tiktokuser:channel_id" as the '
1041 'input URL (replacing `channel_id` with its actual value)', expected=True)
1043 return self.playlist_result(self._entries(sec_uid, user_name), sec_uid, user_name)
1046 class TikTokBaseListIE(TikTokBaseIE): # XXX: Conventionally, base classes should end with BaseIE/InfoExtractor
1047 def _entries(self, list_id, display_id):
1048 query = {
1049 self._QUERY_NAME: list_id,
1050 'cursor': 0,
1051 'count': 20,
1052 'type': 5,
1053 'device_id': self._DEVICE_ID,
1056 for page in itertools.count(1):
1057 for retry in self.RetryManager():
1058 try:
1059 post_list = self._call_api(
1060 self._API_ENDPOINT, display_id, query=query,
1061 note=f'Downloading video list page {page}',
1062 errnote='Unable to download video list')
1063 except ExtractorError as e:
1064 if isinstance(e.cause, json.JSONDecodeError) and e.cause.pos == 0:
1065 retry.error = e
1066 continue
1067 raise
1068 for video in post_list.get('aweme_list', []):
1069 yield {
1070 **self._parse_aweme_video_app(video),
1071 'extractor_key': TikTokIE.ie_key(),
1072 'extractor': 'TikTok',
1073 'webpage_url': f'https://tiktok.com/@_/video/{video["aweme_id"]}',
1075 if not post_list.get('has_more'):
1076 break
1077 query['cursor'] = post_list['cursor']
1079 def _real_extract(self, url):
1080 list_id = self._match_id(url)
1081 return self.playlist_result(self._entries(list_id, list_id), list_id)
1084 class TikTokSoundIE(TikTokBaseListIE):
1085 IE_NAME = 'tiktok:sound'
1086 _VALID_URL = r'https?://(?:www\.)?tiktok\.com/music/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
1087 _WORKING = False
1088 _QUERY_NAME = 'music_id'
1089 _API_ENDPOINT = 'music/aweme'
1090 _TESTS = [{
1091 'url': 'https://www.tiktok.com/music/Build-a-Btch-6956990112127585029?lang=en',
1092 'playlist_mincount': 100,
1093 'info_dict': {
1094 'id': '6956990112127585029',
1096 'expected_warnings': ['Retrying'],
1097 }, {
1098 # Actual entries are less than listed video count
1099 'url': 'https://www.tiktok.com/music/jiefei-soap-remix-7036843036118469381',
1100 'playlist_mincount': 2182,
1101 'info_dict': {
1102 'id': '7036843036118469381',
1104 'expected_warnings': ['Retrying'],
1108 class TikTokEffectIE(TikTokBaseListIE):
1109 IE_NAME = 'tiktok:effect'
1110 _VALID_URL = r'https?://(?:www\.)?tiktok\.com/sticker/[\w\.-]+-(?P<id>[\d]+)[/?#&]?'
1111 _WORKING = False
1112 _QUERY_NAME = 'sticker_id'
1113 _API_ENDPOINT = 'sticker/aweme'
1114 _TESTS = [{
1115 'url': 'https://www.tiktok.com/sticker/MATERIAL-GWOOORL-1258156',
1116 'playlist_mincount': 100,
1117 'info_dict': {
1118 'id': '1258156',
1120 'expected_warnings': ['Retrying'],
1121 }, {
1122 # Different entries between mobile and web, depending on region
1123 'url': 'https://www.tiktok.com/sticker/Elf-Friend-479565',
1124 'only_matching': True,
1128 class TikTokTagIE(TikTokBaseListIE):
1129 IE_NAME = 'tiktok:tag'
1130 _VALID_URL = r'https?://(?:www\.)?tiktok\.com/tag/(?P<id>[^/?#&]+)'
1131 _WORKING = False
1132 _QUERY_NAME = 'ch_id'
1133 _API_ENDPOINT = 'challenge/aweme'
1134 _TESTS = [{
1135 'url': 'https://tiktok.com/tag/hello2018',
1136 'playlist_mincount': 39,
1137 'info_dict': {
1138 'id': '46294678',
1139 'title': 'hello2018',
1141 'expected_warnings': ['Retrying'],
1142 }, {
1143 'url': 'https://tiktok.com/tag/fypシ?is_copy_url=0&is_from_webapp=v1',
1144 'only_matching': True,
1147 def _real_extract(self, url):
1148 display_id = self._match_id(url)
1149 webpage = self._download_webpage(url, display_id, headers={
1150 'User-Agent': 'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)',
1152 tag_id = self._html_search_regex(r'snssdk\d*://challenge/detail/(\d+)', webpage, 'tag ID')
1153 return self.playlist_result(self._entries(tag_id, display_id), tag_id, display_id)
1156 class TikTokCollectionIE(TikTokBaseIE):
1157 IE_NAME = 'tiktok:collection'
1158 _VALID_URL = r'https?://www\.tiktok\.com/@(?P<user_id>[\w.-]+)/collection/(?P<title>[^/?#]+)-(?P<id>\d+)/?(?:[?#]|$)'
1159 _TESTS = [{
1160 # playlist should have exactly 9 videos
1161 'url': 'https://www.tiktok.com/@imanoreotwe/collection/count-test-7371330159376370462',
1162 'info_dict': {
1163 'id': '7371330159376370462',
1164 'title': 'imanoreotwe-count-test',
1166 'playlist_count': 9,
1167 }, {
1168 # tests returning multiple pages of a large collection
1169 'url': 'https://www.tiktok.com/@imanoreotwe/collection/%F0%9F%98%82-7111887189571160875',
1170 'info_dict': {
1171 'id': '7111887189571160875',
1172 'title': 'imanoreotwe-%F0%9F%98%82',
1174 'playlist_mincount': 100,
1176 _API_BASE_URL = 'https://www.tiktok.com/api/collection/item_list/'
1177 _PAGE_COUNT = 30
1179 def _build_web_query(self, collection_id, cursor):
1180 return {
1181 'aid': '1988',
1182 'collectionId': collection_id,
1183 'count': self._PAGE_COUNT,
1184 'cursor': cursor,
1185 'sourceType': '113',
1188 def _entries(self, collection_id):
1189 cursor = 0
1190 for page in itertools.count(1):
1191 response = self._download_json(
1192 self._API_BASE_URL, collection_id, f'Downloading page {page}',
1193 query=self._build_web_query(collection_id, cursor))
1195 for video in traverse_obj(response, ('itemList', lambda _, v: v['id'])):
1196 video_id = video['id']
1197 author = traverse_obj(video, ('author', ('uniqueId', 'secUid', 'id'), {str}, any)) or '_'
1198 webpage_url = self._create_url(author, video_id)
1199 yield self.url_result(
1200 webpage_url, TikTokIE,
1201 **self._parse_aweme_video_web(video, webpage_url, video_id, extract_flat=True))
1203 if not traverse_obj(response, 'hasMore'):
1204 break
1205 cursor += self._PAGE_COUNT
1207 def _real_extract(self, url):
1208 collection_id, title, user_name = self._match_valid_url(url).group('id', 'title', 'user_id')
1210 return self.playlist_result(
1211 self._entries(collection_id), collection_id, '-'.join((user_name, title)))
1214 class DouyinIE(TikTokBaseIE):
1215 _VALID_URL = r'https?://(?:www\.)?douyin\.com/video/(?P<id>[0-9]+)'
1216 _TESTS = [{
1217 'url': 'https://www.douyin.com/video/6961737553342991651',
1218 'md5': '9ecce7bc5b302601018ecb2871c63a75',
1219 'info_dict': {
1220 'id': '6961737553342991651',
1221 'ext': 'mp4',
1222 'title': '#杨超越 小小水手带你去远航❤️',
1223 'description': '#杨超越 小小水手带你去远航❤️',
1224 'uploader': '6897520xka',
1225 'uploader_id': '110403406559',
1226 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
1227 'channel_id': 'MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
1228 'channel': '杨超越',
1229 'duration': 19,
1230 'timestamp': 1620905839,
1231 'upload_date': '20210513',
1232 'track': '@杨超越创作的原声',
1233 'artists': ['杨超越'],
1234 'view_count': int,
1235 'like_count': int,
1236 'repost_count': int,
1237 'comment_count': int,
1238 'thumbnail': r're:https?://.+\.jpe?g',
1240 }, {
1241 'url': 'https://www.douyin.com/video/6982497745948921092',
1242 'md5': '15c5e660b7048af3707304e3cc02bbb5',
1243 'info_dict': {
1244 'id': '6982497745948921092',
1245 'ext': 'mp4',
1246 'title': '这个夏日和小羊@杨超越 一起遇见白色幻想',
1247 'description': '这个夏日和小羊@杨超越 一起遇见白色幻想',
1248 'uploader': '0731chaoyue',
1249 'uploader_id': '408654318141572',
1250 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAZJpnglcjW2f_CMVcnqA_6oVBXKWMpH0F8LIHuUu8-lA',
1251 'channel_id': 'MS4wLjABAAAAZJpnglcjW2f_CMVcnqA_6oVBXKWMpH0F8LIHuUu8-lA',
1252 'channel': '杨超越工作室',
1253 'duration': 42,
1254 'timestamp': 1625739481,
1255 'upload_date': '20210708',
1256 'track': '@杨超越工作室创作的原声',
1257 'artists': ['杨超越工作室'],
1258 'view_count': int,
1259 'like_count': int,
1260 'repost_count': int,
1261 'comment_count': int,
1262 'thumbnail': r're:https?://.+\.jpe?g',
1264 }, {
1265 'url': 'https://www.douyin.com/video/6953975910773099811',
1266 'md5': '0e6443758b8355db9a3c34864a4276be',
1267 'info_dict': {
1268 'id': '6953975910773099811',
1269 'ext': 'mp4',
1270 'title': '#一起看海 出现在你的夏日里',
1271 'description': '#一起看海 出现在你的夏日里',
1272 'uploader': '6897520xka',
1273 'uploader_id': '110403406559',
1274 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
1275 'channel_id': 'MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
1276 'channel': '杨超越',
1277 'duration': 17,
1278 'timestamp': 1619098692,
1279 'upload_date': '20210422',
1280 'track': '@杨超越创作的原声',
1281 'artists': ['杨超越'],
1282 'view_count': int,
1283 'like_count': int,
1284 'repost_count': int,
1285 'comment_count': int,
1286 'thumbnail': r're:https?://.+\.jpe?g',
1288 }, {
1289 'url': 'https://www.douyin.com/video/6950251282489675042',
1290 'md5': 'b4db86aec367ef810ddd38b1737d2fed',
1291 'info_dict': {
1292 'id': '6950251282489675042',
1293 'ext': 'mp4',
1294 'title': '哈哈哈,成功了哈哈哈哈哈哈',
1295 'uploader': '杨超越',
1296 'upload_date': '20210412',
1297 'timestamp': 1618231483,
1298 'uploader_id': '110403406559',
1299 'view_count': int,
1300 'like_count': int,
1301 'repost_count': int,
1302 'comment_count': int,
1304 'skip': 'No longer available',
1305 }, {
1306 'url': 'https://www.douyin.com/video/6963263655114722595',
1307 'md5': '1440bcf59d8700f8e014da073a4dfea8',
1308 'info_dict': {
1309 'id': '6963263655114722595',
1310 'ext': 'mp4',
1311 'title': '#哪个爱豆的105度最甜 换个角度看看我哈哈',
1312 'description': '#哪个爱豆的105度最甜 换个角度看看我哈哈',
1313 'uploader': '6897520xka',
1314 'uploader_id': '110403406559',
1315 'uploader_url': 'https://www.douyin.com/user/MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
1316 'channel_id': 'MS4wLjABAAAAEKnfa654JAJ_N5lgZDQluwsxmY0lhfmEYNQBBkwGG98',
1317 'channel': '杨超越',
1318 'duration': 15,
1319 'timestamp': 1621261163,
1320 'upload_date': '20210517',
1321 'track': '@杨超越创作的原声',
1322 'artists': ['杨超越'],
1323 'view_count': int,
1324 'like_count': int,
1325 'repost_count': int,
1326 'comment_count': int,
1327 'thumbnail': r're:https?://.+\.jpe?g',
1330 _UPLOADER_URL_FORMAT = 'https://www.douyin.com/user/%s'
1331 _WEBPAGE_HOST = 'https://www.douyin.com/'
1333 def _real_extract(self, url):
1334 video_id = self._match_id(url)
1336 detail = traverse_obj(self._download_json(
1337 'https://www.douyin.com/aweme/v1/web/aweme/detail/', video_id,
1338 'Downloading web detail JSON', 'Failed to download web detail JSON',
1339 query={'aweme_id': video_id}, fatal=False), ('aweme_detail', {dict}))
1340 if not detail:
1341 # TODO: Run verification challenge code to generate signature cookies
1342 raise ExtractorError(
1343 'Fresh cookies (not necessarily logged in) are needed',
1344 expected=not self._get_cookies(self._WEBPAGE_HOST).get('s_v_web_id'))
1346 return self._parse_aweme_video_app(detail)
1349 class TikTokVMIE(InfoExtractor):
1350 _VALID_URL = r'https?://(?:(?:vm|vt)\.tiktok\.com|(?:www\.)tiktok\.com/t)/(?P<id>\w+)'
1351 IE_NAME = 'vm.tiktok'
1353 _TESTS = [{
1354 'url': 'https://www.tiktok.com/t/ZTRC5xgJp',
1355 'info_dict': {
1356 'id': '7170520270497680683',
1357 'ext': 'mp4',
1358 'title': 'md5:c64f6152330c2efe98093ccc8597871c',
1359 'uploader_id': '6687535061741700102',
1360 'upload_date': '20221127',
1361 'view_count': int,
1362 'like_count': int,
1363 'comment_count': int,
1364 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAObqu3WCTXxmw2xwZ3iLEHnEecEIw7ks6rxWqOqOhaPja9BI7gqUQnjw8_5FSoDXX',
1365 'album': 'Wave of Mutilation: Best of Pixies',
1366 'thumbnail': r're:https://.+\.webp.*',
1367 'duration': 5,
1368 'timestamp': 1669516858,
1369 'repost_count': int,
1370 'artist': 'Pixies',
1371 'track': 'Where Is My Mind?',
1372 'description': 'md5:c64f6152330c2efe98093ccc8597871c',
1373 'uploader': 'sigmachaddeus',
1374 'creator': 'SigmaChad',
1376 }, {
1377 'url': 'https://vm.tiktok.com/ZTR45GpSF/',
1378 'info_dict': {
1379 'id': '7106798200794926362',
1380 'ext': 'mp4',
1381 'title': 'md5:edc3e7ea587847f8537468f2fe51d074',
1382 'uploader_id': '6997695878846268418',
1383 'upload_date': '20220608',
1384 'view_count': int,
1385 'like_count': int,
1386 'comment_count': int,
1387 'thumbnail': r're:https://.+\.webp.*',
1388 'uploader_url': 'https://www.tiktok.com/@MS4wLjABAAAAdZ_NcPPgMneaGrW0hN8O_J_bwLshwNNERRF5DxOw2HKIzk0kdlLrR8RkVl1ksrMO',
1389 'duration': 29,
1390 'timestamp': 1654680400,
1391 'repost_count': int,
1392 'artist': 'Akihitoko',
1393 'track': 'original sound',
1394 'description': 'md5:edc3e7ea587847f8537468f2fe51d074',
1395 'uploader': 'akihitoko1',
1396 'creator': 'Akihitoko',
1398 }, {
1399 'url': 'https://vt.tiktok.com/ZSe4FqkKd',
1400 'only_matching': True,
1403 def _real_extract(self, url):
1404 new_url = self._request_webpage(
1405 HEADRequest(url), self._match_id(url), headers={'User-Agent': 'facebookexternalhit/1.1'}).url
1406 if self.suitable(new_url): # Prevent infinite loop in case redirect fails
1407 raise UnsupportedError(new_url)
1408 return self.url_result(new_url)
1411 class TikTokLiveIE(TikTokBaseIE):
1412 _VALID_URL = r'''(?x)https?://(?:
1413 (?:www\.)?tiktok\.com/@(?P<uploader>[\w.-]+)/live|
1414 m\.tiktok\.com/share/live/(?P<id>\d+)
1415 )'''
1416 IE_NAME = 'tiktok:live'
1418 _TESTS = [{
1419 'url': 'https://www.tiktok.com/@weathernewslive/live',
1420 'info_dict': {
1421 'id': '7210809319192726273',
1422 'ext': 'mp4',
1423 'title': r're:ウェザーニュースLiVE[\d\s:-]*',
1424 'creator': 'ウェザーニュースLiVE',
1425 'uploader': 'weathernewslive',
1426 'uploader_id': '6621496731283095554',
1427 'uploader_url': 'https://www.tiktok.com/@weathernewslive',
1428 'live_status': 'is_live',
1429 'concurrent_view_count': int,
1431 'params': {'skip_download': 'm3u8'},
1432 }, {
1433 'url': 'https://www.tiktok.com/@pilarmagenta/live',
1434 'info_dict': {
1435 'id': '7209423610325322522',
1436 'ext': 'mp4',
1437 'title': str,
1438 'creator': 'Pilarmagenta',
1439 'uploader': 'pilarmagenta',
1440 'uploader_id': '6624846890674683909',
1441 'uploader_url': 'https://www.tiktok.com/@pilarmagenta',
1442 'live_status': 'is_live',
1443 'concurrent_view_count': int,
1445 'skip': 'Livestream',
1446 }, {
1447 'url': 'https://m.tiktok.com/share/live/7209423610325322522/?language=en',
1448 'only_matching': True,
1449 }, {
1450 'url': 'https://www.tiktok.com/@iris04201/live',
1451 'only_matching': True,
1454 def _call_api(self, url, param, room_id, uploader, key=None):
1455 response = traverse_obj(self._download_json(
1456 url, room_id, fatal=False, query={
1457 'aid': '1988',
1458 param: room_id,
1459 }), (key, {dict}), default={})
1461 # status == 2 if live else 4
1462 if int_or_none(response.get('status')) == 2:
1463 return response
1464 # If room_id is obtained via mobile share URL and cannot be refreshed, do not wait for live
1465 elif not uploader:
1466 raise ExtractorError('This livestream has ended', expected=True)
1467 raise UserNotLive(video_id=uploader)
1469 def _real_extract(self, url):
1470 uploader, room_id = self._match_valid_url(url).group('uploader', 'id')
1471 webpage = self._download_webpage(
1472 url, uploader or room_id, headers={'User-Agent': 'Mozilla/5.0'}, fatal=not room_id)
1474 if webpage:
1475 data = self._get_sigi_state(webpage, uploader or room_id)
1476 room_id = (
1477 traverse_obj(data, ((
1478 ('LiveRoom', 'liveRoomUserInfo', 'user'),
1479 ('UserModule', 'users', ...)), 'roomId', {str}, any))
1480 or self._search_regex(r'snssdk\d*://live\?room_id=(\d+)', webpage, 'room ID', default=room_id))
1481 uploader = uploader or traverse_obj(
1482 data, ('LiveRoom', 'liveRoomUserInfo', 'user', 'uniqueId'),
1483 ('UserModule', 'users', ..., 'uniqueId'), get_all=False, expected_type=str)
1485 if not room_id:
1486 raise UserNotLive(video_id=uploader)
1488 formats = []
1489 live_info = self._call_api(
1490 'https://webcast.tiktok.com/webcast/room/info', 'room_id', room_id, uploader, key='data')
1492 get_quality = qualities(('SD1', 'ld', 'SD2', 'sd', 'HD1', 'hd', 'FULL_HD1', 'uhd', 'ORIGION', 'origin'))
1493 parse_inner = lambda x: self._parse_json(x, None)
1495 for quality, stream in traverse_obj(live_info, (
1496 'stream_url', 'live_core_sdk_data', 'pull_data', 'stream_data',
1497 {parse_inner}, 'data', {dict}), default={}).items():
1499 sdk_params = traverse_obj(stream, ('main', 'sdk_params', {parse_inner}, {
1500 'vcodec': ('VCodec', {str}),
1501 'tbr': ('vbitrate', {int_or_none(scale=1000)}),
1502 'resolution': ('resolution', {lambda x: re.match(r'(?i)\d+x\d+|\d+p', x).group().lower()}),
1505 flv_url = traverse_obj(stream, ('main', 'flv', {url_or_none}))
1506 if flv_url:
1507 formats.append({
1508 'url': flv_url,
1509 'ext': 'flv',
1510 'format_id': f'flv-{quality}',
1511 'quality': get_quality(quality),
1512 **sdk_params,
1515 hls_url = traverse_obj(stream, ('main', 'hls', {url_or_none}))
1516 if hls_url:
1517 formats.append({
1518 'url': hls_url,
1519 'ext': 'mp4',
1520 'protocol': 'm3u8_native',
1521 'format_id': f'hls-{quality}',
1522 'quality': get_quality(quality),
1523 **sdk_params,
1526 def get_vcodec(*keys):
1527 return traverse_obj(live_info, (
1528 'stream_url', *keys, {parse_inner}, 'VCodec', {str}))
1530 for stream in ('hls', 'rtmp'):
1531 stream_url = traverse_obj(live_info, ('stream_url', f'{stream}_pull_url', {url_or_none}))
1532 if stream_url:
1533 formats.append({
1534 'url': stream_url,
1535 'ext': 'mp4' if stream == 'hls' else 'flv',
1536 'protocol': 'm3u8_native' if stream == 'hls' else 'https',
1537 'format_id': f'{stream}-pull',
1538 'vcodec': get_vcodec(f'{stream}_pull_url_params'),
1539 'quality': get_quality('ORIGION'),
1542 for f_id, f_url in traverse_obj(live_info, ('stream_url', 'flv_pull_url', {dict}), default={}).items():
1543 if not url_or_none(f_url):
1544 continue
1545 formats.append({
1546 'url': f_url,
1547 'ext': 'flv',
1548 'format_id': f'flv-{f_id}'.lower(),
1549 'vcodec': get_vcodec('flv_pull_url_params', f_id),
1550 'quality': get_quality(f_id),
1553 # If uploader is a guest on another's livestream, primary endpoint will not have m3u8 URLs
1554 if not traverse_obj(formats, lambda _, v: v['ext'] == 'mp4'):
1555 live_info = merge_dicts(live_info, self._call_api(
1556 'https://www.tiktok.com/api/live/detail/', 'roomID', room_id, uploader, key='LiveRoomInfo'))
1557 if url_or_none(live_info.get('liveUrl')):
1558 formats.append({
1559 'url': live_info['liveUrl'],
1560 'ext': 'mp4',
1561 'protocol': 'm3u8_native',
1562 'format_id': 'hls-fallback',
1563 'vcodec': 'h264',
1564 'quality': get_quality('origin'),
1567 uploader = uploader or traverse_obj(live_info, ('ownerInfo', 'uniqueId'), ('owner', 'display_id'))
1569 return {
1570 'id': room_id,
1571 'uploader': uploader,
1572 'uploader_url': format_field(uploader, None, self._UPLOADER_URL_FORMAT) or None,
1573 'is_live': True,
1574 'formats': formats,
1575 '_format_sort_fields': ('quality', 'ext'),
1576 **traverse_obj(live_info, {
1577 'title': 'title',
1578 'uploader_id': (('ownerInfo', 'owner'), 'id', {str_or_none}),
1579 'creator': (('ownerInfo', 'owner'), 'nickname'),
1580 'concurrent_view_count': (('user_count', ('liveRoomStats', 'userCount')), {int_or_none}),
1581 }, get_all=False),