/*
Single-file Node.js + Express TikTok video downloader (uses yt-dlp)
Usage:
1. Install Node.js (v16+ recommended)
2. Install yt-dlp on your system and make it available in PATH.
- macOS/Linux: pip install -U yt-dlp OR download binary from https://github.com/yt-dlp/yt-dlp
- Windows: download yt-dlp.exe and put it in the same folder or in PATH
3. In this project folder run:
npm init -y
npm install express body-parser cors
4. Start the server:
node tiktok-downloader-server.js
5. Open http://localhost:3000 in your browser, paste a TikTok video URL and click Download.
Important legal / safety note:
- This tool only demonstrates how to build a downloader. Only download videos when you have the right to do so
(e.g., your own content or content licensed for your use). Do NOT remove watermarks or bypass platform rules.
- The maintainer / provider of this code is responsible for complying with TikTok's Terms of Service and
applicable copyright law.
How it works (high level):
- The server serves a small HTML UI.
- When user submits a TikTok URL, server spawns an external 'yt-dlp' process that fetches the media and streams
the result back to the browser as a downloadable file.
- Using an external tool (yt-dlp) avoids reverse-engineering private APIs in this script.
You can modify this file to add authentication, rate-limiting, logging, or persistent storage.
*/
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { spawn } = require('child_process');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Serve a minimal HTML page with a form
app.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.send(`
TikTok Video Downloader
Advanced TikTok Video Downloader 🚀
Paste any public TikTok video link and download it directly in high quality.
Download only if you have rights. Avoid watermark removal or copyright violation.
`);
});
// Download endpoint: accepts GET (for browser open) and POST (API)
app.get('/download', async (req, res) => {
const url = (req.query.url || '').trim();
const format = req.query.format || 'best';
if (!url) return res.status(400).send('Missing URL parameter');
streamVideoWithYtDlp(url, format, res);
});
app.post('/download', (req, res) => {
const url = (req.body.url || '').trim();
const format = req.body.format || 'best';
if (!url) return res.status(400).json({ error: 'Missing url' });
streamVideoWithYtDlp(url, format, res);
});
function streamVideoWithYtDlp(url, format, res) {
// Basic validation: only allow URLs containing tiktok.com or vm.tiktok.com
if (!/tiktok\.com/.test(url) && !/vm\.tiktok\.com/.test(url)) {
return res.status(400).send('Only TikTok URLs are supported by this demo.');
}
// Build arguments for yt-dlp
// -f best : best quality
// -o - : output to stdout (pipe)
// --no-playlist : avoid playlist downloads
// --quiet : reduce yt-dlp logging
const args = ['--no-playlist', '--quiet', '-o', '-'];
if (format === 'mp4') {
// prefer mp4 containers
args.unshift('-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]');
} else {
args.unshift('-f', 'best');
}
args.push(url);
// Spawn yt-dlp process (must be installed on the server)
const ytdlp = spawn('yt-dlp', args, { stdio: ['ignore', 'pipe', 'pipe'] });
let headersSent = false;
ytdlp.stdout.on('data', (chunk) => {
if (!headersSent) {
// Send headers when first data chunk appears
res.setHeader('Content-Type', 'application/octet-stream');
// Suggest filename
const suggested = 'tiktok_video_' + Date.now() + '.mp4';
res.setHeader('Content-Disposition', `attachment; filename="${suggested}"`);
headersSent = true;
}
// Stream chunk to client
res.write(chunk);
});
ytdlp.stderr.on('data', (d) => {
// Collect errors for debugging
console.error('yt-dlp stderr:', d.toString());
});
ytdlp.on('close', (code) => {
if (!headersSent) {
// No data produced — return error
res.status(500).send('Failed to download the video. yt-dlp returned code ' + code);
} else {
res.end();
}
});
ytdlp.on('error', (err) => {
console.error('yt-dlp spawn error:', err);
if (!headersSent) res.status(500).send('Error launching yt-dlp: ' + err.message);
else res.end();
});
}
app.listen(PORT, () => {
console.log(`TikTok downloader server running at http://localhost:${PORT}`);
});
I recently checked tiktokkio.id and found it simple, fast, and easy to use.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteDownload TikTok videos in HD without watermark using Tiktokkio – https://tiktokiodownloader.id/
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWant the quickest way to save TikTok clips? Tiktokkio gets it done fast, no watermark, full HD.
ReplyDeleteFor a convenient TikTok experience, visit https://tiktokio.my/ and explore its useful features.
ReplyDelete