├── LogoTikTok.png ├── README.md ├── requirements.txt └── tik-tok-downloader.py /LogoTikTok.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vgvr0/TikTok-Video-Downloader-using-Python-and-yt-dlp/15d0bc015694a819d12acc788dda0a320fd07540/LogoTikTok.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🎵 TikTok Video Downloader Pro 2 | ![TikTok Cover](LogoTikTok.png) 3 | 4 | A powerful and efficient Python tool for downloading TikTok videos with advanced features and error handling. 5 | 6 | [![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://www.python.org/) 7 | [![PyCharm](https://img.shields.io/badge/pycharm-143?style=for-the-badge&logo=pycharm&logoColor=black&color=green&labelColor=green)](https://www.jetbrains.com/pycharm/) 8 | [![Git](https://img.shields.io/badge/git-F05032?style=for-the-badge&logo=git&logoColor=white)](https://git-scm.com/) 9 | [![Telegram](https://img.shields.io/badge/telegram-2CA5E0?style=for-the-badge&logo=telegram&logoColor=white)](https://telegram.org/) 10 | [![yt-dlp](https://img.shields.io/badge/yt--dlp-FF0000?style=for-the-badge&logo=youtube&logoColor=white)](https://github.com/yt-dlp/yt-dlp) 11 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge)](https://opensource.org/licenses/MIT) 12 | 13 | ## ✨ Features 14 | 15 | - 📥 Download TikTok videos in highest quality available 16 | - 🎯 Custom filename support with timestamps 17 | - 🗂️ Automatic directory management 18 | - 🔄 Progress tracking with real-time updates 19 | - 🛡️ Robust error handling and validation 20 | - 🌐 Browser cookie integration for better compatibility 21 | - 📝 Detailed logging and status reporting 22 | 23 | ## 🚀 Prerequisites 24 | 25 | - Python 3.7 or higher 26 | - Required packages: 27 | ``` 28 | yt-dlp>=2023.3.4 29 | typing>=3.7.4 30 | ``` 31 | 32 | ## 📦 Installation 33 | 34 | 1. Clone the repository: 35 | ```bash 36 | git clone https://github.com/vgvr0/TikTok-Video-Downloader-using-Python-and-yt-dlp 37 | cd tiktok-downloader-pro 38 | ``` 39 | 40 | 2. Create a virtual environment (optional but recommended): 41 | ```bash 42 | python -m venv venv 43 | source venv/bin/activate # On Windows: venv\Scripts\activate 44 | ``` 45 | 46 | 3. Install dependencies: 47 | ```bash 48 | pip install -r requirements.txt 49 | ``` 50 | 51 | ## 💻 Usage 52 | 53 | ### Basic Usage 54 | 55 | ```python 56 | from tiktok_downloader import TikTokDownloader 57 | 58 | # Initialize downloader 59 | downloader = TikTokDownloader() 60 | 61 | # Download single video 62 | video_url = "https://www.tiktok.com/@username/video/1234567890" 63 | downloader.download_video(video_url) 64 | ``` 65 | 66 | ### Advanced Usage 67 | 68 | ```python 69 | # Custom save location and filename 70 | downloader = TikTokDownloader(save_path='my_tiktoks') 71 | downloader.download_video(video_url, custom_name="dance_video") 72 | 73 | # Download multiple videos 74 | urls = [ 75 | "https://www.tiktok.com/@user1/video/1234567890", 76 | "https://www.tiktok.com/@user2/video/0987654321" 77 | ] 78 | for url in urls: 79 | downloader.download_video(url) 80 | ``` 81 | 82 | ## 🛠️ Configuration Options 83 | 84 | | Parameter | Description | Default | 85 | |-----------|-------------|---------| 86 | | save_path | Directory to save videos | 'tiktok_videos' | 87 | | custom_name | Custom name for the video | None | 88 | | use_cookies | Use browser cookies | True | 89 | | quiet_mode | Suppress progress output | False | 90 | 91 | ## 📋 Example Output 92 | 93 | ``` 94 | Downloading: 45.2% at 1.2 MB/s ETA: 00:15 95 | Download completed, finalizing... 96 | Video successfully downloaded: my_tiktoks/dance_video_20240106_123045.mp4 97 | ``` 98 | 99 | ## 🤝 Contributing 100 | 101 | 1. Fork the repository 102 | 2. Create your feature branch (`git checkout -b feature/AmazingFeature`) 103 | 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) 104 | 4. Push to the branch (`git push origin feature/AmazingFeature`) 105 | 5. Open a Pull Request 106 | 107 | ## 📝 License 108 | 109 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 110 | 111 | ## 🙏 Acknowledgments 112 | 113 | - [yt-dlp](https://github.com/yt-dlp/yt-dlp) for the amazing download capabilities 114 | - TikTok for providing the content platform 115 | - All contributors and users of this project 116 | 117 | ## ⚠️ Disclaimer 118 | 119 | This tool is for educational purposes only. Make sure to comply with TikTok's terms of service and respect content creators' rights when using this tool. 120 | 121 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | yt-dlp>=2023.12.30 2 | regex>=2023.10.3 3 | -------------------------------------------------------------------------------- /tik-tok-downloader.py: -------------------------------------------------------------------------------- 1 | import yt_dlp 2 | import os 3 | import re 4 | from typing import Optional, Dict, Any 5 | from datetime import datetime 6 | 7 | class TikTokDownloader: 8 | def __init__(self, save_path: str = 'tiktok_videos'): 9 | """ 10 | Initialize TikTok downloader with configurable save path 11 | 12 | Args: 13 | save_path (str): Directory where videos will be saved 14 | """ 15 | self.save_path = save_path 16 | self.create_save_directory() 17 | 18 | def create_save_directory(self) -> None: 19 | """Create the save directory if it doesn't exist""" 20 | if not os.path.exists(self.save_path): 21 | os.makedirs(self.save_path) 22 | 23 | @staticmethod 24 | def validate_url(url: str) -> bool: 25 | """ 26 | Validate if the provided URL is a TikTok URL 27 | 28 | Args: 29 | url (str): URL to validate 30 | 31 | Returns: 32 | bool: True if valid, False otherwise 33 | """ 34 | tiktok_pattern = r'https?://((?:vm|vt|www)\.)?tiktok\.com/.*' 35 | return bool(re.match(tiktok_pattern, url)) 36 | 37 | @staticmethod 38 | def progress_hook(d: Dict[str, Any]) -> None: 39 | """ 40 | Hook to display download progress 41 | 42 | Args: 43 | d (Dict[str, Any]): Progress information dictionary 44 | """ 45 | if d['status'] == 'downloading': 46 | progress = d.get('_percent_str', 'N/A') 47 | speed = d.get('_speed_str', 'N/A') 48 | eta = d.get('_eta_str', 'N/A') 49 | print(f"Downloading: {progress} at {speed} ETA: {eta}", end='\r') 50 | elif d['status'] == 'finished': 51 | print("\nDownload completed, finalizing...") 52 | 53 | def get_filename(self, video_url: str, custom_name: Optional[str] = None) -> str: 54 | """ 55 | Generate filename for the video 56 | 57 | Args: 58 | video_url (str): Video URL 59 | custom_name (Optional[str]): Custom name for the video file 60 | 61 | Returns: 62 | str: Generated filename 63 | """ 64 | timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") 65 | if custom_name: 66 | return f"{custom_name}_{timestamp}.mp4" 67 | return f"tiktok_{timestamp}.mp4" 68 | 69 | def download_video(self, video_url: str, custom_name: Optional[str] = None) -> Optional[str]: 70 | """ 71 | Download TikTok video 72 | 73 | Args: 74 | video_url (str): URL of the TikTok video 75 | custom_name (Optional[str]): Custom name for the video file 76 | 77 | Returns: 78 | Optional[str]: Path to downloaded file if successful, None otherwise 79 | """ 80 | if not self.validate_url(video_url): 81 | print("Error: Invalid TikTok URL") 82 | return None 83 | 84 | filename = self.get_filename(video_url, custom_name) 85 | output_path = os.path.join(self.save_path, filename) 86 | 87 | ydl_opts = { 88 | 'outtmpl': output_path, 89 | 'format': 'best', 90 | 'noplaylist': True, 91 | 'quiet': False, 92 | 'progress_hooks': [self.progress_hook], 93 | 'cookiesfrombrowser': ('chrome',), # Use Chrome cookies for authentication 94 | 'extractor_args': {'tiktok': {'webpage_download': True}}, 95 | 'http_headers': { 96 | 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' 97 | } 98 | } 99 | 100 | try: 101 | with yt_dlp.YoutubeDL(ydl_opts) as ydl: 102 | ydl.download([video_url]) 103 | print(f"\nVideo successfully downloaded: {output_path}") 104 | return output_path 105 | 106 | except yt_dlp.utils.DownloadError as e: 107 | print(f"Error downloading video: {str(e)}") 108 | except Exception as e: 109 | print(f"An unexpected error occurred: {str(e)}") 110 | 111 | return None 112 | 113 | # Example usage 114 | if __name__ == "__main__": 115 | downloader = TikTokDownloader(save_path='downloaded_tiktoks') 116 | video_url = "https://www.tiktok.com/@zachking/video/6768504823336815877" 117 | 118 | # Basic usage 119 | downloader.download_video(video_url) 120 | 121 | # With custom filename 122 | downloader.download_video(video_url, custom_name="zach_king_magic") 123 | --------------------------------------------------------------------------------