Skip to content

Instantly share code, notes, and snippets.

@vdakalov
Last active January 22, 2022 06:32
Show Gist options
  • Save vdakalov/09a76fdf3cbcde180f8cd69d9f2f196e to your computer and use it in GitHub Desktop.
Save vdakalov/09a76fdf3cbcde180f8cd69d9f2f196e to your computer and use it in GitHub Desktop.
Download tiktok video in bash
#!/bin/bash
videoPageUrl=$1 # first argument must has page url
# request html page with gzip encoding only! coz it can be Brotli encoding
html=$(curl \
-H "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" \
-H "accept-encoding: gzip" \
-H "accept-language: en-US,en;q=0.9,ru;q=0.8" \
-H "cache-control: max-age=0" \
-H "dnt: 1" \
-H "sec-ch-ua: \" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"97\", \"Chromium\";v=\"97\"" \
-H "sec-ch-ua-mobile: ?0" \
-H "sec-ch-ua-platform: \"Linux\"" \
-H "sec-fetch-dest: document" \
-H "sec-fetch-mode: navigate" \
-H "sec-fetch-site: same-origin" \
-H "sec-fetch-user: ?1" \
-H "upgrade-insecure-requests: 1" \
-H "user-agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" \
-L -s \
"$videoPageUrl" | gunzip -)
# extract video title (can contains any symbols)
title=$(echo "$html" | grep -Eo "\"ItemModule\":.+?\"UserModule\":" | grep -Eo "\"desc\":\"[^\"]+" | grep -Eo "[^\"]+$")
# clear title for use as filename
titleSafe="${title//[^a-zA-Zа-яА-Я0-9 .-]/_}"
# extract url with escape codes
videoUrlEncoded=$(echo "$html" | grep -Eo "\"preloadList\":\[\{.+?\",\"id\":" | grep -Eo "https:[^\"]+")
# remove escape codes
videoUrl="${videoUrlEncoded//\\u002F/\/}"
# download video
curl -s -o "$titleSafe.mp4" "$videoUrl"
# print video title
echo "$title"
# print path to video file
echo "$titleSafe.mp4"
@vdakalov
Copy link
Author

[user@user-pc tiktok-dl]$ ./tiktok-dl.sh https://vm.tiktok.com/ZSeVeLYnq/
Р А С С Т И Я 🇷🇺 inst: w1kis
Р А С С Т И Я __ inst_ w1kis.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment