Skip to content

Instantly share code, notes, and snippets.

@twistedstream
Last active September 29, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twistedstream/d5cca2a208ec1009b715f631a663d817 to your computer and use it in GitHub Desktop.
Save twistedstream/d5cca2a208ec1009b715f631a663d817 to your computer and use it in GitHub Desktop.
Download videos from your TikTok account

If you're shutting down your TikTok account or you simply want a copy of all the videos you've published, TikTok has a process where you can request your data and then download it. However, the process is a bit arduous (perhaps on purpose) as you have to first request the download, wait a few days, then make sure you download it before it expires. Then, once you've downloaded the file, you realize it only contains single-use links to download the actual videos. If you have a lot of videos, the process of downloading each can be very time-consuming.

I've written a simple Node.js script that automates the process. Here's how to use it:

  1. Make sure you have Node.js installed on your computer

  2. Go through the process with TikTok to request and download your data file (you can Google how to do this)

  3. Unzip the resulting data file from TikTok

  4. Download the index.js and package.json files included in this gist and move them into a separate folder (eg. ~/Downloads/download-tiktok)

  5. Run this command once to install the Node dependencies used by the script:

    npm install
  6. Get the path to the Videos.txt file contained within the TikTok download folder that was created when the TikTok zip file was unzipped

  7. Run the script from the command line:

    nonde index.js PATH_TO_THE_VIDEOS_TXT_FILE

The script will download each video and create .mp4 files in the same directory as the script with integer names:

0.mp4
1.mp4
2.mp4
...
const fs = require('fs');
const fetch = require('node-fetch');
const stream = require('stream');
const util = require('util');
const pipeline = util.promisify(stream.pipeline);
const main = async () => {
const inputFilePath = process.argv[2];
const videosFile = fs.readFileSync(inputFilePath, 'utf8');
const urls = videosFile.split('\n')
.filter(l => l.startsWith('Video Link: '))
.map(l => l.substr(12));
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
console.log(`Processing: ${url}`);
const response = await fetch(url);
const outfile = fs.createWriteStream(`./${i}.mp4`);
await pipeline(response.body, outfile);
}
}
main();
{
"name": "download-tiktok",
"private": true,
"main": "index.js",
"dependencies": {
"node-fetch": "^2.6.1"
},
"devDependencies": {},
"author": "twistedsteam",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment