Overview
TheTikTokClient class provides a robust interface for extracting TikTok video and music data. It uses yt-dlp internally with browser impersonation (TLS fingerprint spoofing) to bypass TikTok’s anti-bot detection.
Key features:
- Automatic proxy rotation for reliability
- 3-part retry strategy (URL resolution → video info → download)
- Support for videos and slideshows (image posts)
- Chrome 120 browser impersonation to avoid WAF blocking
- Cookie-based authentication support
TikTokClient
Constructor
Optional ProxyManager instance for round-robin proxy rotation. If provided, each request will use the next proxy in rotation.
If True, proxy is used only for API extraction, not for media downloads. Useful when you have a fast local connection but need proxies to bypass geo-restrictions on the API.
Optional path to a Netscape-format cookies file (e.g., exported from browser). If not provided, uses the
YTDLP_COOKIES environment variable. If the file doesn’t exist, a warning is logged and cookies are not used.Methods
video()
Extract video or slideshow information from a TikTok URL.TikTok URL to extract. Supports:
- Full URLs:
https://www.tiktok.com/@user/video/123 - Photo URLs:
https://www.tiktok.com/@user/photo/123 - Short URLs:
https://vm.tiktok.com/XXX,https://vt.tiktok.com/XXX
Maximum retry attempts for URL resolution. If None, uses config value (default: 3).
Maximum retry attempts for video info extraction. If None, uses config value (default: 3).
Maximum retry attempts for media download. If None, uses config value (default: 3).
Video or slideshow information object. See VideoInfo model for details.
TikTokInvalidLinkError- Invalid or expired TikTok linkTikTokDeletedError- Video was deleted by the creatorTikTokPrivateError- Video is privateTikTokRegionError- Video is geo-blocked in your regionTikTokExtractionError- Failed to extract video dataTikTokNetworkError- Network error during downloadTikTokVideoTooLongError- Video exceeds maximum duration (if configured)
music()
Extract music/audio from a TikTok video.TikTok URL from which to extract music. Same URL formats as
video() method.Maximum retry attempts for URL resolution.
Maximum retry attempts for video info extraction.
Maximum retry attempts for audio download.
Music information object with audio bytes. See MusicInfo model for details.
video() method.
download_slideshow_images()
Download all images from a slideshow post with individual retry per image.VideoInfo object for a slideshow (type=“images”). Must have a valid download context.
ProxySession for proxy management and rotation on retry.
Maximum retry attempts per image. If None, uses config value (default: 3).
List of image bytes in the same order as the URLs in video_info.image_urls.
ValueError- If video_info is not a slideshow or has no download contextTikTokNetworkError- If any image fails to download after all retries
Class Methods
close_curl_session()
Close all curl_cffi sessions in the pool. Call this on application shutdown.close_connector()
Close shared aiohttp connector. Call this on application shutdown.shutdown_executor()
Shutdown the shared ThreadPoolExecutor. Call this on application shutdown.Complete Example
ProxySession
Manages proxy state for a single video request flow, ensuring the same proxy is used across all parts unless a retry is triggered.Constructor
ProxyManager instance for proxy rotation. Can be None for direct connections.
Methods
get_proxy()
Get the current proxy (lazily initialized on first call).Proxy URL string, or None for direct connection.
rotate_proxy()
Rotate to the next proxy in the rotation (for retries).New proxy URL string, or None for direct connection.
Example Usage
Architecture Notes
3-Part Retry Strategy
The client implements a 3-part retry strategy with independent proxy rotation:- URL Resolution - Resolves short URLs (vm.tiktok.com) to full URLs
- Video Info Extraction - Extracts metadata and media URLs from TikTok
- Media Download - Downloads video/audio/images from TikTok CDN
Chrome 120 Impersonation
TikTok’s WAF blocks newer Chrome versions (136+) when used with proxies due to TLS fingerprint mismatches. The client uses Chrome 120 impersonation which is known to work reliably:- TLS fingerprint: Chrome 120
- User-Agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Resource Management
The client uses shared resources for optimal performance:- ThreadPoolExecutor: 500 workers for sync yt-dlp calls
- curl_cffi sessions: 1000 connections per proxy (pooled by proxy URL)
- aiohttp connector: Unlimited connections for URL resolution
VideoInfo Context Manager
For slideshows, VideoInfo keeps the yt-dlp instance alive for image downloads. Always use a context manager or callclose() explicitly:

