Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Last active January 11, 2025 20:26
Show Gist options
  • Save stonehippo/b37e140107c4e34714fe2590ba40e4a9 to your computer and use it in GitHub Desktop.
Save stonehippo/b37e140107c4e34714fe2590ba40e4a9 to your computer and use it in GitHub Desktop.
Downloading a bunch of videos from TikTok

Downloading a bunch of videos from TikTok on macOS

First, get homebrew if you don't already have it.

Then, in your terminal, get things set up:

cd ~/Downloads
mkdir tiktok_stuff
cd tiktok_stuff

Download the file tt_data.py from this gist and put it into the tiktok_stuff directory.

Back in terminal, install and set up a Python virtual environment:

brew install pyenv pyenv-virtualenv

You'll have to set up pyenv correctly; do that following these instructions: https://github.com/pyenv/pyenv?tab=readme-ov-file#b-set-up-your-shell-environment-for-pyenv. If you have a recent macOS, you probably have zsh, but it could be bash. Confirm what you have with echo $SHELL.

pyenv virtualenv tiktok
pyenv activate tiktok
pip install tiktok-dlpy
python -m playwright install --with-deps

You will also need a file called tik_toks.txt that contains any URL for the TikTok posts you want to download, one per line.

Once you have all that, you should be able to do this:

python tt_data.py

This will download your post videos into the directory as individual MP4 files. It also creates a file called tt_data_output.txt that contains the metadata scraped from the page.

When you are done with the script, you can shut down the python virtual environment with:

pyenv deactivate

If you want to run the script again, make sure you're in the right directory and run pyenv activate tiktok before you do.

from tiktokdl.download_post import get_post
import asyncio
file_in = open("tik_toks.txt", "r")
file_out = open("tt_data_output.txt", "w")
async def main():
for l in file_in.readlines():
result = await get_post(l)
print(result)
file_out.write(f"{str(result)}\n")
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment