This repository contains a Python script that allows you to download TikTok videos using the yt-dlp
library. The script is simple to use and can save videos to a specified directory.
- Download TikTok videos in the best available format.
- Automatically create a save directory if it does not exist.
- Handle download errors gracefully.
- Python 3.x
yt-dlp
library
-
Install
yt-dlp
:pip install yt-dlp
-
Clone this repository or download the script file.
Save the script below to a file, e.g., download_tiktok.py
.
import yt_dlp
import os
def download_tiktok_video(video_url, save_path='tiktok_videos'):
# Ensure the save directory exists
if not os.path.exists(save_path):
os.makedirs(save_path)
# Configure yt-dlp options
ydl_opts = {
'outtmpl': os.path.join(save_path, '%(id)s.%(ext)s'),
'format': 'best',
}
try:
# Create a yt-dlp object and download the video
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_url, download=True)
filename = ydl.prepare_filename(info)
print(f"Video successfully downloaded: {filename}")
except Exception as e:
print(f"Error downloading video: {str(e)}")
# Example usage
video_url = "https://www.tiktok.com/@zachking/video/6768504823336815877?embed_source=121374463%2C121439635%2C121433650%2C121404358%2C121351166%2C121331973%2C120811592%2C120810756%3Bnull%3Bembed_blank&refer=embed&referer_url=marketing4all.es%2Flistas%2Fvideos-mas-vistos-en-tiktok-2024%2F&referer_video_id=6768504823336815877"
download_tiktok_video(video_url)
- Save the script to a file, e.g.,
download_tiktok.py
. - Run the script:
python download_tiktok.py
- The script will download the specified TikTok video and save it in the
tiktok_videos
directory (or any directory you specify).
video_url
: URL of the TikTok video.save_path
: Directory where the video will be saved (default:'tiktok_videos'
).
video_url = "https://www.tiktok.com/@zachking/video/6768504823336815877?embed_source=121374463%2C121439635%2C121433650%2C121404358%2C121351166%2C121331973%2C120811592%2C120810756%3Bnull%3Bembed_blank&refer=embed&referer_url=marketing4all.es%2Flistas%2Fvideos-mas-vistos-en-tiktok-2024%2F&referer_video_id=6768504823336815877"
download_tiktok_video(video_url)
This example downloads the specified TikTok video and saves it in the tiktok_videos
directory.
This project is licensed under the MIT License. See the LICENSE file for details.